7

描画が少し複雑になると、それを呼び出すことになります。行列と図形の回転の描画に関しては、私の数学は少し錆びています。ここに少しのコードがあります:

private void Form1_Paint(object sender, PaintEventArgs e)
    {
        g = e.Graphics;
        g.SmoothingMode = SmoothingMode.HighQuality;
        DoRotation(e);
        g.DrawRectangle(new Pen(Color.Black), r1);
        g.DrawRectangle(new Pen(Color.Black), r2);

        // draw a line (PEN, CenterOfObject(X, Y), endpoint(X,Y) )
        g.DrawLine(new Pen(Color.Black), new Point((r1.X + 50), (r1.Y + 75)), new Point((/*r1.X + */50), (/*r1.Y - */25)));

        this.lblPoint.Text = "X-pos: " + r1.X + " Y-pos: " + r1.Y;

        //this.Invalidate();
    }
    public void DoRotation(PaintEventArgs e)
    {
        // move the rotation point to the center of object
        e.Graphics.TranslateTransform((r1.X + 50), (r1.Y + 75));
        //rotate
        e.Graphics.RotateTransform((float)rotAngle);
        //move back to the top left corner of the object
        e.Graphics.TranslateTransform(-(r1.X + 50), -(r1.Y + 75));
    }
    public void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        case Keys.T:
                rotAngle += 1.0f;
    }

私が回転すると(私が思うにr1であるはずです)、r1とr2の両方が回転します。図形を追加するときに、各図形を個別に回転できるようにする必要があります。

4

1 に答える 1

27

私はこれに似た関数を使用します:

public void RotateRectangle(Graphics g, Rectangle r, float angle) {
  using (Matrix m = new Matrix()) {
    m.RotateAt(angle, new PointF(r.Left + (r.Width / 2),
                              r.Top + (r.Height / 2)));
    g.Transform = m;
    g.DrawRectangle(Pens.Black, r);
    g.ResetTransform();
  }
}

マトリックスを使用して、各長方形の中央にある特定のポイントで回転を実行します。

次に、ペイントメソッドで、それを使用して長方形を描画します。

g.SmoothingMode = SmoothingMode.HighQuality;
//g.DrawRectangle(new Pen(Color.Black), r1);
//DoRotation(e);
//g.DrawRectangle(new Pen(Color.Black), r2);

RotateRectangle(g, r1, 45);
RotateRectangle(g, r2, 65);

また、2つの長方形を結ぶ線は次のとおりです。

g.DrawLine(Pens.Black, new Point(r1.Left + r1.Width / 2, r1.Top + r1.Height / 2),
                       new Point(r2.Left + r2.Width / 2, r2.Top + r2.Height / 2));

これらの設定の使用:

private Rectangle r1 = new Rectangle(100, 60, 32, 32);
private Rectangle r2 = new Rectangle(160, 100, 32, 32);

をもたらしました:

ここに画像の説明を入力してください

于 2012-04-18T13:40:36.810 に答える