Intersecting rectangles
In computer graphics, it is often necessary to check if two rectangles intersect. This can be used to determine if two objects are overlapping, or to find the intersection of two shapes.
There are several ways to check if two rectangles intersect. One common approach is to use the following steps:
- Find the nearest point to the origin of rectangle
r1. - Translate all points in the space so that the nearest point of
r1is at the origin. - Find the point with the smallest y-axis of
r1. - Find the slope of that point and the point at the origin.
- Rotate using a rotation matrix all the points in the space.
- Note the x-distance and y-distance of the other points of the rectangle
r1. - Check if any point of rectangle
r2is inside the rectangler1.
Here is an explanation of each step:
- The nearest point to the origin is the point that is closest to the origin. This can be found by using the
distance_from_origin()method. - Translating all points in the space so that the nearest point of
r1is at the origin means subtracting the coordinates of the nearest point from the coordinates of all points. This can be done by using the-=operator. - The point with the smallest y-axis is the point that has the smallest y-coordinate. This can be found by using the
min()function. - The slope of a line is the change in the y-coordinate divided by the change in the x-coordinate. In this case, the line is the line that goes through the nearest point of
r1and the origin. The slope can be found by using the(y2 - y1) / (x2 - x1)formula. - Rotating using a rotation matrix all the points in the space means multiplying the coordinates of all points by a matrix that represents a rotation. The rotation matrix can be calculated using the slope of the line found in the previous step.
- The x-distance and y-distance of a point are the distances between the point and the origin in the x-direction and y-direction, respectively. These can be found by using the
abs()function. - Checking if any point of rectangle
r2is inside the rectangler1means checking if the x-coordinates and y-coordinates of the point are within the range ofr1. This can be done by using the>==and<==operators.
Code in C# language
public class Rectangle {
public Point topLeft;
public int width;
public int height;
public Rectangle(Point topLeft, int width, int height) {
this.topLeft = topLeft;
this.width = width;
this.height = height;
}
public bool Intersects(Rectangle otherRectangle) {
// Find the nearest point to the origin of rectangle r1.
Point nearestPoint = this.topLeft;
for (Point point : this.GetPoints()) {
if (point.DistanceTo(Point.Origin) < nearestPoint.DistanceTo(Point.Origin)) {
nearestPoint = point;
}
}
// Translate all points in the space so that the nearest point of r1 is at the origin.
for (Point point : this.GetPoints()) {
point.x -= nearestPoint.x;
point.y -= nearestPoint.y;
}
// Find the point with the smallest y-axis of r1.
Point minYPoint = this.topLeft;
for (Point point : this.GetPoints()) {
if (point.y < minYPoint.y) {
minYPoint = point;
}
}
// Find the slope of that point and the point at the origin.
float slope = (minYPoint.y - nearestPoint.y) / (minYPoint.x - nearestPoint.x);
// Rotate using a rotation matrix all the points in the space.
Matrix rotationMatrix = new Matrix(new double[][] { { 1, 0 }, { 0, slope } });
for (Point point : this.GetPoints()) {
point.x, point.y = rotationMatrix.MultiplyPoint(new Point(point.x, point.y));
}
// Note the x-distance and y-distance of the other points of the rectangle r1.
int[] xDistances = this.GetXDistances();
int[] yDistances = this.GetYDistances();
// Check if any point of rectangle r2 is inside the rectangle r1.
for (Point point : otherRectangle.GetPoints()) {
if (point.x >= min(xDistances) && point.x <= max(xDistances) && point.y >= min(yDistances) && point.y <= max(yDistances)) {
return true;
}
}
return false;
}
private int[] GetXDistances() {
int[] xDistances = new int[this.GetPoints().Length];
for (int i = 0; i < this.GetPoints().Length; i++) {
xDistances[i] = this.GetPoints()[i].x;
}
return xDistances;
}
private int[] GetYDistances() {
int[] yDistances = new int[this.GetPoints().Length];
for (int i = 0; i < this.GetPoints().Length; i++) {
yDistances[i] = this.GetPoints()[i].y;
}
return yDistances;
}
private IEnumerable GetPoints() {
return new List { this.topLeft, new Point(this.topLeft.x + this.width, this.topLeft.y), new Point(this.topLeft.x + this.width, this.topLeft.y + this.height), new Point(this.topLeft.x, this.topLeft.y + this.height) };
}
}
Comments
Post a Comment