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.
- When drawing an object on a specific position.
- When a ruler control shows area coordinates on the screen.
- When the mouse points on the screen, we need to check if there is some object there in the area.

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 areas

The 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;
- Or we can have just one position mapping and then calculate every other position.

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 scale

If 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
Point2Y = Point1Y - Area1Y

Lets Area1X = 2 and Area1Y = 3
Point1 = (3, 5)
We want to find the position of Point2 which lies in Area2
Point2X = Point1X - Area1X
Point2X = 3 - 2
Point2X = 1

The same is for Point1Y
Point2Y = Point1Y - Area1Y
Point2Y = 5 - 3
Point2Y = 2

Point2 = (1, 2)

To find the corresponding position of Point2 which resides in Area2.
Point1X = Point2X + Area1X
Point1Y = Point2Y + Area1Y


Scale 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.


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:

AreaPosX - the x coordinate of the position that corresponds screen 0
ScreenPosX - the x coordinate of the screen position

Scale - the scale between the areas.

AreaX - the coordinate for which we search corresponding
ScreenX - the searched coordinate on the screen.

ScreenX - ScreenPosX = (AreaX - AreaPosX) * Scale \<=>
ScreenX = ScreenPosX + (AreaX - AreaPosX) * Scale

But ScreenPosX = 0 in our case so
ScreenX = (AreaX - AreaPosX) * Scale

Converting from screen to area

ScreenX = (AreaX - AreaPosX) * Scale \<=>
ScreenX/Scale = AreaX - AreaPosX \<=>
AreaX = AreaPosX + ScreenX/Scale

Opposite directions of the coordinates in the areas

When the directions are opposite, we need just to change the sign of the offset.

For converting from area to screen
ScreenX = - (AreaX - AreaPosX) * Scale \<=>
ScreenX = (AreaPosX - AreaX) * Scale \<=>

For converting from area to screen
AreaX = AreaPosX - ScreenX/Scale

Back to main menu