像春天一样在c#中绘图

发布于 2024-09-13 07:38:21 字数 180 浏览 7 评论 0原文

如何使用c#绘图类绘制弹簧形状

alt text http://img812.imageshack .us/img812/373/spring.jpg

How to draw the spring like shape using c# drawing class

alt text http://img812.imageshack.us/img812/373/spring.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许一世地老天荒 2024-09-20 07:38:21

首先,您需要考虑一个代表弹簧的公式。你可以画一个圆,当你绕着它转时,让 X 增加一点。例如:

        for (double i = 0; i < 50; i += 0.01)
        {
            int x = (int)(Math.Sin(i) * 10 + i * 3);
            int y =(int)(Math.Cos(i) * 10 + 50);
        }

i变量视为时间,将结果xy视为要绘制的坐标;你会小步地走过春天的小路。

然后,您可以创建一个新的位图并在这些坐标上使用 SetPixel 方法,然后在表单的 OnPaint 方法中绘制位图它。

如果您擅长数学(我不是:P),您可能只能在位图中绘制像素 - 上面的示例并不能解决 ii.

First of all you'd need to think of a formula that would represent the spring. You could draw a circle and as you're going around it, let the X increase a bit. For instance:

        for (double i = 0; i < 50; i += 0.01)
        {
            int x = (int)(Math.Sin(i) * 10 + i * 3);
            int y =(int)(Math.Cos(i) * 10 + 50);
        }

See the i variable there as time, and the result x and y the coordinates to draw; you'd traverse the path of the spring in small steps.

You could then create a new Bitmap and use the SetPixel method on those coordinates, and in the OnPaint method of your form, draw the bitmap on it.

If you're any good with math (I'm not :P) you might be able to only plot pixels inside the bitmap - the above example doesn't solve the problem of the minimum and maximum values for i.

凹づ凸ル 2024-09-20 07:38:21

这与其说是一个 C# 问题,不如说是一个数学问题。您想要的是为您想要绘制的曲线导出参数方程

然后,按照一定的间隔和一定的步长,用参数方程的值填充 Point 对象的数组(步长越小,最终的图形看起来就越像实际的形状)。然后就可以使用g.DrawLines(MSDN: DrawLines)来绘制曲面上的实际曲线。

通过修改Pen对象的参数,可以编辑线条的宽度、颜色等属性。

您的实际代码如下所示:

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());
}

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.

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.

You can edit the width, color and other properties of the line by modifying parameters of the Pen object.

Your actual code would look like this:

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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文