HexRect2D { public boolean isLineIntersects(float x0, float y0, float x1, float y1) { final int posOfSecPoint; if ((posOfSecPoint = checkRelativePosition(x1, y1)) == 0) { return true; } while (true) { final int posOfFrstPoint = checkRelativePosition(x0, y0); if (posOfFrstPoint == 0) { break; } if ((posOfFrstPoint & posOfSecPoint) != 0) { return false; } if ((posOfFrstPoint & (POS_LEFT | POS_RIGHT)) != 0) { float px = this.left; if ((posOfFrstPoint & POS_RIGHT) != 0) { px += getWidth(); } y0 = y0 + (px - x0) * (y1 - y0) / (x1 - x0); x0 = px; } else { float py = this.top; if ((posOfFrstPoint & POS_BOTTOM) != 0) { py += getHeight(); } x0 = x0 + (py - y0) * (x1 - x0) / (y1 - y0); y0 = py; } } return true; } HexRect2D(final float x, final float y, final float width, final float height); int getLeftAsInt(); float getLeft(); float getTop(); int getTopAsInt(); float getRight(); int getRightAsInt(); float getBottom(); int getBottomAsInt(); float getWidth(); int getWidthAsInt(); float getHeight(); int getHeightAsInt(); float getCenterX(); float getCenterY(); HexPoint2D getCenter(); boolean isPointInside(final HexPoint2D p); int checkRelativePosition(final float x, final float y); boolean isLineIntersects(float x0, float y0, float x1, float y1); static final int POS_LEFT; static final int POS_TOP; static final int POS_RIGHT; static final int POS_BOTTOM; }
@Test(timeout = 1000L) public void testIsLineIntersects() { final HexRect2D rect = new HexRect2D(574.0f, 751.0f, 240.0f, 282.0f); rect.isLineIntersects(814.0f, 768.0f, 800.0f, 736.0f); }