6
votes

enter image description here

in the above image I have shown two rectangles

  • rectangle 1 whose x can vary from -900 to 13700 and Y can vary from -600 to 6458
  • rectangle 2 whose coordinate X can vary from 0 to 3000 and y can vary from 0 to 2000

Also: rectangle 2 has its starting point at left top position(0,0) whereas rectangle 1 has starting point( width/2, height/2).

What I need to do: to convert a point of rectangle 1 to point of rectangle 2 using scaling or translation.

So, what should be scaling factor for x and y coordinates in order to transform the coordinate of rectangle 1 to rectangle 2?

3
Does the new point be on the 'same' spot within the next rectangle?Jeroen van Langen
yes it would be on the same placeYogesh
Even anyhow I want relative positioning among the pointsYogesh
duplicate with answer: stackoverflow.com/questions/20536770/…MBo
Rectangle1 can range from -900 to 13700 but your origin is (width/2,height/2) you say?? This kind of suggests you are using 2 separate coordinate systems in the SAME rectangle. Your points inside the rectangle are specified from a different origin to the rectangle corners??mathematician1975

3 Answers

8
votes

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);
}
0
votes

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.

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

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

}