// file TriangleIntersections.java // input: Two arrays of 3 2-dimensional points, bluePts and greenPts. // output: All intersections points of an edge of the blue triangle with // an edge of the green triangle. (There can be 0, 2, 4, or 6 of // these.) // Either triangle may be degenerate (have 2 identical points) // Note: this does not compute the intersections of the triangles, which is // a more complicated case analysis public class TriangleIntersections { public static final boolean tracing = true; // Construct a new segment from point b to point g. // If valid segment save in segs and increment nSegs // If not, do not increment nSegs // return new value of nSegs // public static int constructSegment(double[] p1, double[] p2, Segment[] segs, int nSegs) { try { segs[nSegs] = new Segment(p1,p2); segs[nSegs].setMyline(); return nSegs+1; } catch (DegenerateSegment e) { if (tracing) System.out.println("DegenerateSegment"); return nSegs; //unincremented } } // end constructSegment // Construct a String to display point p public static String showPoint(double[] p) { return "[" + p[0] + ", " + p[1] + "]"; } // Find all intersections between the triangles bluePts and greenPts public static double[][] TriangleIntersections(double[][] bluePts, double[][] greenPts, double flag) { Segment[] blueSegs = new Segment[3]; Segment[] greenSegs = new Segment[3]; int nBlue = 0; // counting number of Segments nBlue = constructSegment(bluePts[0], bluePts[1], blueSegs, nBlue); nBlue = constructSegment(bluePts[0], bluePts[2], blueSegs, nBlue); nBlue = constructSegment(bluePts[1], bluePts[2], blueSegs, nBlue); int nGreen= 0; // counting number of Segments nGreen= constructSegment(greenPts[0], greenPts[1], greenSegs, nGreen); nGreen= constructSegment(greenPts[0], greenPts[2], greenSegs, nGreen); nGreen= constructSegment(greenPts[1], greenPts[2], greenSegs, nGreen); // if there is a degeneracy, then there is only one segment if (nBlue==2) nBlue=1; if (nGreen==2) nGreen=1; if ((nBlue == 0) || (nGreen == 0)) return new double[1][2]; double[][] intersects = new double[nBlue*nGreen][2]; int nInts = 0; // number of intersection points for (int i=0; i < nBlue ;i++) { for (int j=0; j < nGreen; j++) { try { intersects[nInts] = blueSegs[i].intersect(greenSegs[j]); if (tracing) System.out.println("Intersection of " + blueSegs[i].toString() + "\n and " + greenSegs[j].toString() + " is " + showPoint(intersects[nInts])); nInts++; } // end try catch (OffSegmentException e) { if (tracing) System.out.println("Intersection of " + blueSegs[i].toString() + " with " + greenSegs[j].toString() + " is off the segments"); } // end catch catch (ParallelException e) { if (tracing) System.out.println("Segments" + blueSegs[i].toString() + " and " + greenSegs[j].toString() + " are parallel"); } // end catch } // end inner for loop } // end outer for loop if (nInts < intersects.length-1) intersects[nInts][0] = flag; return intersects; } // end method TriangleIntersections public static void main(String[] args) { double[][] bluePts = {{0.0,0.0},{3.0,0,0},{0.0,3.0}}; double[][] greenPts= {{1.0,1.0}, {4.0,4.0}, {1.0,4.0}}; double flag = -10.0; // Flag for null value double[][] ints = TriangleIntersections(bluePts,greenPts,flag); System.out.println("Intersection points:"); int i=0; while (i < ints.length && ints[i][0] > flag) { System.out.println(" " + showPoint(ints[i])); i++; } } // end main } // end class TriangleIntersections