Stack<Point> S = new Stack<Point>();
S.push(p0);
S.push(points.get(0));
Point top, nextToTop;
Triangle lastAdded = null, temp = null,
lastAddedInStack = null, tempInStack = null;
for (int i = 0; i < points.size(); i++) {
if (i < points.size() - 1) {
// Adding new "narrow" triangle with p0 as one of its vertices
temp = new Triangle(p0, points.get(i), points.get(i+1));
// Linking the triangles (!)
temp.link(lastAdded);
result.add(temp);
lastAdded = temp;
}
if (i > 0) {
// using the idea of Graham's scan
// to make the triangulation "convex" (!)
top = S.peek();
nextToTop = S.elementAt(S.size() - 2);
while (isRightTurn(nextToTop, top, points.get(i))) {
tempInStack = new Triangle(nextToTop, top, points.get(i));
// Linking the triangles (!)
tempInStack.link(lastAddedInStack);
int k = 0;
for (int j = result.size()-1; j >= 0; j--) {