Here you can find the source of intersects(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
Parameter | Description |
---|---|
x1 | the x coordinate of the first endpoint of the first segment |
y1 | the y coordinate of the first endpoint of the first segment |
x2 | the x coordinate of the second endpoint of the first segment |
y2 | the y coordinate of the second endpoint of the first segment |
x3 | the x coordinate of the first endpoint of the second segment |
y3 | the y coordinate of the first endpoint of the second segment |
x4 | the x coordinate of the second endpoint of the second segment |
y4 | the y coordinate of the second endpoint of the second segment |
public static boolean intersects(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
//package com.java2s; public class Main { /**//from w ww. j a v a2 s.com * Tests four points to see if a line from the first to the second would * intersect a line from the third to the fourth. Stolen from * <a href="http://support.microsoft.com/support/kb/articles/Q121/9/60.asp">Microsoft</a> * @param x1 the x coordinate of the first endpoint of the first segment * @param y1 the y coordinate of the first endpoint of the first segment * @param x2 the x coordinate of the second endpoint of the first segment * @param y2 the y coordinate of the second endpoint of the first segment * @param x3 the x coordinate of the first endpoint of the second segment * @param y3 the y coordinate of the first endpoint of the second segment * @param x4 the x coordinate of the second endpoint of the second segment * @param y4 the y coordinate of the second endpoint of the second segment * @return true if the segments intersect, false otherwise */ public static boolean intersects(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { return (((x1 == x3) && (y1 == y3)) || ((x1 == x4) && (y1 == y4)) || ((x2 == x3) && (y2 == y3)) || ((x2 == x4) && (y2 == y4)) || (((CCW(x1, y1, x2, y2, x3, y3) * CCW(x1, y1, x2, y2, x4, y4)) <= 0) && ((CCW(x3, y3, x4, y4, x1, y1) * CCW(x3, y3, x4, y4, x2, y2)) <= 0))); } /** * Test three points to see if, in drawing a line from the first to the * second to the third, you would be moving counter-clockwise (hence CCW). * Stolen from <a href="http://support.microsoft.com/support/kb/articles/Q121/9/60.asp">Microsoft</a> * @param x1 the x coordinate of the first point * @param y1 the y coordinate of the first point * @param x2 the x coordinate of the second point * @param y2 the y coordinate of the second point * @param x3 the x coordinate of the third point * @param y3 the y coordinate of the third point * @return 1 if moving counter-clockwise, -1 if moving clockwise. */ private static int CCW(double x1, double y1, double x2, double y2, double x3, double y3) { double dx1 = x2 - x1; double dx2 = x3 - x1; double dy1 = y2 - y1; double dy2 = y3 - y1; return (((dx1 * dy2) > (dy1 * dx2)) ? 1 : -1); } }