@Override public boolean intersects(Rectangle r) { GeometryFactory gf = new GeometryFactory(); GeometricShapeFactory f = new GeometricShapeFactory(gf); f.setBase(new Coordinate(r.x1(), r.y1())); f.setWidth(r.x2() - r.x1()); f.setHeight(r.y2() - r.y1()); Polygon rect = f.createRectangle(); LineSegment line = new LineSegment(x1, y1, x2, y2); return RectangleIntersects.intersects(rect, line.toGeometry(gf)); }
@Override public double distance(Rectangle r) { if (r.contains(x1, y1) || r.contains(x2, y2)) { return 0; } else { double d1 = distance(r.x1(), r.y1(), r.x1(), r.y2()); if (d1 == 0) return 0; double d2 = distance(r.x1(), r.y2(), r.x2(), r.y2()); if (d2 == 0) return 0; double d3 = distance(r.x2(), r.y2(), r.x2(), r.y1()); double d4 = distance(r.x2(), r.y1(), r.x1(), r.y1()); return Math.min(d1, Math.min(d2, Math.min(d3, d4))); } }