当我使用C#draw Arc/drawlipse或DrawPie-GDI函数绘制椭圆或圆弧时,我相信它会按照我给出的确切角度绘制但是,当我通过编写一个小程序对其进行测试时,我发现DrawArc中225度的扫描角实际上不是225度。我的测试程序每秒画一条从0度到360度的线(就像时钟秒针),并在相同角度下使用drawarc函数并行绘制弧线。
下面的函数用于获取给定起点/终点和角度上的点有人能解释一下为什么会有这种不同吗?我试图通过DrawArc()找到绘制的弧的终点我可以用不同的方式实现它但是,我不明白为什么DrawArc函数是这样工作的0、90、180、270、360度的角度都可以使用DrawArc。

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

油漆的形状是这样的
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);

    }

最佳答案

以某种方式,drawarc根据具有规则半径的圆弧计算角度/绘制,因此它需要较长的边。我找不到解释为什么会这样,但也许this link会有帮助。

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

08-15 22:15