Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to convert a coordinate from one coordinate system to another, so that I can draw it on an image.

Basically the destination coordinate system is as follows:

X range: 0 to 1066
Y range: 0 to 1600

(just a standard image I'm drawing onto with the size of 1066 x 1600)

The position I'm trying to draw onto the image actually has the exact same size, but the coordinate system is different. The span all of the coordinates is 1066x1600.

But a coordinate example would be:

(111.33f, 1408.41f)
(-212.87f, 1225.16f)

The range of this coordinate system is:

X range: -533.333 to 533.333
Y range: 533.333 to 2133.333

I feel like this is VERY simple math, but for some reason I'm not getting it.

How can I convert the coordinates provided into the first coordinate system?

share|improve this question
2  
If the two coordinate systems have the same base vectors you can simply use a scaling factor. If they don't have the same base vectors a base change is necessary. –  thalador Jul 18 '12 at 15:25
add comment

4 Answers

It is simple math:

res = ( src - src_min ) / ( src_max - src_min ) * ( res_max - res_min ) + res_min

src - source coordinate system

res - result coordination system

Edit - explanation of math

( src - src_min ) / ( src_max - src_min ) translates it to coordinate system starting at zero with equal length of source coordinate system (0.0, src_max - src_min ). Then it scales value to coordinate system (0.0, 1.0).

* ( res_max - res_min ) this scales value to coordinate system starting at zero with length of result coordination system (0.0, dst_max - dst_min)

+ res_min translates value to result coordinate system (dst_min, dst_max)

share|improve this answer
    
I thought this as well, but it's not displaying properly: dump.tanaris4.com/sota.png The final coordinate should hit where the white circle is at the bottom –  Geesu Jul 18 '12 at 15:37
1  
This doesn't explain them math at all, why not? :) –  Byte56 Jul 18 '12 at 15:38
    
@Byte56 For me is formula enough to understand something especially if it uses only arithmetic operations, but I've added explanation for people that would need it :) –  Miro Jul 18 '12 at 15:51
1  
@Geesu Then your are probably doing something else bad (rendering matrices? ). –  Miro Jul 18 '12 at 15:53
1  
Thanks for updating that. I generally think it's better to give an answer that tries to explain the why. Otherwise you're just giving the answer to this question, instead of how to solve this question and similar problems. It's a "give a man a fish, teach a man to fish" type thing. –  Byte56 Jul 18 '12 at 19:54
add comment

You can normalize the first value, this will give you a value in the [0,1] range. You can think of that as X percentage, the percentage the value maps to between the minimum and maximum values. Then you can find where that percentage belongs in your destination coordinate system by seeing what value is X percentage through the destination system. I'll use Java code as an example language, I'm sure the concepts are clear enough to translate to any language though.

So normalize:

public static float normalize(float value, float min, float max) {
    return Math.abs((value - min) / (max - min));
}

Using your example you'd input:

xPercent = normalize(x,0,1066);

Then find where it lays in the destination system. With something like

destX = xPercent*(Math.abs(max-min)) + min;

Or to use your values:

destX = xPercent*(Math.abs(533.33--533.33)) + -533.33;

So for example with an x value of 1000 you'd map that to your destination coordinate system to 467.29.

Alternatively, if the coordinate systems will always be the same, you can pre-compute the ratio between them.

So:

xRatio = (Math.abs(srcMax-srcMin))/(Math.abs(destMax-destMin));

destX = x*xRatio+destMin;
share|improve this answer
    
Why Java(C#)? He didn't ask for Java code :) –  Miro Jul 18 '12 at 15:27
6  
It's to give an example. It doesn't need to be used as is, and the concept is clear enough. –  Byte56 Jul 18 '12 at 15:28
    
But if I do this: destX = xPercent*(Math.abs(533.33--533.33)) + -533.33; I always get a negative value, and the result coordinate system is just from 0-1066, should i switch all the coordinates around? –  Geesu Jul 18 '12 at 15:49
    
Switched them around and I'm still getting dump.tanaris4.com/sota.png just like in another post, maybe I'm having another issue altogether (related to C# and drawing). Thanks guys! –  Geesu Jul 18 '12 at 15:50
    
Got it, for some reason I had to do xPercent = 1.0f - xPercent –  Geesu Jul 18 '12 at 16:31
show 1 more comment

The basic equation for 2D coordinate tranformation (in algebra, without rotation involved) is:

TargetCoordinate = TranslateFactor + ScalingFactor*SourceCoordinate

given two points in TargetCoordinate (T1, T2) that corresponds to two points in SourceCoordinate(S1, S2), TranslateFactor and ScalingFactor is given by solving :

T1 = TranslateFactor + ScalingFactor*S1
T2 = TranslateFactor + ScalingFactor*S2

which result :

TranslateFactor = (T2*S1 - T1*S2) / (S1 - S2)
ScalingFactor   = (T2 - T1) / (S2 - S1)

In your case, for the x coordinate

S1 = 0    -> T1 = -533.333
S2 = 1066 -> T2 = 53.333

And thus,

TranslateFactor = -533.333
ScalingFactor   = 1.000625
=> TargetCoordinate = (-533.333) + (1.000625)*SourceCoordinate

y coordinate follow the same procedure

share|improve this answer
add comment

Making a few assumptions:

  • You are (eventually) interested in a matrix impementation, for convenience and power; and
  • You are familiar with homogeneous coordinates.

Then the question migrates to: What is the homgeneous transformation matrix for my basis change?

To answer this we need the answers to three subsidiary quesions first:

  1. Where did my origin move to?
  2. What happened to my X-axis? Let (M11,M12) be the coordinates of the point
  3. What happened to my Y-axis?

Define the answers to these thre questions to be as follows:

  1. (M31,M32) are the coordinates of the new origin under the original coordinate system.
  2. (M11,M12) are the coordinates of the new unit x-vector in the original coordinate system.
  3. (M21,M22) are the coordinates of the new unit y-vector in the original coordinate system.

Then the homogenous transformation matrix is:

( M11, M12,  0 )
( M21, M22,  0 )
( M31, M32,  1 )

My convention here is that points are represented by row vectors, which is the normal computer graphics convention; the mathematicians and physicists often use the oppsoite.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.