Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1.     public boolean checkIntersect(Segment other)
  2.     {
  3.         double mx1 = Math.min(p1.x,p2.x);
  4.         double Mx1 = Math.max(p1.x,p2.x);
  5.         double mx2 = Math.min(other.p1.x,other.p2.x);
  6.         double Mx2 = Math.max(other.p1.x,other.p2.x);
  7.         double my1 = Math.min(p1.y,p2.y);
  8.         double My1 = Math.max(p1.y,p2.y);
  9.         double my2 = Math.min(other.p1.y,other.p2.y);
  10.         double My2 = Math.max(other.p1.y,other.p2.y);
  11.        
  12.         return mx2<=Mx1 && mx1<=Mx2 && my2<=My1 && my1<=My2;
  13.     }
  14.    
  15.    
  16.     public Point getIntersect(Segment other)
  17.     {
  18.         double x1 = p1.x;
  19.         double x2 = p2.x;
  20.         double x3 = other.p1.x;
  21.         double x4 = other.p2.x;
  22.         double y1 = p1.y;
  23.         double y2 = p2.y;
  24.         double y3 = other.p1.y;
  25.         double y4 = other.p2.y;
  26.        
  27.         double d = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4);
  28.         if (d == 0) return null;
  29.        
  30.         double xi = ((x3-x4)*(x1*y2-y1*x2)-(x1-x2)*(x3*y4-y3*x4))/d;
  31.         double yi = ((y3-y4)*(x1*y2-y1*x2)-(y1-y2)*(x3*y4-y3*x4))/d;
  32.        
  33.         Point p = new Point(xi,yi);
  34.        
  35.         if (xi < Math.min(x1,x2) || xi > Math.max(x1,x2))
  36.             return null;
  37.        
  38.         if (xi < Math.min(x3,x4) || xi > Math.max(x3,x4))
  39.             return null;
  40.        
  41.         return p;
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement