绘制极性曲线Lemiscate bug

时间:2012-06-10 14:36:54

标签: c# curve polar-coordinates

我正在尝试绘制lemiscate。我的代码没有正确绘制曲线。为什么线在点0,0(左角)开始或结束?

enter image description here

private void drawLemiscate(Graphics g, int a, int Sx,int Sy) 
{
    int x, y;


    Point[] p = new Point[720];
    for (int phi = 0; phi < 720; phi++)
    {
        int r = (int)(a * Math.Cos(2 * degreeToRadians(phi)));
        if (r > 0)
            continue;
        x = (int)Math.Round((r * Math.Sin(degreeToRadians(phi)) + Sx));
        y = (int)Math.Round((r * Math.Cos(degreeToRadians(phi)) + Sy));
        p[phi] = new Point(x, y);
    }
    Pen pen = new Pen(Color.Red, 1);
    g.DrawLines(pen, p);
    canvas.Invalidate();
}

private double degreeToRadians(double angle)
{
    return Math.PI * angle / 180.0;
}

1 个答案:

答案 0 :(得分:1)

您应该调查p的哪个元素是(0,0)。您可以使用调试器检查它或使用循环查找它并打印出哪些索引(0,0)。

提示:考虑if (r > 0) continue;对输出的影响。