Search code examples
c#mathlinebeziercurve

How to link curves


First of all, let me make it clear that I don't know much about programming. So after I got that out of the way, thanks for reading my question.

So what I currently want to cram into my little C# programm is the following:

  • Draw a line from pA to pX
  • Draw a curve from pX to pY
  • Draw a curve from pY to pZ
  • Draw a line from pZ to pD

My problem with this is the following:

How on earth do I "switch" from a line to a curve, to another curve and then back to a line in C#?

I'd be really happy if anyone could help me with this.

Greetings from Belgium,

-g2609


Solution

  • Seems you want to provide smooth connection of line segments and curves.

    Note that Bezier curves at the end points have direction (tangents) to control points. So just put control points at the continuation of straight segments. Distance from the enpoints to the control ones is responsible for curvature. Try to use values like distXY / 3 to start.

    For curve-curve connection you have to define some rule. For example, define tangent direction (and maginute again). If you need smooth curve chain, consider interpolation splines - this approach calculates cubic curves parameters for all curves and provides continuity.

    Pseudocode for line A-X, cubic Bezier X-Y, line Y-Z.

     VecAX = X - A
     uAX = (VecAX.X / VecAX.Length, VecAX.Y / VecAX.Length)
     curveXY.P0 = X
     curveXY.P1 = X + uAX * VecAX.Length / 3
     curveXY.P2 = Y - uXZ * VecXZ.Length / 3
     curveXY.P3 = Y