Home

GDI+ Tutorials: A Closed Curve

 

A Closed Curve

If you use either the DrawLines(), the DrawBezier() or the DrawBeziers() methods, you would get a continuous line or a series of lines that has a beginning and an end. Alternatively, GDI+ allows you to draw a series of lines but join the end of the last line to the beginning of the first line to have a closed shape. To draw this figure, you can call the Graphics::DrawClosedCurve() method that is overloaded in four versions. Two of them have the following syntaxes:

public:
    void DrawBeziers(Pen^ pen, array<Point>^ points);
    void DrawClosedCurve(Pen^ pen, array<PointF>^ points);

These two versions are the easiest to use. They allow you to provide an array of four Point or four PointF values. When executed, each of these methods draws a curve that passes through each coordinate and closes the curve by joining the end point to the first unless both points are the same. Here is an example:

System::Void Form1_Paint(System::Object ^  sender, 
				  System::Windows::Forms::PaintEventArgs ^  e)
{
    Pen ^ penCurrent = gcnew Pen(Color::Blue);
    array<Point> ^ pt = { Point(40,  42), Point(188, 246),
                          Point(484, 192), Point(350,  48) };

    e->Graphics->DrawClosedCurve(penCurrent, pt);
}

This would produce:

Closed Curve

The first two versions are used to draw the lines but curve them in order to make the shape appear smooth. If you want, you can draw the lines straight from one point to the next without curving them. Using this scenario, the above shape would appear as follows:

Closed Shape With Straight Lines

To draw this type of shape using the DrawClosedCurve() method, you can use one of the following versions of the method:

public:
    void DrawClosedCurve(Pen^ pen, 
			 array<Point>^ points, 
			 float tension, 
			 FillMode fillmode);
    void DrawClosedCurve(Pen^ pen, 
			 array<PointF>^ points, 
			 float tension, 
			 FillMode fillmode);

These versions allow you to specify the tension and the fill mode. The tension factor allow you to specify how much curve would be applied. If this value is passed as 0.00, the points would be joined with straight lines. Otherwise, you can apply a tension using an appropriate decimal value.

The fillmode factor determines how the interior of the curve would be filled. It is controlled through the FillMode enumerator that is defined in the System::Drawing::Drawing2D namespace. The FillMode enumerator has two members: Alternate and Winding. Here is an example:

System::Void Form1_Paint(System::Object ^  sender,
			 System::Windows::Forms::PaintEventArgs ^  e)
{
    using namespace System::Drawing::Drawing2D;

    Pen ^penCurrent = gcnew Pen(Color::Red);
    array<Point> ^ pt = { Point(40,  42), Point(188, 246),
                          Point(484, 192), Point(350,  48) };

    e->Graphics->DrawClosedCurve(penCurrent, pt, 0.75F,
				 FillMode::Winding);
}

This would produce:

Closed Curve

Remember that the higher the tension, the sharper the corners. If you want the shape to show straight lines, pass a tension of 0.00F.

 

Home Copyright © 2007-2012 FunctionX