Skip to content

Instantly share code, notes, and snippets.

@PatrickLerner
Created June 13, 2013 11:07
Show Gist options
  • Save PatrickLerner/5772917 to your computer and use it in GitHub Desktop.
Save PatrickLerner/5772917 to your computer and use it in GitHub Desktop.
/**
* Looks at the triangle references in e and replaces the oldRef with the
* newRef
*
* @param e
* the edge
* @param oldRef
* the old reference that should be replaced
* @param newRef
* the new reference
*/
private void replaceTraingleRef(Edge e, Triangle oldRef, Triangle newRef) {
if (e.getT1().equals(oldRef))
e.setT1(newRef);
else
e.setT2(newRef);
}
private LinkedList<Triangle> getTrianglesOfEdge(Edge ed) {
LinkedList<Triangle> result = new LinkedList<Triangle>();
if (ed.getT1() != null)
result.add(ed.getT1());
if (ed.getT2() != null)
result.add(ed.getT2());
return result;
}
public Point getCommonPoint(Edge e1, Edge e2) {
if (e2.containsPoint(e1.getP1()))
return e1.getP1();
if (e2.containsPoint(e1.getP2()))
return e1.getP2();
return null;
}
/**
* insert a point into triangulation an correct Triangulation if necessary
*
* @param p
* Point to insert
*/
public void insertPoint(Point p) {
Triangle triangle;
try {
triangle = this.getTriangle(p);
LinkedList<Edge> triangleEdges = triangle.getEdges();
LinkedList<Point> outerPoints = new LinkedList<Point>();
outerPoints.add(this.getCommonPoint(triangleEdges.get(0), triangleEdges.get(1)));
outerPoints.add(this.getCommonPoint(triangleEdges.get(0), triangleEdges.get(2)));
outerPoints.add(this.getCommonPoint(triangleEdges.get(1), triangleEdges.get(2)));
// build new edges
Edge e1 = new Edge(p, outerPoints.get(0), null, null);
Edge e2 = new Edge(p, outerPoints.get(1), null, null);
Edge e3 = new Edge(p, outerPoints.get(2), null, null);
// now build triangles
Triangle t1 = new Triangle(buildEdgeList(triangleEdges.get(0), e1, e2));
Triangle t2 = new Triangle(buildEdgeList(triangleEdges.get(1), e2, e3));
triangle.setEdges(buildEdgeList(triangleEdges.get(2), e3, e1));
// set edges correctly
this.replaceTraingleRef(triangleEdges.get(0), triangle, t1);
this.replaceTraingleRef(triangleEdges.get(1), triangle, t2);
e1.setT1(t1);
e1.setT2(t2);
e2.setT1(t1);
e2.setT2(triangle);
e3.setT1(t2);
e3.setT2(triangle);
} catch (IllegalAccessException e4) {
e4.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment