Skip to content
Permalink
98ddaeec7d
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
73 lines (63 sloc) 2.36 KB
public static class CompGeo {
public static int CCW(Point a, Point b, Point c) {
// Return 1 if turn abc is ccw -1 if turn abc is cw 0 if no turn
float f = (b.getX() - a.getX())*(c.getY() - a.getY()) - (c.getX() - a.getX())*(b.getY()-a.getY());
if (f < 0) { return 1; }
if (f > 0) { return -1; }
return 0;
}
public static int immediateCCW(Point parent, Point child, Point v1, Point v2) {
// returns 1 if v1 is more immediate CCW of parent, child than v2
// returns -1 if v2 is more immediate CCW of parent, child than v1
if (CCW(parent, child, v1) > CCW(parent, child, v2)) { return 1; }
if (CCW(parent, child, v1) < CCW(parent, child, v2)) { return -1; }
return CCW(parent, v1, v2);
}
private static boolean between(Point a, Point b, Point c) {
if (a.getX() > c.getX()) { return ((b.getX() > c.getX()) && (b.getX() < a.getX())); }
if (a.getX() < c.getX()) { return ((b.getX() < c.getX()) && (b.getX() > a.getX())); }
if (a.getY() > c.getY()) { return ((b.getY() > c.getY()) && (b.getY() < a.getY())); }
if (a.getY() < c.getY()) { return ((b.getY() < c.getY()) && (b.getY() > a.getY())); }
return false;
}
public static int intersect(Point a, Point b, Point c, Point d) {
if ((CCW(a,b,c) != CCW(a,b,d)) && ((CCW(c,d,a) != CCW(c,d,b)))) {
return 1;
}
if ((CCW(a,b,c) == 0) & (CCW(a,b,d) == 0)) {
if (between(a,c,b) || between(a,d,b) || between(c,a,d) || between(c,b,d)) {
return 1;
}
}
return 0;
}
public static boolean inside(HalfEdge h, Point p, Point q) {
HalfEdge temp = h.getnext();
Point e1 = h.getOrigin();
Point e2 = h.gettwin().getOrigin();
int nis = intersect(e1, e2, p, q);
while (temp != h) {
e1 = temp.getOrigin();
e2 = temp.gettwin().getOrigin();
nis += intersect(e1, e2, p, q);
temp = temp.getnext();
}
if ((nis % 2) == 0) { return false; }
else { return true; }
}
public static float signedArea(HalfEdge h) {
return signedArea(h.getPointsNext());
}
public static float signedArea(ArrayList<Point> points) {
int num = points.size();
Point p = null;
Point q = null;
float sum = 0;
for (int i = 0; i < num; i++) {
p = points.get(i);
q = points.get((i+1)%num);
sum += (p.getX()*q.getY() - q.getX()*p.getY());
}
return sum;
}
}