Java Rectangle Intersect intersects(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)

Here you can find the source of intersects(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)

Description

Tests four points to see if a line from the first to the second would intersect a line from the third to the fourth.

License

Open Source License

Parameter

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

Return

true if the segments intersect, false otherwise

Declaration

public static boolean intersects(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
        double y4) 

Method Source Code

//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);
    }
}

Related

  1. intersection(Line2D.Double line, Rectangle2D.Double bounds)
  2. intersection(Rectangle r1, Rectangle r2, Rectangle out)
  3. intersection(Rectangle rectangle, Rectangle rectangle1, Rectangle rectangle2)
  4. intersectRect(double x1, double y1, double w1, double h1, double x2, double y2, double w2, double h2)
  5. intersects(double oldx, double oldy, double oldwidth, double oldheight, double oldx2, double oldy2, double oldwidth2, double oldheight2)
  6. intersects(final float x, final float y, final float z, final int sx, final int sy, final int sz)
  7. intersects(final Rectangle elementRect, final Rectangle viewportRect)
  8. intersects(final Rectangle2D a, final Rectangle2D b)
  9. intersects(float cx, float cy, float radius, float left, float top, float right, float bottom)