@Test public void intersectsCircleTest() { Rectangle rectangle1 = create(1, 2, 3, 4); Assert.assertTrue(rectangle1.intersectsCircle(1, 2, 0)); Assert.assertTrue(rectangle1.intersectsCircle(1, 2, 1)); Assert.assertTrue(rectangle1.intersectsCircle(1, 2, 10)); Assert.assertTrue(rectangle1.intersectsCircle(2, 3, 0)); Assert.assertTrue(rectangle1.intersectsCircle(2, 3, 1)); Assert.assertTrue(rectangle1.intersectsCircle(2, 3, 10)); Assert.assertTrue(rectangle1.intersectsCircle(3.5, 4, 0.5)); Assert.assertTrue(rectangle1.intersectsCircle(3, 4.5, 0.5)); Assert.assertTrue(rectangle1.intersectsCircle(4, 4, 1)); Assert.assertTrue(rectangle1.intersectsCircle(4, 4, 10)); Assert.assertFalse(rectangle1.intersectsCircle(0, 0, 0)); Assert.assertFalse(rectangle1.intersectsCircle(0, 1, 0)); Assert.assertFalse(rectangle1.intersectsCircle(0, 1, 1)); Assert.assertFalse(rectangle1.intersectsCircle(3.5, 4, 0.49999)); Assert.assertFalse(rectangle1.intersectsCircle(3, 4.5, 0.49999)); Assert.assertFalse(rectangle1.intersectsCircle(4, 5, 1)); Assert.assertFalse(rectangle1.intersectsCircle(4, 5, 1.4)); }
public boolean intersectsCircle(double pointX, double pointY, double radius) { double halfWidth = getWidth() / 2; double halfHeight = getHeight() / 2; double centerDistanceX = Math.abs(pointX - getCenterX()); double centerDistanceY = Math.abs(pointY - getCenterY()); if (centerDistanceX > halfWidth + radius) { return false; } else if (centerDistanceY > halfHeight + radius) { return false; } if (centerDistanceX <= halfWidth) { return true; } else if (centerDistanceY <= halfHeight) { return true; } double cornerDistanceX = centerDistanceX - halfWidth; double cornerDistanceY = centerDistanceY - halfHeight; return cornerDistanceX * cornerDistanceX + cornerDistanceY * cornerDistanceY <= radius * radius; }
Rectangle implements Serializable { public boolean intersectsCircle(double pointX, double pointY, double radius) { double halfWidth = getWidth() / 2; double halfHeight = getHeight() / 2; double centerDistanceX = Math.abs(pointX - getCenterX()); double centerDistanceY = Math.abs(pointY - getCenterY()); if (centerDistanceX > halfWidth + radius) { return false; } else if (centerDistanceY > halfHeight + radius) { return false; } if (centerDistanceX <= halfWidth) { return true; } else if (centerDistanceY <= halfHeight) { return true; } double cornerDistanceX = centerDistanceX - halfWidth; double cornerDistanceY = centerDistanceY - halfHeight; return cornerDistanceX * cornerDistanceX + cornerDistanceY * cornerDistanceY <= radius * radius; } }
Rectangle implements Serializable { public boolean intersectsCircle(double pointX, double pointY, double radius) { double halfWidth = getWidth() / 2; double halfHeight = getHeight() / 2; double centerDistanceX = Math.abs(pointX - getCenterX()); double centerDistanceY = Math.abs(pointY - getCenterY()); if (centerDistanceX > halfWidth + radius) { return false; } else if (centerDistanceY > halfHeight + radius) { return false; } if (centerDistanceX <= halfWidth) { return true; } else if (centerDistanceY <= halfHeight) { return true; } double cornerDistanceX = centerDistanceX - halfWidth; double cornerDistanceY = centerDistanceY - halfHeight; return cornerDistanceX * cornerDistanceX + cornerDistanceY * cornerDistanceY <= radius * radius; } Rectangle(double left, double top, double right, double bottom); }
Rectangle implements Serializable { public boolean intersectsCircle(double pointX, double pointY, double radius) { double halfWidth = getWidth() / 2; double halfHeight = getHeight() / 2; double centerDistanceX = Math.abs(pointX - getCenterX()); double centerDistanceY = Math.abs(pointY - getCenterY()); if (centerDistanceX > halfWidth + radius) { return false; } else if (centerDistanceY > halfHeight + radius) { return false; } if (centerDistanceX <= halfWidth) { return true; } else if (centerDistanceY <= halfHeight) { return true; } double cornerDistanceX = centerDistanceX - halfWidth; double cornerDistanceY = centerDistanceY - halfHeight; return cornerDistanceX * cornerDistanceX + cornerDistanceY * cornerDistanceY <= radius * radius; } Rectangle(double left, double top, double right, double bottom); boolean contains(Point point); Rectangle enlarge(double left, double top, double right, double bottom); Rectangle envelope(double padding); @Override boolean equals(Object obj); Point getCenter(); double getCenterX(); double getCenterY(); double getHeight(); double getWidth(); @Override int hashCode(); boolean intersects(Rectangle rectangle); boolean intersectsCircle(double pointX, double pointY, double radius); Rectangle shift(Point origin); @Override String toString(); }
Rectangle implements Serializable { public boolean intersectsCircle(double pointX, double pointY, double radius) { double halfWidth = getWidth() / 2; double halfHeight = getHeight() / 2; double centerDistanceX = Math.abs(pointX - getCenterX()); double centerDistanceY = Math.abs(pointY - getCenterY()); if (centerDistanceX > halfWidth + radius) { return false; } else if (centerDistanceY > halfHeight + radius) { return false; } if (centerDistanceX <= halfWidth) { return true; } else if (centerDistanceY <= halfHeight) { return true; } double cornerDistanceX = centerDistanceX - halfWidth; double cornerDistanceY = centerDistanceY - halfHeight; return cornerDistanceX * cornerDistanceX + cornerDistanceY * cornerDistanceY <= radius * radius; } Rectangle(double left, double top, double right, double bottom); boolean contains(Point point); Rectangle enlarge(double left, double top, double right, double bottom); Rectangle envelope(double padding); @Override boolean equals(Object obj); Point getCenter(); double getCenterX(); double getCenterY(); double getHeight(); double getWidth(); @Override int hashCode(); boolean intersects(Rectangle rectangle); boolean intersectsCircle(double pointX, double pointY, double radius); Rectangle shift(Point origin); @Override String toString(); final double bottom; final double left; final double right; final double top; }