[Polycasso-commit] SF.net SVN: polycasso:[136] trunk/polycasso/src/com/mebigfatguy/polycasso/ Polyg
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2009-12-03 05:15:54
|
Revision: 136
http://polycasso.svn.sourceforge.net/polycasso/?rev=136&view=rev
Author: dbrosius
Date: 2009-12-03 05:15:43 +0000 (Thu, 03 Dec 2009)
Log Message:
-----------
try to push add polygon to create more localized polygons, and weight it, so that small polys are created more often.
Modified Paths:
--------------
trunk/polycasso/src/com/mebigfatguy/polycasso/PolygonData.java
Modified: trunk/polycasso/src/com/mebigfatguy/polycasso/PolygonData.java
===================================================================
--- trunk/polycasso/src/com/mebigfatguy/polycasso/PolygonData.java 2009-12-03 04:03:57 UTC (rev 135)
+++ trunk/polycasso/src/com/mebigfatguy/polycasso/PolygonData.java 2009-12-03 05:15:43 UTC (rev 136)
@@ -23,6 +23,7 @@
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Polygon;
+import java.awt.Rectangle;
import java.util.Random;
/**
@@ -92,17 +93,51 @@
public static PolygonData randomPoly(Dimension size, int maxPoints) {
Random r = new Random();
Polygon polygon = new Polygon();
+ Rectangle polyRect = getPolyBounds(r, size);
+
int numPoints = r.nextInt(maxPoints - 3) + 3;
-
for (int i = 0; i < numPoints; i++) {
- polygon.addPoint(r.nextInt(size.width), r.nextInt(size.height));
+ polygon.addPoint(polyRect.x + r.nextInt(polyRect.width), polyRect.y + r.nextInt(polyRect.height));
}
Color c = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());
return new PolygonData(c, r.nextFloat(), polygon);
}
+
+ /**
+ * returns a rectangle that new polygons should points out of. It attempts to allow
+ * for creating localized polygons, so that small areas will be generated more likely.
+ *
+ * @param r a random generator object
+ * @param maxSize the max size of the image
+ *
+ * @return a rectangle to pick polygon points out of
+ */
+ private static Rectangle getPolyBounds(Random r, Dimension maxSize) {
+ Rectangle bounds = new Rectangle();
+
+ int polySize = r.nextInt(12);
+ if (polySize < 3) {
+ bounds.width = 2 + (maxSize.width / 7);
+ bounds.height = 2 + (maxSize.height / 7);
+ } else if (polySize < 6) {
+ bounds.width = 2 + (maxSize.width / 5);
+ bounds.height = 2 + (maxSize.height / 5);
+ } else if (polySize < 9) {
+ bounds.width = 2 + (maxSize.width / 3);
+ bounds.height = 2 + (maxSize.height / 3);
+ } else {
+ bounds.width = maxSize.width;
+ bounds.height = maxSize.height;
+ }
+
+ bounds.x = r.nextInt(maxSize.width - bounds.width + 1) - 1;
+ bounds.y = r.nextInt(maxSize.height - bounds.height + 1) - 1;
+ return bounds;
+ }
+
/**
* clones this polygon data data
*
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|