The example Change tension for a smooth curve in C# shows how to use the Graphics class’s DrawCurve draw method to draw a smooth curve connecting a sequence of points. The method’s tension parameter lets you change how directly the curve visits each point.
This example is similar but when you select points, it draws a collection of curves with different tension values. The following code shows how the program draws its curves.
// The points selected by the user. private List<Point> Points = new List<Point>(); private void Form1_Paint(object sender, PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; // Draw the points. foreach (Point point in Points) e.Graphics.FillEllipse(Brushes.Black, point.X - 3, point.Y - 3, 5, 5); if (Points.Count < 2) return; // Draw the curve. using (Pen pen = new Pen(Color.Red)) { for (int t = 0; t <= 20; t += 2) { pen.Color = Color.FromArgb( 255 * t / 20, 0, 255 - 255 * t / 20); e.Graphics.DrawCurve(pen, Points.ToArray(), t / 10f); } } }
Like the previous example, this program stores point data in a List<Point>. The Paint event handler draws circles representing the points. It creates a new pen and then loops over the values 0, 2, 4, … 20. It divides each of those values by 20 to get a tension value between 0 and 1. It sets the pen’s color so it starts blue and shades to red, and then draws the curve with the given color and tension.




Pingback: Draw a curve with negative tension in C# - C# HelperC# Helper