问题
private void panel1_Paint(object sender, PaintEventArgs e)
{
Pen myPen = new Pen(Color.Blue, 1);
Pen myPen2 = new Pen(Color.Red, 1);
Point[] array = { new Point(639, 75), new Point(606, 124), new oint(664, 123) };
matrix.TransformPoints(array);
e.Graphics.Transform = matrix;
e.Graphics.RotateTransform(ang, MatrixOrder.Append);
myPen2.RotateTransform(ang, MatrixOrder.Append);
e.Graphics.DrawPolygon(myPen2, array);
}
I'm using Visual Studio 2010. The above code should be for drawing a triangle in C#, but I can't rotate it without drawn triangle location. How can I achieve that?
回答1:
This will do the rotation:
private void panel1_Paint(object sender, PaintEventArgs e)
{
Pen myPen = new Pen(Color.Blue, 1);
Pen myPen2 = new Pen(Color.Red, 1);
Point[] array = { new Point(239, 75), new Point(206, 124), new Point(264, 123) };
float x = array.Select(_ => _.X).Sum() / array.Length;
float y = array.Select(_ => _.Y).Sum() / array.Length;
e.Graphics.DrawPolygon(myPen, array);
e.Graphics.TranslateTransform(x,y);
e.Graphics.RotateTransform(ang);
e.Graphics.TranslateTransform(-x, -y);
e.Graphics.DrawPolygon(myPen2, array);
}
No need to rotate a simple Pen although for more adavanced pens this may well be called for..
I have calculated the rotation center coordinates and then move the Graphics'
origin there, rotate it and then move back. Then we can draw.
To test you can use a trackbar:
float ang = 0f;
private void trackBar1_Scroll(object sender, EventArgs e)
{
ang = (float) trackBar1.Value;
panel1.Invalidate();
}
回答2:
You are using multiple methods mixed up. Don't rotate the pen/e.Graphics/matrix all together. When you call e.Graphics.RotateTransform
you are manipulating the e.Graphics.Transform
You need to setup the matrix and assign it to the transform. In your case, your triangle has world coordinates, so you need to translate them before you can rotate them.
This will help:
public partial class Form1 : Form
{
// define triangle points ('world coords :'-( ?????????
private Point[] _triangle = { new Point(639, 75), new Point(606, 124), new Point(664, 123) };
// cache the angle
private float _angle = 0;
// cache the GObjects!!
private Pen _myPen = new Pen(Color.Blue, 1);
private Pen _myPen2 = new Pen(Color.Red, 1);
// The rotation origin position
private Point _origin = new Point(637, 110);
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
// draw the origin triangle
e.Graphics.DrawPolygon(_myPen, _triangle);
// create a matrix
Matrix matrix = new Matrix();
// first translate the triangle coordinates with the negative origin coords (translate coords to origin coordinate system)
// This translation is only needed, because your triangle is in world coordinates..
matrix.Translate(-_origin.X, -_origin.Y, MatrixOrder.Append);
// do the rotation..
matrix.Rotate(_angle, MatrixOrder.Append);
// translate the triangle coordinates back to the 'world' coordinate system
matrix.Translate(_origin.X, _origin.Y, MatrixOrder.Append);
// assign the matrix to the Graphics
e.Graphics.Transform = matrix;
// Draw the polygon
e.Graphics.DrawPolygon(_myPen2, _triangle);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
float.TryParse(textBox1.Text, NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture.NumberFormat, out _angle);
panel1.Refresh();
}
}
I would define the triangle with it's own origin, this way you can draw/rotate it anywhere on the panel.
来源:https://stackoverflow.com/questions/36035558/degree-rotation-on-triangle-without-changing-position-using-c-sharp-windows-appl