GraphicsPath.AddArc如何使用startAngle和sweepAngle参数?

发布于 2024-08-02 16:00:43 字数 1910 浏览 5 评论 0原文

我正在尝试使用 System.Drawing.Drawing2D.GraphicsPath.AddArc 绘制从 0 度开始并扫描到 135 度的椭圆弧。

我遇到的问题是,对于椭圆形,绘制的圆弧与我的预期不匹配。

例如,以下代码生成下面的图像。绿色圆圈是我期望弧的端点使用沿着椭圆的点的公式的地方。我的公式适用于圆形,但不适用于椭圆。

这与极坐标和笛卡尔坐标有关系吗?

    private PointF GetPointOnEllipse(RectangleF bounds, float angleInDegrees)
    {
        float a = bounds.Width / 2.0F;
        float b = bounds.Height / 2.0F;

        float angleInRadians = (float)(Math.PI * angleInDegrees / 180.0F);

        float x = (float)(( bounds.X + a ) + a * Math.Cos(angleInRadians));
        float y = (float)(( bounds.Y + b ) + b * Math.Sin(angleInRadians));

        return new PointF(x, y);
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Rectangle circleBounds = new Rectangle(250, 100, 500, 500);
        e.Graphics.DrawRectangle(Pens.Red, circleBounds);

        System.Drawing.Drawing2D.GraphicsPath circularPath = new System.Drawing.Drawing2D.GraphicsPath();
        circularPath.AddArc(circleBounds, 0.0F, 135.0F);
        e.Graphics.DrawPath(Pens.Red, circularPath);

        PointF circlePoint = GetPointOnEllipse(circleBounds, 135.0F);
        e.Graphics.DrawEllipse(Pens.Green, new RectangleF(circlePoint.X - 5, circlePoint.Y - 5, 10, 10));

        Rectangle ellipseBounds = new Rectangle(50, 100, 900, 500);
        e.Graphics.DrawRectangle(Pens.Blue, ellipseBounds);

        System.Drawing.Drawing2D.GraphicsPath ellipticalPath = new System.Drawing.Drawing2D.GraphicsPath();
        ellipticalPath.AddArc(ellipseBounds, 0.0F, 135.0F);
        e.Graphics.DrawPath(Pens.Blue, ellipticalPath);

        PointF ellipsePoint = GetPointOnEllipse(ellipseBounds, 135.0F);
        e.Graphics.DrawEllipse(Pens.Green, new RectangleF(ellipsePoint.X - 5, ellipsePoint.Y - 5, 10, 10));
    }

替代文字

I am trying to use System.Drawing.Drawing2D.GraphicsPath.AddArc to draw an arc of an ellipse starting at 0 degrees and sweeping to 135 degrees.

The issue I am running in to is that for an ellipse, the arc drawn does not match up with what I would expect.

For example, the following code generates the image below. The green circles are where I would expect the end points of the arc to be using the formula for a point along an ellipse. My formula works for circles but not for ellipses.

Does this have something to do with polar versus Cartesian coordinates?

    private PointF GetPointOnEllipse(RectangleF bounds, float angleInDegrees)
    {
        float a = bounds.Width / 2.0F;
        float b = bounds.Height / 2.0F;

        float angleInRadians = (float)(Math.PI * angleInDegrees / 180.0F);

        float x = (float)(( bounds.X + a ) + a * Math.Cos(angleInRadians));
        float y = (float)(( bounds.Y + b ) + b * Math.Sin(angleInRadians));

        return new PointF(x, y);
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Rectangle circleBounds = new Rectangle(250, 100, 500, 500);
        e.Graphics.DrawRectangle(Pens.Red, circleBounds);

        System.Drawing.Drawing2D.GraphicsPath circularPath = new System.Drawing.Drawing2D.GraphicsPath();
        circularPath.AddArc(circleBounds, 0.0F, 135.0F);
        e.Graphics.DrawPath(Pens.Red, circularPath);

        PointF circlePoint = GetPointOnEllipse(circleBounds, 135.0F);
        e.Graphics.DrawEllipse(Pens.Green, new RectangleF(circlePoint.X - 5, circlePoint.Y - 5, 10, 10));

        Rectangle ellipseBounds = new Rectangle(50, 100, 900, 500);
        e.Graphics.DrawRectangle(Pens.Blue, ellipseBounds);

        System.Drawing.Drawing2D.GraphicsPath ellipticalPath = new System.Drawing.Drawing2D.GraphicsPath();
        ellipticalPath.AddArc(ellipseBounds, 0.0F, 135.0F);
        e.Graphics.DrawPath(Pens.Blue, ellipticalPath);

        PointF ellipsePoint = GetPointOnEllipse(ellipseBounds, 135.0F);
        e.Graphics.DrawEllipse(Pens.Green, new RectangleF(ellipsePoint.X - 5, ellipsePoint.Y - 5, 10, 10));
    }

alt text

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

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

发布评论

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

评论(2

蓝色星空 2024-08-09 16:00:43

输入图像描述这里我对 GraphicsPath.AddArc 的工作原理感到困惑由于找不到合适的图,所以就画了一张。以防万一其他人也遭受类似的痛苦! https://i.sstatic.net/50HBn.jpg

enter image description hereI was getting confused about how GraphicsPath.AddArc worked & I couldn't find any decent diagrams, so I drew one. Just in case anyone else has been suffering similarly! https://i.sstatic.net/50HBn.jpg

十六岁半 2024-08-09 16:00:43

GraphicsPath.AddArc 完全按照您的要求进行操作 - 它将弧线延伸到从椭圆中心投影的直线,与 x 轴顺时针精确地成 135 度角。

不幸的是,当您使用角度作为要绘制的饼图切片的直接比例时,这没有帮助。要找出需要与 AddArc 一起使用的角度 B,给定一个作用于圆的角度 A(以弧度为单位),请使用:

B = Math.Atan2(sin(A) * height / width, cos(A))

其中 宽度高度 是椭圆。

在示例代码中,尝试在 Form1_Paint 末尾添加以下内容:

ellipticalPath = new System.Drawing.Drawing2D.GraphicsPath();
ellipticalPath.AddArc(
    ellipseBounds,
    0.0F,
    (float) (180.0 / Math.PI * Math.Atan2(
        Math.Sin(135.0 * Math.PI / 180.0) * ellipseBounds.Height / ellipseBounds.Width,
        Math.Cos(135.0 * Math.PI / 180.0))));
e.Graphics.DrawPath(Pens.Black, ellipticalPath);

结果应如下所示:
替代文本 http://img216.imageshack.us/img216/1905/arcs.png< /a>

GraphicsPath.AddArc does exactly what you ask it to do -- it the arc up to a line projecting from the ellipse center, at an exact angle of 135 degrees clockwise from the x axis.

Unfortunately, this doesn't help when you're using the angle as a direct proportion of a pie chart slice you want to draw. To find out the angle B you need to use with AddArc, given an angle A that works on a circle, in radians, use:

B = Math.Atan2(sin(A) * height / width, cos(A))

Where width and height are those of the ellipse.

In your sample code, try adding the following at the end of Form1_Paint:

ellipticalPath = new System.Drawing.Drawing2D.GraphicsPath();
ellipticalPath.AddArc(
    ellipseBounds,
    0.0F,
    (float) (180.0 / Math.PI * Math.Atan2(
        Math.Sin(135.0 * Math.PI / 180.0) * ellipseBounds.Height / ellipseBounds.Width,
        Math.Cos(135.0 * Math.PI / 180.0))));
e.Graphics.DrawPath(Pens.Black, ellipticalPath);

The result should look as follows:
alt text http://img216.imageshack.us/img216/1905/arcs.png

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