Daniel
Sapoundjiev on
Conversion of coordinates between two areas Conversion of coordinates between two areas
When representing a real object such as canvas, or another area on
the computer screen we need to have a way to map the positions.
That is necessary in different situations. What we have to do?What area has is coordinates. We need to convert coordinates from one area to the other and vice versa. Relation between the two areasThe two areas exist separately with no relation between them. When we need to have some relation between them we need to give extra information about it. This information will help us in the conversion. Therefore, how can we do the conversion?
- If we have array which maps the positions between the areas; The first way is obviously very slow and unpractical. Therefore, it's better to calculate the positions. In this case, we need less input information. One position from the first area and one from the second. These positions correspond each other. Usually there is some scale between the areas and between the measures used in different areas. Measures can be meters, inch, and pixels. Therefore, we need the scale too. Having the two positions, we can make the conversion. By calculating the offset from the corresponding positions. If we choose to give a corresponding position for the screen position (0;0) we will have even less input parameters. Just one position from the area and scale. Conversion without scaleIf we have two areas that are not scaled, the conversion is pretty simple. We just have to know the positions of two points from the different areas that correspond to each other. We can choose position (1,1) from first area and corresponding position (2,2) in second area. And to calculate all other points. But for this we need to know 4 points. If we say that the position from the second area will always be (0,0) we don't have to keep it. This way we will have to keep only the position from the first area. Also, when we talk about monitor screen as second area, we have always position (0,0) visible So, lets: Area1X and Area1Y are the coordinates of the point in Area1 that correspond to point (0,0) in Area2. To find the corresponding position of Point1(5,7) which lies in Area1 on Area2 we have the formula;
Point2X = Point1X - Area1X
Lets Area1X = 2 and Area1Y = 3
To find the corresponding position of Point2 which resides in
Area2. ScaleScale represents the ratio between the two areas. It can be Area1/Area2 or Area2/Area1. Which one to choose? Area1 can be infinite. Area2 as a monitor screen is finite. We keep information where the (0,0) position of Area2 meets the Area1. Also we keep scale how to convert positions from Area1 to Area2 and vice versa. Scale=Area1/Area2 As we say Area1X will correspond to 0. We can say Scale will correspond to 1 in Area2 The equation - converting area coordinate to screen
Let us have: Converting from screen to area
ScreenX = (AreaX - AreaPosX) * Scale \<=> Opposite directions of the coordinates in the areas
When the directions are opposite, we need just to change the sign
of the offset. |