Change tension for a smooth curve in C#


[curve][curve]

The post Draw a smooth curve connecting points in C# explains how you can use the Graphics class’s DrawCurve method to draw a smooth curve that connects a sequence of points. It uses the following code.

e.Graphics.DrawCurve(Pens.Red, Points.ToArray());

This example uses the following version of the code.

e.Graphics.DrawCurve(Pens.Red, Points.ToArray(), Tension);

This overload of the DrawCurve method takes as a last parameter a tension value that determines how tightly the curve should bend while following the points. (This parameter might have been better named “looseness” because larger values make the curve looser.)

A value of 0.0 makes DrawCurve connect the points with straight lines so you get a polyline. The value 0.5 is the default if you don’t include a tension value. Normally the tension value should be between 0.0 and 1.0, although sometimes large values provide interesting results. For example, the picture on the right above shows the loopy result you get when tension is 5.0.

When you adjust this example’s track bar, the program updates the Tension variable and redraws so you can see the result.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.

This entry was posted in drawing, graphics and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *