I would like some help checking whether two rectangles overlap by using Java. There are formulas like this formula (0<AM⋅AB<AB⋅AB)∧(0<AM⋅AD<AD⋅AD)
that can check if a point is within a rotated rectangle; I want to use two points of a rectangle (top-left and bottom-right, although they may be fully rotated to be top-right and bottom-left) to see if it overlaps with another rectangle made up from two points.
Edit - This was marked as a duplicate of this, which is good, but I would also like to be using doubles as points on a grid which is not supported by java.awt.Polygon
Since this is still marked as a duplicate, I'll put my answer here:
What I did is I used the Polygon2D class from here along with PolygonIterator to get full functionality from the first class, then I added the following method to the internal class, Double
, within Polygon2D
/**
* Get all points of the polygon
*/
public CustomPoint[] getPoints() {
int c = getVertexCount();
CustomPoint[] cp = new CustomPoint[c];
for (int i = 0; i < c; i++)
cp[i] = new CustomPoint(getX(i), getY(i));
return cp;
}
Then, I made the CustomPoint
class to support functionality with doubles
public class CustomPoint {
public double x = 0;
public double y = 0;
public CustomPoint(double x, double y) {
this.x = x;
this.y = y;
}
public double[] getLocation() {
return new double[] { x, y };
}
public void setLocation(double x, double y) {
this.x = x;
this.y = y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
}
I later turned all my points into an array of doubles and converted it into a Polygon that allows for doubles after that, I edited the method given for checking whether two polygons were overlapping
public boolean isPolygonsIntersecting(Polygon2D.Double a, Polygon2D.Double b) {
for (int x = 0; x < 2; x++) {
Polygon2D.Double polygon = x == 0 ? a : b;
for (int i1 = 0; i1 < polygon.getPoints().length; i1++) {
int i2 = (i1 + 1) % polygon.getPoints().length;
CustomPoint p1 = polygon.getPoints()[i1];
CustomPoint p2 = polygon.getPoints()[i2];
CustomPoint normal = new CustomPoint(p2.y - p1.y, p1.x - p2.x);
double minA = Double.POSITIVE_INFINITY;
double maxA = Double.NEGATIVE_INFINITY;
for (CustomPoint p : a.getPoints()) {
double projected = normal.x * p.x + normal.y * p.y;
if (projected < minA)
minA = projected;
if (projected > maxA)
maxA = projected;
}
double minB = Double.POSITIVE_INFINITY;
double maxB = Double.NEGATIVE_INFINITY;
for (CustomPoint p : b.getPoints()) {
double projected = normal.x * p.x + normal.y * p.y;
if (projected < minB)
minB = projected;
if (projected > maxB)
maxB = projected;
}
if (maxA < minB || maxB < minA)
return false;
}
}
return true;
}
And so after doing all of that, I had tested and confirmed that it does indeed now work with doubles. Sure hope this helps anyone who had the same problem. Please also note that the Polygon2D and PolygonIterator classes are under copyright by "The Regents of the University of California" and are being used here for non-distributed code.