开发者

Lines rotation problem

开发者 https://www.devze.com 2023-04-04 01:10 出处:网络
I have 2 lines that i draw like this: float Alpha = RotDegrees; PointF PitCenter = new Point(picBoxZoomMap.Width / 2, picBoxZoomMap.Height / 2);

I have 2 lines that i draw like this:

float Alpha = RotDegrees;
PointF PitCenter = new Point(picBoxZoomMap.Width / 2, picBoxZoomMap.Height / 2);
PointF p = new PointF(PitCenter.X - 20, PitCenter.Y - 250);
PointF p2 = new PointF(PitCenter.X + 20, PitCenter.Y - 250);

zoomgfx.DrawLine(Pens.Red, PitCenter, new PointF(
    (float)((p.Y - PitCenter.Y) * Math.Sin(Alph开发者_JAVA技巧a * Math.PI / 180) + p.X),
    (float)(PitCenter.Y + (p.Y - PitCenter.Y) * Math.Cos(Alpha * Math.PI / 180))));

zoomgfx.DrawLine(Pens.Red, PitCenter, new PointF(
    (float)((p2.Y - PitCenter.Y) * Math.Sin(Alpha * Math.PI / 180) + p2.X),
    (float)(PitCenter.Y + (p2.Y - PitCenter.Y) * Math.Cos(Alpha * Math.PI / 180))));

Here are the lines when Alpha = 0;

Lines rotation problem

And here are the lines after 90 degrees rotation..

Lines rotation problem

As you see the lines somehow meets.. i really cant understand why.. Any ideas?


Your formula for the rotation is incorrect, have a look here --> Rotate a point by another point in 2D

Change your code to this, and you will get the right effect:

PointF PitCenter = new Point(picBoxZoomMap.Width / 2, picBoxZoomMap.Height / 2);
PointF p = new PointF(PitCenter.X - 20, PitCenter.Y - 250);
PointF p2 = new PointF(PitCenter.X + 20, PitCenter.Y - 250);

var AlphaRad = RotDegrees * Math.PI / 180;

zoomgfx.DrawLine(Pens.Red, PitCenter, new PointF(
(float)(Math.Cos(AlphaRad) * (p.X - PitCenter.X) - Math.Sin(AlphaRad) * (p.Y - PitCenter.Y) + PitCenter.X),
(float)(Math.Sin(AlphaRad) * (p.X - PitCenter.X) + Math.Cos(AlphaRad) * (p.Y - PitCenter.Y) + PitCenter.Y)));

zoomgfx.DrawLine(Pens.Red, PitCenter, new PointF(
(float)(Math.Cos(AlphaRad) * (p2.X - PitCenter.X) - Math.Sin(AlphaRad) * (p2.Y - PitCenter.Y) + PitCenter.X),
(float)(Math.Sin(AlphaRad) * (p2.X - PitCenter.X) + Math.Cos(AlphaRad) * (p2.Y - PitCenter.Y) + PitCenter.Y)));


You take the difference between the Y-coordinates and multiply with Sin to get the X-coordinates. This is correct.

However, you also take the difference between the Y-coordinates when you construct the new Y-coordinates. For this, you should take the difference between the X-coordinates and multiply by Cos.

E.g. your line 8 and line 12 produces the same Y-coordinates for the new point because p.Y - PitCenter.Y is the same as p2.Y - PitCenter.Y since p.Y = p2.Y.

Makes sense?


I find it easier to understand things if I break it down a bit more. It looks to me like you're trying to do too many things all at once. I think this will do what you're looking for (I'm not sure if the radius definition is exactly what you want) but it's hopefully clear enough that you understand what I'm suggesting.

float RotDegrees = 90.0;              // Centerline angle for wedge
float width = 10.0;                   // Assume a 10 degree wedge

// Center of view
PointF PitCenter = new PointF(picBoxZoomMap.Width / 2,
                              picBoxZoomMap.Height / 2);

// Determine the angle for the wedges in radians
float theta0 = (RotDegrees - width / 2.0) * Math.PI / 180.0;
float theta1 = (RotDegrees + width / 2.0) * Math.PI / 180.0;

// May need to adjust this to satisfy your needs 
float radius = 100.0;

// Determine the endpoints of the new wedge ... Assumes (0,0) is in the upper
// left corner rather than the lower left (where it belongs ;).  If it's in the
// lower left after all, change the subtraction in the Y components to an
// addition
PointF p0 = new PointF( PitCenter.X + radius * Math.Cos(theta0),
                        PitCenter.Y - radius * Math.Sin(theta0) );

PointF p1 = new PointF( PitCenter.X + radius * Math.Cos(theta1),
                        PitCenter.Y - radius * Math.Sin(theta1));

// Draw the lines
zoomgfx.DrawLine(Pens.Red, PitCenter, p0);
zoomgfx.DrawLine(Pens.Red, PitCenter, p1);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号