ryanhallcs

Simple Java Polygon Generator

May 8th, 2011
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. public static Polygon Generate(int seed)
  2. {
  3.     Random generator = new Random(seed);
  4.                
  5.     // Set points using a min and max range
  6.     int numPoints = (int)Math.floor(generator.nextDouble() * (maxPts-minPts)) + minPts;
  7.  
  8.     int[] yPolyPoints = new int[numPoints];
  9.     int[] xPolyPoints = new int[numPoints];
  10.  
  11.     // Set the radius using min and max range as well
  12.     radius = (int)Math.floor(generator.nextDouble() * (maxRad - minRad)) + minRad;
  13.  
  14.     double crAng = 0,
  15.    
  16.             // Angle between each point
  17.             angDiff = Math.toRadians(360.0 / numPoints),
  18.            
  19.             // Arbitrary radJitter range; notice I subtract each by the jitter / 2.0 to get a +/- jitter with this range
  20.             radJitter = radius / 3.0,
  21.             angJitter = angDiff * .9;
  22.    
  23.     for (int i=0; i<numPoints; i++)
  24.     {
  25.             double tRadius = radius + (generator.nextDouble() * radJitter - radJitter / 2.0);
  26.             double tAng = crAng + (generator.nextDouble() * angJitter - angJitter / 2.0);
  27.             int nx = (int)(Math.sin(tAng) * tRadius),
  28.                     ny = (int)(Math.cos(tAng) * tRadius);
  29.             yPolyPoints[i] = ny;
  30.             xPolyPoints[i] = nx;
  31.             crAng += angDiff;
  32.     }
  33.  
  34.     return new Polygon(xPolyPoints, yPolyPoints, numPoints);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment