Search code examples
c#gdi+system.drawingquadratic

Draw Quadratic Curve


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


Solution

  • 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.