My Math Forum

My Math Forum (http://mymathforum.com/math-forums.php)
-   Linear Algebra (http://mymathforum.com/linear-algebra/)
-   -   Coordinate conversion (http://mymathforum.com/linear-algebra/12867-coordinate-conversion.html)

DoodleGal May 14th, 2010 11:11 AM

Coordinate conversion
 
Being new here, I wasn't sure where to post this, especially because I don't know what area of math the answer will lay in.

Anyway, I am a 3D computer graphics designer with a programming problem that needs a mathematical solution.

Basically, the problem I have is that I have a 2D planar object that is mapped in one coordinate system and needs to be converted to another.

The exact numbers that are being dealt with can vary, because the shape will vary, but the new coordinate system is always the same. The new system can only have values between 0 and one.

I will be dealing with the coordinates as (x,z) points, so that means that the target planar map will have x values from 0 to 1 and z values from 0 to 1.

What I am doing is looping through all existing vertices, finding the min and max values of x and z. Then I subtract the min from the max to see if x or z contains the largest span of numbers.

Here's a bit of code that should be simple enough to understand if you are at all familiar with simple programming languages.

Code:

// nNumVertices contains the total number of x/z points we are dealing with
//aAllVerts is an array containing all x/z pairs
var minVertX = 0;
var minVertZ = 0;
var maxVertX = 0;
var maxVertZ = 0;

// Find max and min x and z values
for( j = 0; j < nNumVertices; j++ )
{
        if ( aVertices[1] < minVertX)
        {
                minVertX = aVertices[1];
        }
       
        if ( aVertices[1] > maxVertX)
        {
                maxVertX = aVertices[1];
        }
       
        if ( aVertices[2] < minVertZ)
        {
                minVertZ = aVertices[2];
        }
       
        if ( aVertices[2] > maxVertZ)
        {
                maxVertZ = aVertices[2];
        }
}

// Determine if the x or z direction contains the greatest span of values
if( maxVertX - minVertX > maxVertZ - minVertZ)
{
        maxVertValue = maxVertX - minVertX;
}

if( maxVertValue < maxVertZ - minVertZ)
{
        maxVertValue = maxVertZ - minVertZ;
}

The vertices that are to be converted can have both negative and positive values.

For example:
(x,Z) Vartices to be converted:

203.58326721191406 / 133.94407653808594
646.3115234375 / 912.4210815429688
-901.4326171875 / 1094.460205078125
-1500.000244140625 / 1735.3692626953125
-1500 / 133.94407653808594

In this illustration, the black values are the current x/z values, the blue values are what I want to convert to.

http://i7.photobucket.com/albums/y27...conversion.png

Thanks,
Beth

hkoehler May 18th, 2010 05:57 AM

Re: Coordinate conversion
 
It seems to me that you're looking for the following:
transform(x,y) = ((x-minVertX)/maxVertValue, (y-minVertY)/maxVertValue)

DoodleGal May 18th, 2010 09:27 AM

Re: Coordinate conversion
 
Quote:

Originally Posted by hkoehler
transform(x,y) = ((x-minVertX)/maxVertValue, (y-minVertY)/maxVertValue)

That's it all right. As long as maxVertValue is the largest of all the vertex values, whether it's x or y.
In the example, maxVertValue = 1735.3692626953125.

I actually figured it out yesterday. But thanks for responding. :)


All times are GMT -8. The time now is 12:18 PM.

Copyright © 2018 My Math Forum. All rights reserved.