So I've been trying to do this myself, but it's just definitely something wrong with my idea of math because it's not collecting any of the data I want it to collect.
Here's a quick rundown of the function's use:
So if here's two different passes and results the method should give in each given scenario:
Pass #1:
The idea is that, regardless of the order the coordinates are given, the rectangle should always be the same in its result.
If anyone could give an approach to this, I'd be grateful. Preferably in code - doesn't have to be C# code, just something that relates to code or any other language.
EDIT: Here's my current attempt at the function. It's using XNA and the method is an extension. You can see what I'm trying to achieve, but it does not compute at all. To clarify: the list is always empty, regardless of what coordinates are given.
Thanks in advance!
Here's a quick rundown of the function's use:
- The method is given two coordinates, each with an X and Y value.
- Based on these two coordinates, the method should find out the coordinate of the top left-corner of the rectangle.
- Also, it should find the width and height of the rectangle.
So if here's two different passes and results the method should give in each given scenario:
Pass #1:
- Coordinate 1: 30,14
- Coordinate 2: 50,20
- Result: Rectangle X = 30, Y = 14, Width = 20, Height = 6
- Coordinate 1: 50,20
- Coordinate 2: 30,14
- Result: Rectangle X = 30, Y = 14, Width = 20, Height = 6
The idea is that, regardless of the order the coordinates are given, the rectangle should always be the same in its result.
If anyone could give an approach to this, I'd be grateful. Preferably in code - doesn't have to be C# code, just something that relates to code or any other language.
EDIT: Here's my current attempt at the function. It's using XNA and the method is an extension. You can see what I'm trying to achieve, but it does not compute at all. To clarify: the list is always empty, regardless of what coordinates are given.
Code:
public static List<Vector2> GetTileRect(this Tileset tileset, Vector2 start, Vector2 end)
{
// Get tile character
char key = tileset.GetChar(Game1.SelectedTile);
// Get true vectors
Vector2 realStart, realEnd;
float x = start.X < end.X ? start.X : end.X;
float y = start.Y < end.Y ? start.Y : end.Y;
realStart = new Vector2(x, y);
x = start.X < end.X ? end.X : start.X;
x = start.Y < end.Y ? end.Y : start.Y;
realEnd = new Vector2(x, y);
// Get width and height (in tiles)
int width = (int)(realEnd.X - realStart.X);
int height = (int)(realEnd.Y - realStart.Y);
// Iterate each tile
List<Vector2> tiles = new List<Vector2>();
for(int px = 0; px < width; px++){
for(int py = 0; py < height; py++){
tiles.Add(new Vector2(realStart.X + px, realStart.Y + py));
}
}
// Return list of tiles
return tiles;
}
Thanks in advance!