Skip to content

Instantly share code, notes, and snippets.

@Unh0lyTigg
Last active December 23, 2015 14:09
Show Gist options
  • Save Unh0lyTigg/6647064 to your computer and use it in GitHub Desktop.
Save Unh0lyTigg/6647064 to your computer and use it in GitHub Desktop.
Compute the overall area of some rectangles...
public class RectsArea {
public static long area(java.awt.Rectangle... rects) {
if (rects == null)
return 0;
java.awt.Rectangle massArea = null;
for (java.awt.Rectangle r : rects)
if (massArea == null)
massArea = r;
else
massArea = massArea.union(r);
if (massArea == null)
return 0;
if (massArea.x < 0) {
int moved = 0 - massArea.x;
massArea.x = 0;
for (java.awt.Rectangle r : rects)
r.x += moved;
}
if (massArea.y < 0) {
int moved = 0 - massArea.y;
massArea.y = 0;
for (java.awt.Rectangle r : rects)
r.y += moved;
}
java.awt.image.BufferedImage map = new java.awt.image.BufferedImage(massArea.width, massArea.height, java.awt.image.BufferedImage.TYPE_INT_ARGB);
java.awt.Graphics g = map.createGraphics();
java.awt.Color brush = new java.awt.Color(0, 0, 0, 1);
g.setColor(brush);
for (java.awt.Rectangle r : rects)
g.fillRect(r.x, r.y, r.width, r.height);
g.dispose();
long area = 0;
for (int x = 0; x < map.getWidth(); ++x)
for (int y = 0; y < map.getHeight(); ++y)
if (new java.awt.Color(map.getRGB(x, y)).equals(brush))
++area;
return area;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment