From 28fb8be431f6b25fab000d522a74b7c96200a0c2 Mon Sep 17 00:00:00 2001 From: AnushaTiwari5 Date: Sun, 8 Oct 2023 22:00:02 -0400 Subject: [PATCH 1/4] checking ellipse overlaps by directly checking points on curve --- src/AEG/AEGTree.ts | 3 -- src/AEG/CutNode.ts | 3 -- src/AEG/Ellipse.ts | 110 ++++++++++++++------------------------------- 3 files changed, 34 insertions(+), 82 deletions(-) diff --git a/src/AEG/AEGTree.ts b/src/AEG/AEGTree.ts index e4c8a431..287ed7de 100644 --- a/src/AEG/AEGTree.ts +++ b/src/AEG/AEGTree.ts @@ -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])) { @@ -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); diff --git a/src/AEG/CutNode.ts b/src/AEG/CutNode.ts index fd6cd83c..686632dc 100644 --- a/src/AEG/CutNode.ts +++ b/src/AEG/CutNode.ts @@ -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); } @@ -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"); diff --git a/src/AEG/Ellipse.ts b/src/AEG/Ellipse.ts index bb872bbd..d3fdd4a4 100644 --- a/src/AEG/Ellipse.ts +++ b/src/AEG/Ellipse.ts @@ -110,12 +110,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; } @@ -128,12 +133,19 @@ export class Ellipse { */ public containsShape(otherShape: Rectangle | Ellipse): boolean { if (otherShape instanceof Rectangle) { - for (let i = 0; i < 4; i++) { + /* for (let i = 0; i < 4; i++) { if (!this.containsPoint(otherShape.getCorners()[i])) { return false; } } - return true; + return true; */ + + //If the 2 opposite corners of the rectangle are within this ellipse, + //The rectangle is contained within this ellipse + return ( + this.containsPoint(otherShape.getCorners()[0]) && + this.containsPoint(otherShape.getCorners()[2]) + ); } else if (otherShape instanceof Ellipse) { //If all the widest coordinates of the other ellipse are within this ellipse, //This ellipse contains the other ellipse @@ -148,35 +160,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. @@ -196,54 +179,29 @@ 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]; - - curve = -1; - } else if (quadrant === 3) { - //bottom left quadrant - points[0] = this.getWidestCoordinates()[2]; - points[1] = this.getWidestCoordinates()[3]; - - curve = -1; - } - - for (let i = 2; i < 6; i++) { - let x = points[0].x; - if (curve === 1) { - x = x + (i - 1) * (quadDistance / 5); + const pointDist = this.radiusX / 9; + //get widestCoord(3) + //pointDistance = radx/6 + //Get 5 points btwn each widest + //from 0 - 12, + pointDist + + points[0] = this.getWidestCoordinates()[3]; + let x: number; + let y: number; + + for (let i = 1; i < 40; i++) { + if (i < 20) { + 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; } From 90d4725990bfcbcdec6e33d9bf15e225b32bc845 Mon Sep 17 00:00:00 2001 From: AnushaTiwari5 Date: Sun, 8 Oct 2023 22:35:29 -0400 Subject: [PATCH 2/4] ellipse on ellipse fully working --- src/AEG/Ellipse.ts | 2 +- src/AtomCreation.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AEG/Ellipse.ts b/src/AEG/Ellipse.ts index d3fdd4a4..25d5f8da 100644 --- a/src/AEG/Ellipse.ts +++ b/src/AEG/Ellipse.ts @@ -77,7 +77,7 @@ 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) ); diff --git a/src/AtomCreation.ts b/src/AtomCreation.ts index 639b3a94..6a7cb51a 100644 --- a/src/AtomCreation.ts +++ b/src/AtomCreation.ts @@ -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 ); From 9da25afa359d9edfa3e3c75048571e861969655a Mon Sep 17 00:00:00 2001 From: AnushaTiwari5 Date: Sun, 8 Oct 2023 22:54:40 -0400 Subject: [PATCH 3/4] fixed contains atom bug, removed unecessary comment lines --- src/AEG/Ellipse.ts | 23 ++--------------------- src/AEG/Rectangle.ts | 2 -- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/src/AEG/Ellipse.ts b/src/AEG/Ellipse.ts index 25d5f8da..a9dde1a2 100644 --- a/src/AEG/Ellipse.ts +++ b/src/AEG/Ellipse.ts @@ -83,13 +83,6 @@ export class Ellipse { ); 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); */ } /** @@ -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])) { @@ -133,19 +125,12 @@ export class Ellipse { */ public containsShape(otherShape: Rectangle | Ellipse): boolean { if (otherShape instanceof Rectangle) { - /* for (let i = 0; i < 4; i++) { + for (let i = 0; i < 4; i++) { if (!this.containsPoint(otherShape.getCorners()[i])) { return false; } } - return true; */ - - //If the 2 opposite corners of the rectangle are within this ellipse, - //The rectangle is contained within this ellipse - return ( - this.containsPoint(otherShape.getCorners()[0]) && - this.containsPoint(otherShape.getCorners()[2]) - ); + return true; } else if (otherShape instanceof Ellipse) { //If all the widest coordinates of the other ellipse are within this ellipse, //This ellipse contains the other ellipse @@ -182,10 +167,6 @@ export class Ellipse { private getEllipsePoints(): Point[] { const points: Point[] = []; const pointDist = this.radiusX / 9; - //get widestCoord(3) - //pointDistance = radx/6 - //Get 5 points btwn each widest - //from 0 - 12, + pointDist points[0] = this.getWidestCoordinates()[3]; let x: number; diff --git a/src/AEG/Rectangle.ts b/src/AEG/Rectangle.ts index 37b6b2fb..b3bfeddf 100644 --- a/src/AEG/Rectangle.ts +++ b/src/AEG/Rectangle.ts @@ -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; From dd9bc8fee5bf327e0409fbb0208c2146cfc249f4 Mon Sep 17 00:00:00 2001 From: AnushaTiwari5 Date: Sun, 8 Oct 2023 23:00:59 -0400 Subject: [PATCH 4/4] increased points on ellipse --- src/AEG/Ellipse.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/AEG/Ellipse.ts b/src/AEG/Ellipse.ts index a9dde1a2..a8cdf883 100644 --- a/src/AEG/Ellipse.ts +++ b/src/AEG/Ellipse.ts @@ -166,14 +166,14 @@ export class Ellipse { private getEllipsePoints(): Point[] { const points: Point[] = []; - const pointDist = this.radiusX / 9; + const pointDist = this.radiusX / 15; points[0] = this.getWidestCoordinates()[3]; let x: number; let y: number; - for (let i = 1; i < 40; i++) { - if (i < 20) { + for (let i = 1; i < 64; i++) { + if (i < 33) { x = points[i - 1].x + pointDist; y = this.getCurvePoint(x, 1); } else {