SHARE
TWEET

Intersection

project_number_03 Jan 25th, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.         System.out.println(isIntersect(1, 1, 5, 5, 1, 2, 3, 1)); //пересекаются
  5.         System.out.println(isIntersect(1, 1, 5, 5, 1, 2, 1, 3)); //не пересекаются
  6.     }
  7.  
  8.     public static boolean isIntersect(double ax1, double ay1, double ax2, double ay2, double bx1, double by1, double bx2, double by2) {
  9.         double v1 = (bx2 - bx1) * (ay1 - by1) - (by2 - by1) * (ax1 - bx1);
  10.         double v2 = (bx2 - bx1) * (ay2 - by1) - (by2 - by1) * (ax2 - bx1);
  11.         double v3 = (ax2 - ax1) * (by1 - ay1) - (ay2 - ay1) * (bx1 - ax1);
  12.         double v4 = (ax2 - ax1) * (by2 - ay1) - (ay2 - ay1) * (bx2 - ax1);
  13.  
  14.         return (v1 * v2 < 0) && (v3 * v4 < 0);
  15.     }
  16. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top