Transforming coordinates of one rectangle to another rectangle

和自甴很熟 提交于 2019-12-05 09:01:37
Matthew Watson

If:

Rectangle 1 has (x1, y1) origin and (w1, h1) for width and height, and
Rectangle 2 has (x2, y2) origin and (w2, h2) for width and height, then

Given point (x, y) in terms of Rectangle 1 coords, to convert it to Rectangle 2 coords:

xNew = ((x-x1)/w1)*w2 + x2;
yNew = ((y-y1)/h1)*h2 + y2;

Do the calculation in floating point and convert back to integer after, to avoid possible overflow.


In C#, the above would look something like:

PointF TransformPoint(RectangleF source, RectangleF destination, PointF point)
{
    return new PointF(
        ((point.X - source.X) / source.Width) * destination.Width + destination.X,
        ((point.Y - source.Y) / source.Height) * destination.Height + destination.Y);
}

Try using this:

rectangelOne.x = rectangelTwo.x + rectangelTwo.width / 2;
rectangelOne.y = rectangelTwo.y + rectangleTwo.height / 2;

if your rectangleOne's pivot is correctly it should center automatically, otherwhise add this:

rectangleOne.x -= rectangleOne.width / 2;
rectangleTwo.y -= rectangleOne.height / 2;

hope this helps.

float transformValue (float x,float  A,float B, float C, float D){

    return C + ((x-A) * (D-C)/(B-A));

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