PDA

View Full Version : I'm looking for a rectangle algorithm...


Raptisoft
07-22-2005, 05:09 AM
Hi all,

I'm looking for an algo, before I attempt to write it myself. This is what I want to accomplish.

I have a big rectangle, let's say 1024x1024. I want to chop rectangular pieces out of it, so let's say I have:

Rect(0,0,1024,1024)

And I want to REMOVE:

Rect(50,50,300,300)
Rect(200,55,800,100)
Rect(100,400,50,50)

...What I'm looking for is some sort of algo that will give me all the NON-REMOVED spaces as a list of rectangles. Does this sound familiar to anyone?

Rod Hyde
07-22-2005, 05:24 AM
Is this (http://www.rawmaterialsoftware.com/juce/api/classRectangleList.html) what you're looking for? "This class allows a set of rectangles to be treated as a solid shape, and can add and remove rectangular sections of it, and simplify overlapping or adjacent rectangles."

--- Rod

Raptisoft
07-22-2005, 05:28 AM
Yeah, that's EXACTLY what I'm looking for... is there source, or is this some library you have to crosslink about a million things for?

Edit: Never mind, I found the download... not exactly the friendliest site for browsing. :)

Raptisoft
07-22-2005, 06:09 AM
...and let me just add that the way other programmers do things, like arrays, etc, is really stupid.

See, this is why I never go to GDC or IDC or whatever. I'd be a machine gun toting maniac in seconds.

mahlzeit
07-22-2005, 07:00 AM
Explain, Raptisoft, explain! :)

Raptisoft
07-22-2005, 07:04 AM
Storing rectangles as four coordinates in a single array, so that one must do all operations in by shifting by four, by removing four, by adding four... it deserves the death penalty. No appeal!

But all other things aside, I managed to adapt this to my own setup pretty fast, so I suppose it's grumble okay.

svero
07-22-2005, 08:25 AM
Ahh.. open sores... Be careful you don't get infected. Adapting this kind of code from the net is a little like accepting free multiple blood tranfusions from random donors in botswana. You'll regret it!

Raptisoft
07-22-2005, 08:34 AM
That's funny Svero, because now I'm getting random lockups while processing, that didn't happen before. :)

svero
07-22-2005, 08:54 AM
Totally predictable. In Aargon I used a piece of free code I found on the web to do one small thing with sizing an image in a dialog. There was of course a bug in the game related to that where the dialog image would appear all warped out of size on some systems that I didnt squash until i realized it was there much later...

ggambett
07-22-2005, 09:53 AM
What about this... make a list of the horizontal and vertical lines defined by the rectangles you want to cut out, remove the duplicates, and you get your original rectangle with several vertical and horizontal cuts; the rectangles you want to cut away are defined by these lines. So make a list of rectangles generated by the vertical and horizontal cuts, remove the ones you want to remove, and optionally merge the remaining ones (since they're non-overlapping, it's relatively easy)

Jack Norton
07-22-2005, 12:36 PM
I sense an incoming Qix clone from Raptisoft... ;)