我怎样才能得到最接近三次贝塞尔曲线来给点意见? [英] How can I get a cubic bezier curve closest to given points?

查看:177
本文介绍了我怎样才能得到最接近三次贝塞尔曲线来给点意见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于N点:

P0,P1,P2,...,PN

p0, p1, p2, ..., pn;

我如何能得到点C1,C2,使得由

How can I get the point c1, c2 so that the cubic bezier curve defined by

P0,C1,C2,PN

p0, c1, c2, pn

最接近给点意见?

我试过最小二乘法。我写了这个之后,我读的http://www.mathworks.com/matlabcentral/fileexchange/15542-cubic-bezier-least-square-fitting.但我不能找到一个很好的T(I)的功能。

I tried least square method. I wrote this after I read the pdf document in http://www.mathworks.com/matlabcentral/fileexchange/15542-cubic-bezier-least-square-fitting. But I can't find a good t(i) function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace BezierFitting
{
    class CubicBezierFittingCalculator
    {
        private List<Point> data;

        public CubicBezierFittingCalculator(List<Point> data)
        {
            this.data = data;
        }

        private double t(int i)
        {
            return (double)(i - 1) / (data.Count - 1);
            // double s = 0.0, d = 0.0;
            // 
            // for (int j = 1; j < data.Count; j++)
            // {
            //     if (j < i)
            //     {
            //         s += (data[j] - data[j - 1]).Length;
            //     }
            //     d += (data[j] - data[j - 1]).Length;
            // }
            // return s / d;
        }

        public void Calc(ref Point p1, ref Point p2)
        {
            double n = data.Count;
            Vector p0 = (Vector)data.First();
            Vector p3 = (Vector)data.Last();

            double a1 = 0.0, a2 = 0.0, a12 = 0.0;
            Vector c1 = new Vector(0.0, 0.0), c2 = new Vector(0.0, 0.0);
            for (int i = 1; i <= n; i++)
            {
                double ti = t(i), qi = 1 - ti;
                double ti2 = ti * ti, qi2 = qi * qi;
                double ti3 = ti * ti2, qi3 = qi * qi2;
                double ti4 = ti * ti3, qi4 = qi * qi3;
                a1 += ti2 * qi4;
                a2 += ti4 * qi2;
                a12 += ti3 * qi3;

                Vector pi = (Vector)data[i - 1];
                Vector m = pi - qi3 * p0 - ti3 * p3;
                c1 += ti * qi2 * m;
                c2 += ti2 * qi * m;
            }
            a1 *= 9.0;
            a2 *= 9.0;
            a12 *= 9.0;
            c1 *= 3.0;
            c2 *= 3.0;

            double d = a1 * a2 - a12 * a12;
            p1 = (Point)((a2 * c1 - a12 * c2) / d);
            p2 = (Point)((a1 * c2 - a12 * c1) / d);
        }
    }
}

什么是获得三次Bezier曲线最靠近指定点的最佳方式是什么?

What's the best way to get a cubic bezier curve closest to given points?

例如,这里有30点:

22, 245
26, 240
39, 242
51, 231
127, 189
136, 185
140, 174
147, 171
163, 162
169, 155
179, 107
181, 147
189, 168
193, 187
196, 75
199, 76
200, 185
201, 68
204, 73
205, 68
208, 123
213, 118
216, 210
216, 211
218, 68
226, 65
227, 110
228, 102
229, 87
252, 247

这些点分布在三次Bezier曲线由四个点controled:

Those points are distributed around the the cubic bezier curve controled by four points:

P 0(0,256)中,P1(512,0),P 2(0,0),P3(256,256)。

P0 (0, 256), P1 (512, 0), P2 (0, 0), P3 (256, 256).

假设曲线是从(0,256)到(256,256),如何得到休息两个控制点接近原始点?

Suppose the curve is from (0, 256) to (256, 256), how to get rest two control points close to the origional points?

推荐答案

您的问题是很辛苦,如果你想创建一个尖的曲线。我能想到一个启发式的创建初始的控制点。对于第一个控制点,尝试采取从到第一个锚点的距离来分类的,当你有可用的点的前1/3。该排序是必要的,否则,你可能会被遍布跳跃。把你点了1/3,并做了线性最小二乘法拟合,这是具有线性时间复杂度。这让你的曲线需要起飞方向。做同样的事情在过去1/3,你有落地的方向发展。

Your problem is very hard if you want to create curves with cusps. I can think of a heuristic to create an initial set of control points. For the first control point, try taking the first 1/3 of the points you have available, when sorted from the distance to the first anchor point. The sorting is necessary, otherwise, you may be jumping all over. Take that 1/3 of your points and do a linear least squares fit, which is has linear time complexity. That gives you the direction your curve needs to take off. Do the same thing with the last 1/3, and you have the "landing" direction.

使用这些线性解决方案,打造载体指向远离锚点,然后尝试使这些载体更长和更短,试图尽量减少误差。的控制点是沿着从锚点那些载体

Use those linear solutions to create vectors pointing away from the anchor points, then try making those vectors longer and shorter, trying to minimize the error. The control points would be along those vectors from the anchor points.

下面是一些其他的想法(我只能上传两个环节!): 物理论坛问题 贝塞尔曲线拟合论文

Here are some other ideas (I can only post two links!): physics forum question bezier curve fitting thesis

这篇关于我怎样才能得到最接近三次贝塞尔曲线来给点意见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆