Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[C#] Rectangle Out of Two Random Coordinates

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:
  • 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
Pass #2:
  • 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!
 
You have a typo in your code, specifically:

Code:
x = start.Y < end.Y ? end.Y : start.Y;

I think you meant to say y = ... instead of x = ....

The coordinates of the top-left corner can be given by (min(X1, X2), min(Y1, Y2)). The width is abs(X1 - X2) and the height is abs(Y1 - Y2).
 
Ahh, thank you for pointing this out. That typo was the whole reason why this shit wasn't working >_> added the abs to the width and height as well.

Thanks, it's solved now!
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top