Skip to content

Commit

Permalink
simplied edge intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
AnushaTiwari5 committed Oct 12, 2023
1 parent 2a7f01d commit dd9ef58
Showing 1 changed file with 18 additions and 133 deletions.
151 changes: 18 additions & 133 deletions src/AEG/AEGUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function shapesOverlap(
if (newShape instanceof Rectangle) {
if (existingShape instanceof Rectangle) {
//For rectangle-rectangle, check if their edges intersect
return edgesIntersect(newShape, existingShape);
return edgesWithin(newShape, existingShape) || edgesWithin(existingShape, newShape);
} else {
return ellipseRectangleCollision(existingShape as Ellipse, newShape as Rectangle);
}
Expand Down Expand Up @@ -121,139 +121,24 @@ export function shapeContains(
}

/**
* Method that checks if any edges of this rectangle overlap with the other rectangle.
* @param otherRect The other rectangle to be checked.
* Method that checks if any edges of a rectangle are within the other rectangle.
* @param rect1 The incoming rectangle.
* @param rect2 The existing rectangle.
* @returns True, if edges overlap. Else, false.
* @todo This algo can and should be simplified to be less than 10 lines of code. -James-Oswald
*/
function edgesIntersect(shape1: Rectangle, shape2: Rectangle): boolean {
const thisCorners = shape1.getCorners();
const otherCorners = shape2.getCorners();

if (thisCorners[0].y <= otherCorners[0].y && thisCorners[2].y >= otherCorners[0].y) {
//The top edge of the other rectangle is within the horizontal boundaries
//of this rectangle
if (thisCorners[0].x <= otherCorners[0].x && thisCorners[1].x >= otherCorners[0].x) {
//The left edge of the other rectangle is within the vertical boundaries
//of this rectangle
return true;
} else if (
//The left edge of this rectangle is within the vertical boundaries
//of the other rectangle
otherCorners[0].x <= thisCorners[0].x &&
otherCorners[1].x >= thisCorners[0].x
) {
return true;
} else if (
//The right edge of the other rectangle is within the vertical boundaries
//of this rectangle
thisCorners[0].x <= otherCorners[1].x &&
thisCorners[1].x >= otherCorners[1].x
) {
return true;
} else if (
//The right edge of this rectangle is within the vertical boundaries
//of the other rectangle
otherCorners[0].x <= thisCorners[1].x &&
otherCorners[1].x >= thisCorners[1].x
) {
return true;
}

return false;
} else if (otherCorners[0].y <= thisCorners[0].y && otherCorners[2].y >= thisCorners[0].y) {
//The top edge of this rectangle is within the horizontal boundaries
//of the other rectangle
if (thisCorners[0].x <= otherCorners[0].x && thisCorners[1].x >= otherCorners[0].x) {
//The left edge of the other rectangle is within the vertical boundaries
//of this rectangle
return true;
} else if (
//The left edge of this rectangle is within the vertical boundaries
//of the other rectangle
otherCorners[0].x <= thisCorners[0].x &&
otherCorners[1].x >= thisCorners[0].x
) {
return true;
} else if (
//The right edge of the other rectangle is within the vertical boundaries
//of this rectangle
thisCorners[0].x <= otherCorners[1].x &&
thisCorners[1].x >= otherCorners[1].x
) {
return true;
} else if (
//The right edge of this rectangle is within the vertical boundaries
//of the other rectangle
otherCorners[0].x <= thisCorners[1].x &&
otherCorners[1].x >= thisCorners[1].x
) {
return true;
}

return false;
} else if (thisCorners[0].y <= otherCorners[2].y && thisCorners[2].y >= otherCorners[2].y) {
//The bottom edge of the other rectangle is within the horizontal boundaries
//of this rectangle
if (thisCorners[0].x <= otherCorners[0].x && thisCorners[1].x >= otherCorners[0].x) {
//The left edge of the other rectangle is within the vertical boundaries
//of this rectangle
return true;
} else if (
//The left edge of this rectangle is within the vertical boundaries
//of the other rectangle
otherCorners[0].x <= thisCorners[0].x &&
otherCorners[1].x >= thisCorners[0].x
) {
return true;
} else if (
//The right edge of the other rectangle is within the vertical boundaries
//of this rectangle
thisCorners[0].x <= otherCorners[1].x &&
thisCorners[1].x >= otherCorners[1].x
) {
return true;
} else if (
//The right edge of this rectangle is within the vertical boundaries
//of the other rectangle
otherCorners[0].x <= thisCorners[1].x &&
otherCorners[1].x >= thisCorners[1].x
) {
return true;
}

return false;
} else if (otherCorners[0].y <= thisCorners[2].y && otherCorners[2].y >= thisCorners[2].y) {
//The bottom edge of this rectangle is within the horizontal boundaries
//of the other rectangle
if (thisCorners[0].x <= otherCorners[0].x && thisCorners[1].x >= otherCorners[0].x) {
//The left edge of the other rectangle is within the vertical boundaries
//of this rectangle
return true;
} else if (
//The left edge of this rectangle is within the vertical boundaries
//of the other rectangle
otherCorners[0].x <= thisCorners[0].x &&
otherCorners[1].x >= thisCorners[0].x
) {
return true;
} else if (
//The right edge of the other rectangle is within the vertical boundaries
//of this rectangle
thisCorners[0].x <= otherCorners[1].x &&
thisCorners[1].x >= otherCorners[1].x
) {
return true;
} else if (
//The right edge of this rectangle is within the vertical boundaries
//of the other rectangle
otherCorners[0].x <= thisCorners[1].x &&
otherCorners[1].x >= thisCorners[1].x
) {
return true;
}

return false;
function edgesWithin(rect1: Rectangle, rect2: Rectangle): boolean {
const corners1 = rect1.getCorners();
const corners2 = rect2.getCorners();

if (
//Check if the horizontal edges are within
((corners1[0].y <= corners2[0].y && corners1[2].y >= corners2[0].y) ||
(corners1[0].y <= corners2[2].y && corners1[2].y >= corners2[2].y)) &&
//Check if the vertical edges are within
((corners1[0].x <= corners2[0].x && corners1[1].x >= corners2[0].x) ||
(corners1[0].x <= corners2[1].x && corners1[1].x >= corners2[1].x))
) {
return true;
}

return false;
Expand Down Expand Up @@ -292,7 +177,7 @@ export function pointInEllipse(ellipse: Ellipse, point: Point): boolean {
Math.pow(point.y - ellipse.center.y, 2) / Math.pow(ellipse.radiusY, 2);
//);

return p < 1;
return p <= 1;
}

/**
Expand Down

0 comments on commit dd9ef58

Please sign in to comment.