1

How can I draw Quadratic Curve through 3 points by using C# System.Drawing namespace?

warvariuc
  • 57,116
  • 41
  • 173
  • 227
Pavel Podlipensky
  • 8,201
  • 5
  • 42
  • 53

1 Answers1

6

Do you want to draw a quadratic curve that goes through three given points, or do you want to draw a quadratic Bézier curve that uses three given points?

If what you want is a Bézier curve, try this:

private void AddBeziersExample(PaintEventArgs e)
{

    // Adds a Bezier curve.
    Point[] myArray =
             {
                 new Point(100, 50),
                 new Point(120, 150),
                 new Point(140, 100)
             };

    // Create the path and add the curves.
    GraphicsPath myPath = new GraphicsPath();
    myPath.AddBeziers(myArray);

    // Draw the path to the screen.
    Pen myPen = new Pen(Color.Black, 2);
    e.Graphics.DrawPath(myPen, myPath);
}

Which I just shamelessly lifted from the MSDN documentation for GraphicsPath.AddBeziers().

Edit: If what you really want is to fit a quadratic curve, then you need to do a curve fitting or polynomial interpolation on your points. Perhaps this answer from Ask Dr. Math will help.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • Just to note that `GraphicsPath.AddBeziers()` takes `Point[]` array of at least 4 points. So if you want to draw the curve through 3 points, just pass the middle point two times: `Point[] points = { startPoint, middlePoint, middlePoint, endPoint };` – Da2da2 Jul 09 '22 at 21:10