From your code snippet, I assume that you are trying to draw a curve. To do this, you can look at GraphicsPath . Instead of drawing individual points, you can use points in the form of coordinates that you connect along lines. Then in your code, you can create a GraphicsPath using the AddLine method.
Then it could be drawn on a bitmap, for example.
EDIT
Example (not verified):
GraphicsPath p = new GraphicsPath(); for (float x = x1; x < x2; x += dx) { Point point = new Point(); point.X = x; point.Y = Math.Sin(x); Point point2 = new Point(); point2.X = x+dx; point2.Y = Math.Sin(x+dx); p.AddLine(point, point2); } graphics.DrawPath(p);
Another way is to use the WPF Path class, which will work in roughly the same way, but is a real user interface element that you can add to child Canvas elements.
EDIT
People have indicated that the code above is Windows Forms code. Well, here is what you can do in WPF:
myPolygon = new Polygon(); myPolygon.Stroke = System.Windows.Media.Brushes.Black; myPolygon.Fill = System.Windows.Media.Brushes.LightSeaGreen; myPolygon.StrokeThickness = 2; myPolygon.HorizontalAlignment = HorizontalAlignment.Left; myPolygon.VerticalAlignment = VerticalAlignment.Center; PointCollection points = new PointCollection(); for (float x = x1; x < x2; x += dx) { Point p = new Point(x, Math.Sin(x)); points.Add(p); } myPolygon.Points = points; canvas1.Children.Add(myPolygon);
source share