Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

36 ellipse bounding box #95

Merged
merged 5 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/AEG/AEGTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export class AEGTree {
* @returns True, if the node can be inserted. Else, false
*/
public canInsert(incomingNode: AtomNode | CutNode): boolean {
console.log("checking can insert");
const currentCut: CutNode = this.sheet.getCurrentCut(incomingNode);
for (let i = 0; i < currentCut.children.length; i++) {
if (this.overlaps(incomingNode, currentCut.children[i])) {
Expand All @@ -82,8 +81,6 @@ export class AEGTree {
}

const currentCut: CutNode = this.sheet.getCurrentCut(incomingNode);
//const originalChildren: (AtomNode | CutNode)[] = currentCut.children;
//==============CHANGEDDDD=========
const originalChildren: (AtomNode | CutNode)[] = [...currentCut.children];
currentCut.children.push(incomingNode);

Expand Down
3 changes: 0 additions & 3 deletions src/AEG/CutNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export class CutNode {
for (let i = 0; i < this.children.length; i++) {
const child: CutNode | AtomNode = this.children[i];
if (child instanceof CutNode && this.children[i].containsNode(newNode)) {
//======DEBUGGG=======
console.log("current cut: " + this.children[i]);
//newNode can be placed at least one layer deeper
return child.getCurrentCut(newNode);
}
Expand Down Expand Up @@ -77,7 +75,6 @@ export class CutNode {
if (otherNode instanceof AtomNode) {
return this.ellipse.containsShape(otherNode.rect);
} else if (otherNode instanceof CutNode) {
//ELLIPSE TO BE IMPLEMENTED ACCURATELY
return this.ellipse.containsShape(otherNode.ellipse as Ellipse);
} else {
throw Error("containsNode expected AtomNode or CutNode");
Expand Down
101 changes: 20 additions & 81 deletions src/AEG/Ellipse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,12 @@ export class Ellipse {
//(x, y) = new point
//(h, k) = center

const p: number = Math.round(
const p: number = Math.ceil(
Math.pow(otherPoint.x - this.center.x, 2) / Math.pow(this.radiusX, 2) +
Math.pow(otherPoint.y - this.center.y, 2) / Math.pow(this.radiusY, 2)
);

return p <= 1;

//Method 2: scaling eclipse to check for containment
/* const scale_y = this.radiusX / this.radiusY;
const dx = otherPoint.x - this.center.x;
const dy = (otherPoint.y - this.center.y) * scale_y;
return Math.pow(dx, 2) + Math.pow(dy, 2) <= Math.pow(this.radiusX, 2); */
}

/**
Expand All @@ -98,7 +91,6 @@ export class Ellipse {
* @returns True, if there is an overlap. Else, false.
*/
public overlaps(otherShape: Rectangle | Ellipse): boolean {
//return this.boundingBox.overlaps(otherShape);
if (otherShape instanceof Rectangle) {
for (let i = 0; i < 4; i++) {
if (this.containsPoint(otherShape.getCorners()[i])) {
Expand All @@ -110,12 +102,17 @@ export class Ellipse {
} else {
//check if the rectangular bounding boxes of the ellipse overlap
if (this.boundingBox.overlaps((otherShape as Ellipse).boundingBox)) {
console.log("ellipse boxes overlap");
return true;
//return true;
//if there is an overlap, check if points along the ellipse curve overlap
//this can be done by checking if points along the curve of the other ellipse
//are within this ellipse
//return this.checkQuadrantOverlap(otherShape);

const otherPoints: Point[] = otherShape.getEllipsePoints();
for (let i = 0; i < otherPoints.length; i++) {
if (this.containsPoint(otherPoints[i])) {
return true;
}
}
}
return false;
}
Expand Down Expand Up @@ -148,35 +145,6 @@ export class Ellipse {
throw Error("Invalid Shape passed to containsShape, must be a Rectangle | Ellipse");
}

/**
* Method that checks if any quadrant of another ellipse overlaps with this ellipse.
* This can be done by checking if a point on the curve of the ellipse is within this ellipse.
* @param otherEllipse The other ellipse that might be overlapping with this ellipse
* @returns True, if there is an overlap. Else, false
*/
private checkQuadrantOverlap(otherEllipse: Ellipse): boolean {
//Get the quadrant which might be overlapping with this ellipse.
//To do so, check which corner of the rectangular bounding box of the other ellipse
//is within this ellipse.
for (let i = 0; i < 4; i++) {
if (this.containsPoint(otherEllipse.boundingBox.getCorners()[i])) {
//Get the points on the curve of the ellipse in that quadrant
const points: Point[] = otherEllipse.getQuadrantPoints(i);

console.log("has corner " + i);
//If any points along the curve are within this ellipse, the other ellipse overlaps
//with this ellipse. Return true.
for (let j = 0; j < 6; j++) {
if (this.containsPoint(points[j])) {
console.log("Has overlap");
return true;
}
}
}
}
return false;
}

/**
* An array containing the widest coordinates of the ellipse, i.e. the coordinates along the
* x-axis and y-axis of the ellipse.
Expand All @@ -196,54 +164,25 @@ export class Ellipse {
];
}

/**
* Method that returns the points on the curve of the ellipse in a specific quadrant
* @param quadrant The quadrant which we want the points in
* @returns An array of points along the curve of the ellipse
*/
private getQuadrantPoints(quadrant: number): Point[] {
//==========DEBUGGG=========
console.log("Getting points for: " + this + "\n" + "In quad: " + quadrant);

private getEllipsePoints(): Point[] {
const points: Point[] = [];
const quadDistance = this.radiusX;
let curve = 1;

if (quadrant === 0) {
//top left quadrant
points[0] = this.getWidestCoordinates()[3];
points[1] = this.getWidestCoordinates()[0];
} else if (quadrant === 1) {
//top right quadrant
points[0] = this.getWidestCoordinates()[0];
points[1] = this.getWidestCoordinates()[1];
} else if (quadrant === 2) {
//bottom right quadrant
points[0] = this.getWidestCoordinates()[1];
points[1] = this.getWidestCoordinates()[2];
const pointDist = this.radiusX / 15;

curve = -1;
} else if (quadrant === 3) {
//bottom left quadrant
points[0] = this.getWidestCoordinates()[2];
points[1] = this.getWidestCoordinates()[3];

curve = -1;
}
points[0] = this.getWidestCoordinates()[3];
let x: number;
let y: number;

for (let i = 2; i < 6; i++) {
let x = points[0].x;
if (curve === 1) {
x = x + (i - 1) * (quadDistance / 5);
for (let i = 1; i < 64; i++) {
if (i < 33) {
x = points[i - 1].x + pointDist;
y = this.getCurvePoint(x, 1);
} else {
x = x - (i - 1) * (quadDistance / 5);
x = points[i - 1].x - pointDist;
y = this.getCurvePoint(x, -1);
}
const y = this.getCurvePoint(x, curve);
points[i] = new Point(x, y);
}

//========DEBUGGGG========
console.log(points);
return points;
}

Expand Down
2 changes: 0 additions & 2 deletions src/AEG/Rectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ export class Rectangle {
}
return false;
} else {
//const ellipseBoundary = (otherShape as Ellipse).boundingBox;
//return this.overlaps(ellipseBoundary);
for (let i = 0; i < 4; i++) {
if ((otherShape as Ellipse).containsPoint(this.getCorners()[i])) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/AtomCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function moveAtom(event: MouseEvent) {
function atomUp() {
const atomMetrics: TextMetrics = ctx.measureText(atom);
const newRect: Rectangle = new Rectangle(
new Point(currentPoint.x, currentPoint.y + atomMetrics.actualBoundingBoxAscent),
new Point(currentPoint.x, currentPoint.y - atomMetrics.actualBoundingBoxAscent),
atomMetrics.width,
atomMetrics.fontBoundingBoxDescent + atomMetrics.actualBoundingBoxAscent
);
Expand Down