Skip to content

Instantly share code, notes, and snippets.

@connorjclark
Created June 17, 2013 08:35
Show Gist options
  • Save connorjclark/5795475 to your computer and use it in GitHub Desktop.
Save connorjclark/5795475 to your computer and use it in GitHub Desktop.
amitp Polygonal Map Generation - rendering the outermost polygons
//Written by Hoten
//www.hotengames.com
//feel free to use
for (Center c : centers) {
g.setColor(key[c.index]);
//only used if Center c is on the edge of the graph. allows for completely filling in the outer polygons
Corner edgeCorner1 = null;
Corner edgeCorner2 = null;
for (Center n : c.neighbors) {
Edge e = edgeWithCenters(c, n);
if (e.v0 == null) {
//outermost voronoi edges aren't stored in the graph
continue;
}
//find a corner on the exterior of the graph
//if this Edge e has one, then it must have two,
//finding these two corners will give us the missing
//triangle to render. this special triangle is handled
//outside this for loop
Corner cornerWithOneAdjacent = e.v0.border ? e.v0 : e.v1;
if (cornerWithOneAdjacent.border) {
if (edgeCorner1 == null) {
edgeCorner1 = cornerWithOneAdjacent;
} else {
edgeCorner2 = cornerWithOneAdjacent;
}
}
drawTriangle(g, e.v0, e.v1, c);
}
//handle the missing triangle
if (edgeCorner2 != null) {
//if these two outer corners are NOT on the same exterior edge of the graph,
//then we actually must render a polygon (w/ 4 points) and take into consideration
//one of the four corners (either 0,0 or 0,height or width,0 or width,height)
//note: the 'missing polygon' may have more than just 4 points. this
//is common when the number of sites are quite low (like less than 5), but not a problem
//with a more useful number of sites.
//TODO: find a way to fix this
if (closeEnough(edgeCorner1.loc.x, edgeCorner2.loc.x, 1)) {
drawTriangle(g, edgeCorner1, edgeCorner2, c);
} else {
int[] x = new int[4];
int[] y = new int[4];
x[0] = (int) c.loc.x;
y[0] = (int) c.loc.y;
x[1] = (int) edgeCorner1.loc.x;
y[1] = (int) edgeCorner1.loc.y;
//determine which corner this is
x[2] = (int) ((closeEnough(edgeCorner1.loc.x, bounds.x, 1) || closeEnough(edgeCorner2.loc.x, bounds.x, .5)) ? bounds.x : bounds.right);
y[2] = (int) ((closeEnough(edgeCorner1.loc.y, bounds.y, 1) || closeEnough(edgeCorner2.loc.y, bounds.y, .5)) ? bounds.y : bounds.bottom);
x[3] = (int) edgeCorner2.loc.x;
y[3] = (int) edgeCorner2.loc.y;
g.fillPolygon(x, y, 4);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment