Skip to content

Commit

Permalink
rectangle overlap when no corners within bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AnushaTiwari5 committed Oct 9, 2023
1 parent 09b748d commit 7f57f62
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/AEG/Ellipse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Point} from "./Point";
import {Rectangle} from "./Rectangle";
//import {Polynomial, polynomialRoots} from "nomial";

/**
* Class that defines an Ellipse.
Expand Down
60 changes: 49 additions & 11 deletions src/AEG/Rectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ export class Rectangle {
*/
public overlaps(otherShape: Rectangle | Ellipse): boolean {
if (otherShape instanceof Rectangle) {
const thisCorners = this.getCorners();
const otherCorners = otherShape.getCorners();

//Overlap occurs if either of the corners of either shape are within the other
for (let i = 0; i < 4; i++) {
if (
this.containsPoint(otherCorners[i]) ||
otherShape.containsPoint(thisCorners[i])
) {
return true;
}
if (
this.checkHorizontalEdgeOverlap(otherShape) &&
otherShape.checkVerticalEdgeOverlap(this)
) {
return true;
} else if (
this.checkVerticalEdgeOverlap(otherShape) &&
otherShape.checkHorizontalEdgeOverlap(this)
) {
return true;
}

return false;
} else {
for (let i = 0; i < 4; i++) {
Expand All @@ -102,6 +102,44 @@ export class Rectangle {
}
}

/**
* Checks if any of the horizontal edges of the other rectangle lie within the horizontal
* boundaries of this rectangle
* @param otherRect The other rectangle
* @returns True, if the other edges lie within this boundary. Else, false
*/
private checkHorizontalEdgeOverlap(otherRect: Rectangle): boolean {
const thisCorners = this.getCorners();
const otherCorners = otherRect.getCorners();

if (thisCorners[0].y <= otherCorners[0].y && thisCorners[2].y >= otherCorners[0].y) {
return true;
} else if (thisCorners[0].y <= otherCorners[2].y && thisCorners[2].y >= otherCorners[2].y) {
return true;
}

return false;
}

/**
* Checks if any of the vertical edges of the other rectangle lie within the vertical
* boundaries of this rectangle
* @param otherRect The other rectangle
* @returns True, if the other edges lie within this boundary. Else, false
*/
private checkVerticalEdgeOverlap(otherRect: Rectangle): boolean {
const thisCorners = this.getCorners();
const otherCorners = otherRect.getCorners();

if (thisCorners[0].x <= otherCorners[0].x && thisCorners[1].x >= otherCorners[0].x) {
return true;
} else if (thisCorners[0].x <= otherCorners[1].x && thisCorners[1].x >= otherCorners[1].x) {
return true;
}

return false;
}

/**
* Method that checks whether another shape is within this rectangle.
* @param otherShape The shape that might be within this rectangle.
Expand Down

0 comments on commit 7f57f62

Please sign in to comment.