为什么DrawArc / DrawPie角点与实际弧点的角度相同?

时间:2014-01-28 13:17:11

标签: c# algorithm system.drawing angle ellipse

当我使用C#DrawArc / DrawEllipse或DrawPie GDI函数绘制椭圆或圆弧时,我相信它绘制的是我给出的精确角度。但是,当我通过编写一个小程序对它进行测试时,我发现DrawArc中的225度扫描角度实际上不是225度。我的测试程序每秒绘制一条从0度到360度的线(如时钟秒针)并且使用DrawArc函数并行绘制弧线以获得相同的角度。

下面的函数用于获取给定起点/终点和角度的角度点。有人可以解释为什么这种差异?我试图通过DrawArc()找到绘制弧的终点。我可以用不同的方式实现它。但是,我不明白为什么DrawArc函数以这种方式工作? DrawArc可以使用0,90,180,270,360个角度。

public static Point PointOnEllipseFromAngle(Point center, int radiusX, int radiusY, int angle)
    {
        double x = center.X + radiusX * Math.Cos(angle * (Math.PI / 180.0));
        double y = center.Y + radiusY * Math.Sin(angle * (Math.PI / 180.0));
        return new Point((int)x, (int)y);
    }

Form Paint就像这样

private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Rectangle rect = Bounds;
        rect.Inflate(-50, -50);
        // Mid point
        Point mid = new Point(rect.Left+(rect.Width / 2), rect.Top+(rect.Height / 2));

        // Arc point for the given angle (angle is incremented in timer every second)
        Point p1 = PointOnEllipseFromAngle(new Point(rect.Left+(rect.Width / 2), rect.Top+(rect.Height / 2)), rect.Width / 2, rect.Height / 2, angle); 

        // Line between mid and arc point
        e.Graphics.DrawLine(new Pen(Color.Blue, 2), mid, p1);
        e.Graphics.DrawString(angle.ToString(), new Font("Arial", 18), new SolidBrush(Color.Red), p1);
        e.Graphics.FillEllipse(new SolidBrush(Color.Red), new Rectangle(p1.X - 5, p1.Y - 5, 10, 10)); // red circle at edge of the line

        // DrawArc for the same angle
        e.Graphics.DrawArc(new Pen(Color.Green,2), rect, 0, angle);

        // Just Drawing axis lines (horizontal, vertical, diagonal)
        e.Graphics.DrawLine(new Pen(Color.Black, 2), mid, new Point(rect.Left+rect.Width,rect.Top+(rect.Height/2)));
        e.Graphics.DrawLine(new Pen(Color.Black, 2), mid, new Point(rect.Left, rect.Top + (rect.Height / 2)));
        e.Graphics.DrawLine(new Pen(Color.Black, 2), mid, new Point(rect.Left + (rect.Width/2), rect.Top + rect.Height));
        e.Graphics.DrawLine(new Pen(Color.Black, 2), mid, new Point(rect.Left + (rect.Width / 2), rect.Top));
        e.Graphics.DrawLine(new Pen(Color.Black, 2), rect.Left,rect.Top,rect.Right,rect.Bottom);
        e.Graphics.DrawLine(new Pen(Color.Black, 2), rect.Left, rect.Bottom, rect.Right, rect.Top);

    }

1 个答案:

答案 0 :(得分:1)

不知何故,DrawArc根据具有规则半径的弧计算角度/绘图,因此需要较长的一侧。我无法找到解释为什么它以这种方式工作,但也许this link有帮助。

45 degrees on regular arc

        var radiusX = rect.Width/2;
        var radiusY = rect.Height/2;
        // Mid point
        var mid = new Point(rect.Left + (rect.Width/2), rect.Top + (rect.Height/2));

        int larger = Math.Max(radiusX, radiusY);

        // Arc point for the given angle (angle is incremented in timer every second)
        Point p1 = PointOnEllipseFromAngle(new Point(rect.Left + radiusX, rect.Top + radiusY), larger, larger, angle);
        //Point p1 = PointOnEllipseFromAngle(new Point(rect.Left + radiusX, rect.Top + radiusY), radiusX, radiusY, angle);