import java.awt.*; /** * Edge.java represents a half plane defined by an edge of a clip polygon for * a polygon clipping algorithm * * Created: Mon Aug 16 23:40:36 1999 * * @author Tim Lambert */ class Edge { private int x1,y1; //start point private int x2,y2; //end point private int a,b,c; // (x,y) is inside the edge if a*x + b*y + c > 0; //we use ints to store the equation to avoid round off problems Edge(int x1, int y1, int x2, int y2) { a = y2 - y1; b = x1 - x2; c = -a*x1 - b*y1; this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } /* is this point inside the half plane defined by the edge? */ public boolean inside(Point p){ return a*p.x + b*p.y + c > 0; } /* return the intersection of this edge with another one from p1 to p2 */ /* rounded to nearest int */ public Point intersect(Point p1, Point p2) { int d = p2.y - p1.y; int e = p1.x - p2.x; int f = -d*p1.x - e*p1.y; int denom = e*a - b*d; int x = Math.round((b*f - e*c)/(float)denom); int y = Math.round((d*c - a*f)/(float)denom); return new Point(x,y); } /* draw this edge on a Graphics object */ /* We don't just draw from (x1,y1) to (x2,y2) because the half plane is bounded by an infinite line. But we can't draw an infinite line so compromise by drawing one 5 times as long as (x1,y1)-(x2,y2) */ public void draw(Graphics g){ g.drawLine(x1+2*b,y1-2*a,x2-2*b,y2+2*a); } /* Convert to a string - handy for debugging */ public String toString() { return "("+x1+","+y1+")-("+x2+","+y2+")"; } }