Math (in WPF): Getting new x,y coordinates after a translation

馋奶兔 提交于 2019-12-05 11:50:33

Assuming you're rotating clockwise from 12 o'clock, your new x-coordinate will be:

sin(130) * 200 = 153 + original x-coordinate

And your new y-coordinate will be

cos(130) * 200 = -129 + original y-coordinate (assuming negative 'y' is down)

As below, note that in C#, for example, sin and cos take radians, not degrees - multiply by Math.PI/180 to get the value in radians first.

You can use cos and sin to work out the amount of movement in the x and y coordinates. This is based on the unit circle.

The unit circle has angles starting from the right side, going anti-clockwise. So in the unit circle your angle of 130 degrees is actually 320 degrees.

actualAngle = 90 - 130 = -40 
(which is the same as 320 degrees when used in sin/cos)

The other thing to note is that the cos and sin functions are using radians instead of degrees.

angleInRadians = 320 * pi / 180 (about 5.59)
x = cos(angleInRadians) * 200 + 300 (about 453.21)
y = sin(angleInRadians) * 200 + 400 (about 271.44)

look at the TransformToVisual if you have the coordinate of your object from Visual A origin and you want the coordinates of your object from Visual B origin, then you do

   var transformation = A.TransformToVisual(B);

you can cast transformation from GeneralTransform to Transform if needed. Then you can use Transform method of your GeneralTransform object.

I will detail how you can do without using Mathematics :

Note that, I don't try what I'm saying, so maybe there is some mistakes.

I will call the square you are moving Visual B,

I will call the container of the square Visual A.

Now, you want to be able to do a translation of B from B origin to B', and retrieve B' coordinate from A origin.

If your translation is (200,0), then the coordinate of B' from B origin is (200,0). You want these coordinates from A origin. So you have to do.

var transform = B.TransformToVisual(A);
var pointFromAOrigin  = transform.Transform(new Point(200,0));

pointFromAOrigin reflect what you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!