Visual C#

GDI+ Shapes: Curves

A curve is a line that joins two or more points. If only two points are involved, the line would join them but the line is not straight. If there are…

4 min read828 words11 code samples

Introduction#

A curve represents a non-straight line that links two or more coordinates. In cases where only two points are present, the line connects them with a bend rather than a straight path. For a set of three points A, B, and C, the curve starts at A, passes through B, and concludes at C. When more than three points are provided, the path begins at the first, traverses each intermediate point, and finishes at the final one. These points don't need to be collinear; indeed, the primary goal of a curve is to link non-aligned points with a continuous, flowing line. Three example curves are shown here, marked C1, C2, and C3:

The first curve, C1, has only two points. The second curve, C2, has three points. The third curve, C3, has four points. The section between any two points is called a segment, so a curve can also be described by the number of segments it contains. If a curve has only two points, then it has only one segment from the first point to the second, which is also the last point. If a curve has three points, it has two segments: one from the first point to the second, and another from the second to the third. From this, the number of segments in a curve equals the number of points minus 1. In GDI+, you can draw a curve with the Graphics.DrawCurve() method. To do that, you specify how many points are involved, usually by declaring an array of Point or PointF values. Because you decide how many points to use, the Graphics class offers the following DrawCurve() syntax:

C#
public void DrawCurve(Pen pen, Point[] points);
public void DrawCurve(Pen pen, PointF[] points);

This version of the method takes an array of Point or PointF values as arguments. The number of members of the array depends on you. Here is an example that uses four points to draw a curve with three segments:

C#
using System;
using System.Drawing;
using System.Windows.Forms;

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.Blue);
        Point[] pt = { new Point( 40,  42),
	              new Point(188, 246),
                              new Point(484, 192),
	              new Point(350,  48) };

        e.Graphics.DrawCurve(penCurrent, pt);
    }
}

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

        return 0;
    }
}

This would produce:

As you can see, when the curve is drawn, a bent line crosses the intermediary points between the first and the last. To make the lines non-straight, the compiler uses a value called tension used to bend the line. If you want, you can specify the bending factor that should be applied. To do this, you would use the following version of the DrawCurve() method:

C#
public void DrawCurve(Pen pen,
   		      Point[]points, 
		      float tension);
public void DrawCurve(Pen pen, 
		      PointF[] points, 
		      float tension);

The amount of bending to apply is passed as the tension argument. It can be passed as a decimal value >= 0.00. If this value is passed as 0.00, the lines would be drawn straight. Here is an example:

C#
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.DrawCurve(penCurrent, pt, 0.00F);
}

This means that, if you want a real curve, either you don't pass the tension argument and use the first version of the method or you pass the tension argument as a value higher than 0.00. Here is an example:

C#
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.DrawCurve(penCurrent, pt, 2.15F);
}

This would produce:

Both versions of the DrawCurve() method that we have used allow you to start the curve on the first point. Consider the following example that draws a curve of five points resulting in four segments:

C#
private void Exercise_Paint(object sender, PaintEventArgs e)
{
        Pen penCurrent = new Pen(Color.Blue);
        PointF[] pt = { new PointF(20.00F,  322.00F),
		new PointF(124, 24),
	            	new PointF(214, 242), 
		new PointF(275,  28),
         	    	new PointF(380.00F,  322.00F) };

        e.Graphics.DrawCurve(penCurrent, pt);
}

Consequently, to produce a genuine curve, you can either omit the tension parameter to use the default method or provide a tension value greater than 0.00. Consider this example:

If you want, you can start the curve on any point instead of the first. To support this, the Graphics class provides the following version of the DrawCurve() method:

C#
public void DrawCurve(Pen pen, 
	           PointF[] points, 
	           int offset, 
	           int numberOfSegments);

The offset argument allows you to specify how many points should be skipped before starting to draw. The first conclusion is that the value of the offset must be 0 or higher. If you pass this argument as 0, the curve would be drawn from the first point. If you pass this argument as 1, the first point would not be considered in the curve. This means that the drawing of the curve would start on the second point and so on. If you pass it as 2, the first and the second point would not be part of the curve, meaning that the curve would start on the third point. After the curve has started from the point you specify using the offset argument, you can then specify how many segments of the curve would be drawn. This number must be lower than the number of available segments, that is after subtracting the offset value from the total number of segments of the array. Here is an example:

C#
private void Exercise_Paint(object sender, PaintEventArgs e)
{
        Pen penCurrent = new Pen(Color.Blue);
        PointF[] pt = { new PointF(20.00F,  322.00F),
			   new PointF(124, 24),
	            	   new PointF(214, 242), 
			   new PointF(275,  28),
                    	   new PointF(380.00F,  322.00F) };

        e.Graphics.DrawCurve(penCurrent, pt, 1, 2);
}

This would produce:

Once again, the compiler arranges to apply a tension when drawing the curve. If you would prefer to use straight lines or to apply a different tension than the default, you can use the following version of the Graphics.DrawCurve() method:

C#
public void DrawCurve(Pen pen, 
		   Point[] points, 
		   int offset, 
		   int numberOfSegments, 
		   float tension);
public void DrawCurve(Pen pen, 
		   PointF[] points, 
		   int offset, 
		   int numberOfSegments, 
		   float tension);

This time, you can pass the value of the tension as 0 to get straight lines:

C#
private void Exercise_Paint(object sender, PaintEventArgs e)
{
        Pen penCurrent = new Pen(Color.Blue);
        PointF[] pt = { new PointF(20.00F,  322.00F),
			   new PointF(124, 24),
	                   new PointF(214, 242),
			   new PointF(275,  28),
                	   new PointF(380.00F,  322.00F) };

        e.Graphics.DrawCurve(penCurrent, pt, 0, 4, 0);
}

This would produce:

Or you can pass the tension with any positive value of your choice. Here is an example:

C#
private void Exercise_Paint(object sender, PaintEventArgs e)
{
        Pen penCurrent = new Pen(Color.Blue);
        PointF[] pt = { new PointF(20.00F, 322.00F),
                        new PointF(124, 24),
	                new PointF(214, 242),
	                new PointF(275, 28),
                    	new PointF(380.00F, 322.00F) };

        e.Graphics.DrawCurve(penCurrent, pt, 1, 3, 1.750F);
}

Alternatively, you may provide any positive tension value you prefer. Below is an example of this approach:

Home Copyright © 2010-2011 C# Key