【问题标题】:Why DrawArc/DrawPie angle point differs from actual Arc point for the same angle?为什么 DrawArc/DrawPie 角度点与相同角度的实际 Arc 点不同?
【发布时间】:2014-02-19 19:27:43
【问题描述】:

当我使用 C# DrawArc/DrawEllipse 或 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);
    }

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

    }

【问题讨论】:

  • 不清楚你在问什么。您是在问DrawArc 中是否有错误?或者你是在问你的代码中是否有一个错误可以找到椭圆上的点?您能否描述(或者,更好的是,展示一张图片)您的预期和会发生什么?
  • @JimMischel:我认为根据 Matthias 给出的答案,现在的问题是关于 DrawArc() 方法 - 我的代码中没有错误,但 DrawArc() 函数正在以某种方式工作与它应该做的不同。即使在MSDN中也没有这样的解释!!!因此,无论您将哪个矩形作为此函数的参数,它只会为您提供基于在该矩形中压缩的圆形的弧,而不是在视图中具有正确角度的实际椭圆。

标签: c# algorithm system.drawing angle ellipse


【解决方案1】:

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

【讨论】:

  • 非常感谢!你说的对。我确实很想知道它为什么会这样工作(设计缺陷?),但现在,至少我知道让我头脑清醒的原因:) 非常感谢。
猜你喜欢
  • 2022-12-19
  • 2022-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多