5

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

代替テキスト

4

2 に答える 2

5

ここに画像の説明を入力GraphicsPath.AddArc がどのように機能するかについて混乱していて、まともな図が見つからなかったので、1つ描きました。他の誰かが同じように苦しんでいる場合に備えて!http://imgur.com/lNBewKZ

于 2016-11-02T14:00:44.787 に答える
2

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

于 2009-08-20T22:25:33.430 に答える