GDI+ Tutorials: A Bézier Curve


A Bézier Curve

A bézier curve is a continuous line that is drawn using four points that are not necessarily aligned. It can be illustrated as follows:

To draw this line (with four points), the compiler would draw a curve from the first point to the fourth point. Then it would bend the curve by bringing each middle (half-center) side close to the second and the third points respectively, without touching those second and third points. For example, the above bézier curve could have been drawn using the following four points:

To draw a bézier curve, the Graphics class provides the DrawBezier() method that is overloaded in three versions whose syntaxes are:

    void DrawBezier(Pen ^pen,
                    Point pt1,
                    Point pt2,
                    Point pt3,
                    Point pt4);
    void DrawBezier(Pen ^pen,
                    PointF pt1,
                    PointF pt2,
                    PointF pt3,
                    PointF pt4);
    void DrawBezier(Pen ^pen,
                    float x1,
                    float y1,
                    float x2,
                    float y2,
                    float x3,
                    float y3,
                    float x4,
                    float y4);

Based on this, to draw a bézier line, you can use either four Point or PointF values or the coordinates of the four points. Here is an example:

System::Void Form1_Paint(System::Object ^  sender, 
			 System::Windows::Forms::PaintEventArgs ^  e)
    Pen ^ penCurrent = gcnew Pen(Color::Blue);
    Point pt1 = Point(20,  12),
                pt2 = Point(88, 246),
                pt3 = Point(364, 192),
                pt4 = Point(250,  48);

    e->Graphics->DrawBezier(penCurrent, pt1, pt2, pt3, pt4);

This would produce:

Bezier Curve



Home Copyright © 2007-2012 FunctionX