Home

GDI+ Shapes: A Closed Curve

Introduction

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, Point[] points);
public void DrawClosedCurve(Pen pen, 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:

private void Exercise_Paint(object sender, PaintEventArgs e)
{
        Pen penCurrent = new Pen(Color.Blue);
        Point[] pt = { new Point(40,  42), new Point(188, 246),
                          new Point(484, 192), new 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, 
			 Point[] points, 
			 float tension, 
			 FillMode fillmode);
public void DrawClosedCurve(Pen pen, 
			 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:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

public class Exercise : Form
{
    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        Paint += new PaintEventHandler(Exercise_Paint);
    }

    private void Exercise_Paint(object sender, PaintEventArgs e)
    {
        Pen penCurrent = new Pen(Color.Red);
        Point[] pt = { new Point(40,  42), new Point(188, 246),
                          new Point(484, 192), new Point(350,  48) };

        e.Graphics.DrawClosedCurve(penCurrent, pt, 0.75F,
                     FillMode.Winding);
    }
}

public class Program
{
    public static int Main()
    {
        Application.Run(new Exercise());

        return 0;
    }
}

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 © 2010-2020, FunctionX