Created
March 26, 2010 00:57
-
-
Save xcoderzach/344341 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.geom.Line2D; | |
import java.util.ArrayList; | |
import java.util.Random; | |
public class Polygon { | |
private ArrayList<Point> points = new ArrayList<Point>(); | |
public static void main(String[] args) { | |
Polygon poly = new Polygon(); | |
poly.randomPolygon(); | |
for (int i = 0; i < poly.points.size(); i++) { | |
System.out.print(poly.points.get(i).getX() + ","); | |
System.out.print(poly.points.get(i).getY() + ";"); | |
} | |
} | |
private void addPoint(double x, double y) { | |
points.add(new Point(x, y)); | |
} | |
public void randomPolygon() { | |
Random rand = new Random(); | |
addPoint(rand.nextDouble() * 10, rand.nextDouble() * 10); | |
addPoint(rand.nextDouble() * 10, rand.nextDouble() * 10); | |
for (int i = 0; i < 1000000; i++) { | |
int segment = rand.nextInt(points.size()); | |
points.add(segment, new Point()); | |
if(!isConvex()) { | |
points.remove(segment); | |
} | |
} | |
} | |
private boolean isConvex() { | |
for (int j = 0; j < points.size(); j++) { | |
Point point1 = points.get(j); | |
Point point2 = points.get((j + 1) % points.size()); | |
Point point3 = points.get((j + 2) % points.size()); | |
if(!counterClockwise(point1, point2, point3)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
private boolean counterClockwise(Point point1, Point point2, Point point3) { | |
return (point2.getX() - point1.getX())*(point3.getY() - point1.getY()) - (point2.getY() - point1.getY())*(point3.getX() - point1.getX()) > 0; | |
} | |
private double getArea() { | |
double area = 0.0; | |
for(int i = 0; i < points.size() ; i++) { | |
int j = (i+1) % points.size(); | |
area += points.get(i).getX() * points.get(j).getY(); | |
area -= points.get(i).getY() * points.get(j).getX(); | |
} | |
return Math.abs(area/2); | |
} | |
// checks that point3 and point4 are on opposite sides of the line passing | |
// through point1 and point2 | |
private boolean intersects(Point point1, Point point2, Point point3, | |
Point point4) { | |
// subtract a very small amount, to fix the edge case where a ray tested | |
// passes through an intersection | |
return Line2D.linesIntersect(point1.getX(), point1.getY(), point2 | |
.getX() - 0.001, point2.getY() - 0.001, point3.getX(), point3 | |
.getY(), point4.getX(), point4.getY()); | |
} | |
private double getSlope(Point point1, Point point2) { | |
double slope = (point1.getY() - point2.getY()) / (point1.getX() - point2.getX()); | |
return slope; | |
} | |
/** | |
* If a ray extending from the new point, intersects the edges of the | |
* polygon in and odd number of places it must be contained otherwise it is | |
* not. | |
*/ | |
public boolean contains(double x, double y) { | |
int intersections = 0; | |
for (int i = 0; i < points.size(); i++) { | |
// using mod so it wraps back around connecting the first point to | |
// the last point | |
Point point1 = points.get((i + 1) % points.size()); | |
Point point2 = points.get(i); | |
if (intersects(point1, point2, new Point(x, y), new Point(x, 10))) { | |
intersections++; | |
} | |
} | |
return intersections == 1; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment