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.

[Resolved] [C#] Screen Rectangle Problem

This problem is something I don't seem to be able to wrap my head around. It's not an error or anything, it's just that my mind doesn't seem to let me find a suitable way to do this.

What I am trying to do is, first, based on some coordinate data, the code generates a list of rectangles, all within the screen's bounds. This first part is fine, no problems here.
But now, what I want to do next, is to get all the rectangles of the portions of the screen that was not used by the first list.

An example, assuming that the resolution is 1920x1080 and we have one rectangle in the first list with the following data:
x = 600, y = 240, width = height = 64

The rectangles that should be generated in the second list should cover the remaining portions of the screen, thus the list should contain:
Rect1: x = 0, y = 0, width = 1920, height = 600
Rect2: x = 0, y = 240, width = 600, height = 840
Rect3: x = 664, y = 240, width = 1256, height = 840
Rect4: x = 600, y = 304, width = 600, height = 776
(This was taken from rough assumption, I reckon these should fill the remaining portions)

To clarify:
  • The rectangles that fill the screen must NOT overlap a single pixel of the rectangles in the first list.
  • Every pixel of the screen must be covered in rectangles from either the first or the second list.
  • The first list can contain anything from none to 500 rectangles. So it must be dynamic.

Any help on this?

Thanks in advance.
 
An iterative algorithm. (Iterations <= number of rectangles in first list)

First, order the first list either left to right (X), or top to bottom (Y).
Using left to right...

build the first new rect to the left of the first rectangle.

Check to see if the 2nd rect falls above or below the 1st. ( X(2) < X(1) + WIDTH(1) )
Fill in the area between X(1) & X(2)

Check 3rd rect to first & 2nd... etc...

When all tests are false, start over with the last X(n) + WIDTH(n), Y(n) + HEIGHT(n) as the new starting point.

HOpe that makes sense... basically, work from top left to bottom right until it's all filled in.

I made the assumption that the rects in the first list do not overlap (share the same pixels)
 
Brewmeister":21jw3acu said:
I made the assumption that the rects in the first list do not overlap (share the same pixels)
That is an accurate assumption.

Thanks for the reply, it served its purpose and did exactly as I wanted it to do. Thanks!
 

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