從您的代碼段我假設你想繪製一條曲線。要做到這一點,你可以看看GraphicsPath
。您可以將點用作座標,而不是繪製單個點,而是通過線連接。然後,在您的代碼中,您可以使用AddLine
方法創建GraphicsPath
。例如,這可以被繪製到位圖上。
編輯
樣品(未測試):
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);
另一種方法是使用WPF Path
類,它會的工作大致相同,但它是一個真正的UI元素,你可以添加到Canvas
的孩子。
編輯
人士指出,上面的代碼是Windows窗體代碼。嗯,這裏是你可以在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);
的'Point'類不代表視覺點,但只是一組,你可以使用座標定義**你的點應顯示在哪裏。 – 2012-01-18 14:29:30