Reputation: 1649
I have an endless grid and need a javascript function to get coordinates within the area of a start and endpoint. The grid looks like this.
_______________________
| | | |
| -1,-1 | 0,-1 | 1,-1 |
|_______|_______|_______|
| | | |
| -1,0 | 0,0 | 1,0 |
|_______|_______|_______|
| | | |
| -1,1 | 0,1 | 1,1 |
|_______|_______|_______|
So lets say I want all coordinates from (-1,-1) to (0,0). In this case I would get the 4 values (-1,-1), (0,-1), (-1,0), (0,0).
I know this are basic mathematics but I don't find a clever solution here.
Upvotes: 2
Views: 194
Reputation: 4183
You could try something like this:
function GetCoordinatesBetween(xStart, xEnd, yStart, yEnd) {
var curX = xStart;
var curY = yStart;
var res = []; // Array for result coordinates
while (curX <= xEnd) {
while (curY <= yEnd) {
res.push({ x: curX, y: curY });
curY++;
}
curX++;
curY = yStart;
}
return res;
}
This function returns your coordinates as array in the following format:
[
0: { x: -1, y: -1},
1: { x: -1, y: 0},
...
]
Upvotes: 2