Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean checkIntersect(Segment other)
- {
- double mx1 = Math.min(p1.x,p2.x);
- double Mx1 = Math.max(p1.x,p2.x);
- double mx2 = Math.min(other.p1.x,other.p2.x);
- double Mx2 = Math.max(other.p1.x,other.p2.x);
- double my1 = Math.min(p1.y,p2.y);
- double My1 = Math.max(p1.y,p2.y);
- double my2 = Math.min(other.p1.y,other.p2.y);
- double My2 = Math.max(other.p1.y,other.p2.y);
- return mx2<=Mx1 && mx1<=Mx2 && my2<=My1 && my1<=My2;
- }
- public Point getIntersect(Segment other)
- {
- double x1 = p1.x;
- double x2 = p2.x;
- double x3 = other.p1.x;
- double x4 = other.p2.x;
- double y1 = p1.y;
- double y2 = p2.y;
- double y3 = other.p1.y;
- double y4 = other.p2.y;
- double d = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4);
- if (d == 0) return null;
- double xi = ((x3-x4)*(x1*y2-y1*x2)-(x1-x2)*(x3*y4-y3*x4))/d;
- double yi = ((y3-y4)*(x1*y2-y1*x2)-(y1-y2)*(x3*y4-y3*x4))/d;
- Point p = new Point(xi,yi);
- if (xi < Math.min(x1,x2) || xi > Math.max(x1,x2))
- return null;
- if (xi < Math.min(x3,x4) || xi > Math.max(x3,x4))
- return null;
- return p;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement