简体   繁体   中英

map the point x,y in Cartesian coordinate into 2D Array elements

I would like to create a map of the enviroment, ie. my room. I want to have a 2D representation of my room using 2D Array.

I am trying to map a room, which has coordinate start at (5,5), (10,5), (5,10) and (10,10). This is a square of size 5x5 if you ever wonder. I would like to map this into a 2D grid of 100x100 cells.

The math I am using to map is,

X' = (maxRangeX - minRangeX)*(X - minX)/(maxX - minX) + minRangeX
Y' = (maxRangeY - minRangeY)*(Y - minY)/(maxY - minY) + minRangeY

where

maxRangeX   100 maxX    10  maxRangeY   100 maxY    10

minRangeX   0   minX    5   minRangeY   0   minY    5

As you know, the way Array works is it start (0,0) at the top left, and (100,100) at the bottom right, in this case I mean. But, by using the math I showed above, it seems to map the room into a 2D array which has (0,0) start at bottom left and (100,100) ends at top right, (0,100) on top left and (100,0) on bottom right.

I want to map the room over so that the result is ordered in a structure like how array structure does.

Any help is greatly appreciated.

Update:

The idea is still as above, and here is a picture to illustrate further.

http://i.stack.imgur.com/h4Mgm.png

Using your notation:

X' = maxRangeY - ((maxRangeY - minRangeY)*(maxY-(Y - minY))/(maxY - minY) + minRangeY)
Y' = maxRangeX - ((maxRangeX - minRangeX)*(maxX-(X - minX))/(maxX - minX) + minRangeX)

The "invert" of x maps to the "invert" of y and the "invert" of y maps to the "invert" of x.

C# functions:

static int Transform(int coord, int coordMin, int newCoordMin, double factor)
{
   return (newCoordMin + (int)((coord - coordMin) * factor));
}
static void Transform(int[,] arrIn, int minX, int minY, int maxX, int maxY,
                    out int[,] arrOut, int minRangeX, int minRangeY, int maxRangeX, int maxRangeY)
{

double factorX = (maxRangeX - minRangeX) / (maxX - minX);
double factorY = (maxRangeY - minRangeY) / (maxY - minY);

arrOut = new int[maxRangeX + 1, maxRangeY + 1];

for (int x = minX; x <= maxX; ++x)
    for (int y = minY; y <= maxY; ++y)
        arrOut[maxRangeY - Transform(y, minY, minRangeY, factorY), maxRangeX - Transform(x, minX, minRangeX, factorX)] = arrIn[maxX - (x - minX), maxY - (y - minY)];
}

This is working supposing the following conditions:
- the ranges on x and on y are the same for the first square and for the second, respectively
- the intermediate points in the larger square aren't calculated (interpolated)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM