How can I create a simple 2D NURBS using XAML?

拥有回忆 提交于 2019-12-07 12:12:58

问题


I need to create a spline with two endpoints and 'n' control points.
As far as I am aware, a Bezier curve allows for only one control point, and a Bezier spline allows for two control points. However, I need to be able to add as many control points as I see fit, not limited to one or two.

Here is an example of what I want to achieve, with 4 control points:
(Source: Wikipedia article on NURBS)


So far I've only been able to combine a series of BezierSegments together like this:
http://img297.imageshack.us/img297/3706/bezierpath.png
<Polyline   Stroke="Green" Stretch="Uniform"
            Points="0,0 1,2 2,1 3,3 4,3 5,2 6,3 7,2 8,1.75 9,2.5" />

<Path Stroke="Red" Stretch="Uniform">
  <Path.Data>
    <PathGeometry>
      <PathGeometry.Figures>
        <PathFigureCollection>
          <PathFigure StartPoint="0,0">
            <PathFigure.Segments>
              <PathSegmentCollection>
                <BezierSegment Point1="1,2" Point2="2,1" Point3="3,3" />
                <BezierSegment Point1="4,3" Point2="5,2" Point3="6,3" />
                <BezierSegment Point1="7,2" Point2="8,1.75" Point3="9,2.5" />
              </PathSegmentCollection>
            </PathFigure.Segments>
          </PathFigure>
        </PathFigureCollection>
      </PathGeometry.Figures>
    </PathGeometry>
  </Path.Data>
</Path>

回答1:


Not out of the box but take a look at this previous question it will point you to how to draw NURBS using c#, you can then turn the code into something then implements PathSegment to used it under WPF.




回答2:


Although in the title you mention NURBS, you seem to be looking for a way to draw a series of Bezier Spline segments that are smooth at the connection points.

To achieve this you need to set the point1 on the second segment so it will mirror point2 on the 1st segment relative to the connection point between the segments (which is point3 on the 1st segment).

In your case the connection point is (3,3) and point2 on the first segment is (2,1) so you need to make point1 on the second segment (4,5) instead of (4,3) in your code.

Similarity change point1 on the 3rd segment to (7,4) so it mirrors (5,2) relative to (6,3) and you end up with the following segments that might solve your problem:

 BezierSegment Point1="1,2" Point2="2,1" Point3="3,3"

 BezierSegment Point1="4,5" Point2="5,2" Point3="6,3" 

 BezierSegment Point1="7,4" Point2="8,1.75" Point3="9,2.5" 

Now, if you don’t like me changing your control points and really want (4,3) and (7,2) to be control points, add more segments. Just remember that you don’t get to decide what point1 is, if you want smooth connectivity with the previous segment.

Notes:

  1. this solution will give you a 1st degree of smothness at the connection points. If you want also a second degree of smothness (C2) you can achieve that by setting up point2 appropritaly. If you take point1 of the previous segment and mirror it relative to point2 of the previous segment and then mirror the result relative to point1 of the current segment you will get the desired point2 of the current segment. Then you simply choose any point3 you desire and you are done. See http://ibiblio.org/e-notes/Splines/B-spline.htm for more on this.

  2. The exmaple you gave from wikipedia is a NURBS. A NURBS is like a bezier spline, but it adds weights to the points. From what I can tell from the documentation BezierSegment does not support that.



来源:https://stackoverflow.com/questions/1456162/how-can-i-create-a-simple-2d-nurbs-using-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!