2012-06-26 65 views
2

有沒有人看到一個體面的繪圖算法cornu螺旋(aka,clothoids)或樣條曲線?對於弧線,我們有像Bresenham算法這樣的東西。這適用於吉祥物嗎?cornu spiral/spline的高速繪圖算法?

的維基百科頁面有這個聖人代碼:

p = integral(taylor(cos(L^2), L, 0, 12), L) 
q = integral(taylor(sin(L^2), L, 0, 12), L) 
r1 = parametric_plot([p, q], (L, 0, 1), color = 'red') 

是否有參數地塊任何示例代碼可用?我沒有看到我的網頁搜索。

回答

2

我沒有看到任何現有的高速算法。但是,我的確瞭解了繪製這種東西的常用方法。本質上,您遞歸地分割L,直到計算出的左,中,右點與足夠靠近您繪製該線的直線相距足夠近。我能夠使用MathNet.Numerics.dll進行集成。部分代碼:

public static void DrawClothoidAtOrigin(List<Point> lineEndpoints, Point left, Point right, double a, double lengthToMidpoint, double offsetToMidpoint = 0.0) 
    { 
     // the start point and end point are passed in; calculate the midpoint 
     // then, if we're close enough to a straight line, add that line (aka, the right end point) to the list 
     // otherwise split left and right 

     var midpoint = new Point(a * C(lengthToMidpoint + offsetToMidpoint), a * S(lengthToMidpoint + offsetToMidpoint)); 
     var nearest = NearestPointOnLine(left, right, midpoint, false); 
     if (Distance(midpoint, nearest) < 0.4) 
     { 
      lineEndpoints.Add(right); 
      return; 
     } 
     DrawClothoidAtOrigin(lineEndpoints, left, midpoint, a, lengthToMidpoint * 0.5, offsetToMidpoint); 
     DrawClothoidAtOrigin(lineEndpoints, midpoint, right, a, lengthToMidpoint * 0.5, offsetToMidpoint + lengthToMidpoint); 
    } 

    private static double Distance(Point a, Point b) 
    { 
     var x = a.X - b.X; 
     var y = a.Y - b.Y; 
     return Math.Sqrt(x * x + y * y); 
    } 

    private static readonly double PI_N2 = Math.Pow(Math.PI * 2.0, -0.5); 

    public static double C(double theta) 
    { 
     return Integrate.OnClosedInterval(d => Math.Cos(d)/Math.Sqrt(d), 0.0, theta)/PI_N2; 
    } 

    public static double S(double theta) 
    { 
     return Integrate.OnClosedInterval(d => Math.Sin(d)/Math.Sqrt(d), 0.0, theta)/PI_N2; 
    }