Advertisement
project_number_03

Intersection

Jan 25th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement