I am trying to translate a co-ordinate from one rectangle to another. In realty, the second rectangle is the first rectangle "scaled-up".
I am using the following method:
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 co-ords, to convert it to
Rectangle 2 co-ords:
xNew = ((x-x1)/w1)*w2 + x2;
yNew = ((y-y1)/h1)*h2 +y2;
In code, this simply looks like that:
float floatX = ((((point.X - source.X) / source.Width) * destination.Width + destination.X));
float floatY = ((((point.Y - source.Y) / source.Height) * destination.Height + destination.Y));
However, I don't think this is correct.
Example 1:
Rectangle1 x=9, y=9, Width= 310 Height = 190
Rectangle2 x=0, y=0, Width= 800 Height = 800
Given Point 799,799 in Rectangle 2, I get 318.6125, 198.7625
Example 2:
Rectangle1 x=9, y=9, Width= 2 Height = 2
Rectangle2 x=0, y=0, Width= 800 Height = 800
Given Point 300,300 in Rectangle 2, I get 9.75, 9.75
In Example 1 if I Round Up I get the desired result. In Example 2 if I Round Down I get the desired result.
How do I get one size fits all solution? Do I need to apply a scaling factor to get the correct Rounding logic?
Probably the best is to round to nearest in all cases, but there are problematic cases. In your example 2, you only have the choice to output $9,10,$ or $11$ in each direction. If you round to nearest, inputs of $0-200$ will go to $9$, $200-600$ will go to $10$ and $600-800$ will go to $11$, which means that half the inputs go to the middle. The conflicts at $200,600$ will be determined by how you round an exact $0.5$. This is an extreme case because the destination is so small. If you go the other direction in example 2, you can only output $0, 400$ or $800$. There is no rounding.