Jonathan Perry
Jonathan Perry

Reputation: 3053

Converting screen coords to world coords

I'm working on a conversion matrix for a map application I'm currently writing.

I have a point in screen coordinates and the points in the target coordinate space, and for some stupid reason I cannot seem to figure out how to find the conversion matrix the converts the points in my screen coordinates to the world coordinates.

For example:

Coordinate (1005.758, 673.661) should be converted to (786382.6, 2961730.3) and coordinate (1010.240, 665.217) should be converted to (786488.3, 2961837). I'm using WPF so that's why the screen coordinates are actually double and not int.

Upvotes: 1

Views: 1128

Answers (1)

Howard
Howard

Reputation: 39187

With two coordinates you can extract scaling factor and translation (if the map is using these two operations only).

Scaling in the first dimension is given by

s1 = (786382.6 - 786488.3) / (1005.758 - 1010.24) => s1 ~ 23.583

and in the second via

s2 = (2961730.3 - 2961837) / (673.661 - 665.217) => s2 ~ -12.636

while the corresponding offsets are

o1 = 786382.6 - s1 * 1005.758 => o1 ~ 762663.586

and

o2 = 2961730.3 - s2 * 673.661 => o2 ~ 2970242.809

Thus your transformation is given by

(x, y) -> (o1+s1*x, o2+s2*y) = (762663.586+23.583*x, 2970242.809-12.636*y)

Upvotes: 1

Related Questions