问题描述
如何使用c#绘图类绘制类似弹簧的形状
How to draw the spring like shape using c# drawing class
替代文本http://img812.imageshack.us/img812/373/spring .jpg
推荐答案
与C#相比,这更多的是数学问题.您想要为要绘制的曲线导出参数方程.
This is more of a math problem than a C# one. What you want is to derive a Parametric equation for the curve you wish to draw.
通过该操作,以一定的间隔和一定的步长,用参数方程式的值填充Point对象数组(步长越小,最终图形看起来就越像实际形状).然后,您可以使用g.DrawLines( MSDN:DrawLines )绘制表面上的实际曲线.
With that go and fill an array of Point objects with values for the parametric equation on a certain interval with a certain step (the smaller the step the more the final drawing will look like the actual shape). Then you can use g.DrawLines (MSDN: DrawLines) to draw the actual curve on a surface.
您可以通过修改Pen对象的参数来编辑线的宽度,颜色和其他属性.
You can edit the width, color and other properties of the line by modifying parameters of the Pen object.
您的实际代码如下:
void DrawSpring (Graphics g)
{
List<Point> points = new List<Point>();
double step = 0.01;
for(double t = -2; t < 2; t += step)
{
Point p = new Point();
p.X = XPartOfTheEquation(t);
p.Y = YPartOfTheEquation(t);
points.Add(p);
}
g.DrawLines(new Pen(new SolidBrush(Color.Black), 2f), points.ToArray());
}
这篇关于春天像在C#中绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!