I have a method that adds points to a field of random, unconnected points, and the next point to be added must be within the largest empty triangle drawn between three points. The four corners of the field are the first points to be added.
My problem is that my current solution for calculating every triangle uses four nested enhanced-for loops '(foreach
style), and takes a long time as the numbers of points add up. I am looking at processing this in parallel (maybe with Java 8 parallel streams?), but is there a more efficient way select the points or test if they are inside the triangle?
Many thanks
for(Point p1 : POINTS) {
for(Point p2 : POINTS) {
if(p2==p1) {continue;}
for(Point p3 : POINTS) {
if(p3==p2 || p3==p1) {continue;}
Polygon p = new Polygon(new int[]{p1.x,p2.x,p3.x}, new int[]{p1.y,p2.y,p3.y}, 3);//Construct a triangle from these points
boolean empty = true;
for(Point p4 : POINTS) {//test the remaining points if they are in the triangle
if(p4==p3 || p4 == p2 || p4 == p1) {continue;}
if(p.contains(p4)){
empty = false;
break;
}
}
if(empty==true) {
emptyTriangles.add(new Triplet<Point,Point,Point>(p1,p2,p3));//emptyTriangles contains the coordinates of the empty triangles
}
}
}
}