Skip to content

Commit

Permalink
Ellipses!
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Oswald committed Oct 8, 2023
1 parent 87fa378 commit 3d8ce22
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "msedge",
"name": "Edge Vite Debug",
"request": "launch",
"url": "http://localhost:5173/",
"webRoot": "${workspaceFolder}/src"
},
{
"type": "chrome",
"request": "launch",
Expand Down
6 changes: 5 additions & 1 deletion src/AEG/AEGTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class AEGTree {
const currentCut: CutNode = this.sheet.getCurrentCut(incomingNode);
//const originalChildren: (AtomNode | CutNode)[] = currentCut.children;
//==============CHANGEDDDD=========
const originalChildren: (AtomNode | CutNode)[] = structuredClone(currentCut.children);
const originalChildren: (AtomNode | CutNode)[] = [...currentCut.children];
currentCut.children.push(incomingNode);

if (incomingNode instanceof CutNode) {
Expand Down Expand Up @@ -137,4 +137,8 @@ export class AEGTree {
}
}
}

public toString(): string {
return this.sheet.toFormulaString()
}
}
36 changes: 30 additions & 6 deletions src/AEG/CutNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {Point} from "./Point";
* Class that defines a Cut in AEGTree.
* @author Anusha Tiwari
* @author Ryan Reilly
* @author James Oswald
*/
export class CutNode {
/**
* The boundary of this node.
*/
ellipse: Ellipse | null; //Null for sheet
ellipse: Ellipse | null; //Null for sheet of assertion

/**
* Contains the list of child nodes nested within this node.
Expand All @@ -35,11 +36,12 @@ export class CutNode {
*/
public getCurrentCut(newNode: CutNode | AtomNode): CutNode {
for (let i = 0; i < this.children.length; i++) {
if (this.children[i] instanceof CutNode && this.children[i].containsNode(newNode)) {
let 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 this.getCurrentCut(this.children[i]);
return child.getCurrentCut(newNode);
}
}
return this; //we are at the deepest valid level that newNode can be placed
Expand Down Expand Up @@ -73,10 +75,13 @@ export class CutNode {
}

if (otherNode instanceof AtomNode) {
return this.ellipse.containsShape((otherNode as AtomNode).rect);
} else {
return this.ellipse.containsShape(otherNode.rect);
} else if(otherNode instanceof CutNode) {
//ELLIPSE TO BE IMPLEMENTED ACCURATELY
return this.ellipse.containsShape((otherNode as CutNode).ellipse as Ellipse);
return this.ellipse.containsShape(otherNode.ellipse as Ellipse);
}
else {
throw Error("containsNode expected AtomNode or CutNode")
}
}

Expand Down Expand Up @@ -132,4 +137,23 @@ export class CutNode {
}
return str;
}

public toFormulaString(): string {
let formulaString = ""
for(let child of this.children){
if (child instanceof AtomNode){
formulaString += child.identifier;
} else if(child instanceof CutNode){
formulaString += child.toFormulaString();
}
formulaString += " "
}
formulaString = formulaString.slice(0, -1);
if(this.ellipse == null){
formulaString = "[" + formulaString + "]";
}else{
formulaString = "(" + formulaString + ")";
}
return formulaString;
}
}
5 changes: 3 additions & 2 deletions src/AEG/Ellipse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,18 @@ export class Ellipse {
}
}
return true;
} else {
} else if (otherShape instanceof Ellipse) {
//If all the widest coordinates of the other ellipse are within this ellipse,
//This ellipse contains the other ellipse
const points = (otherShape as Ellipse).getWidestCoordinates();
const points = otherShape.getWidestCoordinates();
for (let j = 0; j < 4; j++) {
if (!this.containsPoint(points[j])) {
return false;
}
}
return true;
}
throw Error("Invalid Shape passed to containsShape, must be a Rectangle | Ellipse")
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/AtomCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function atomUp() {
);
const newAtom: AtomNode = new AtomNode(atom, currentPoint, newRect);
tree.insert(newAtom);
console.log(tree.toString());
canvas.removeEventListener("mousemove", moveAtom);
canvas.removeEventListener("mouseup", atomUp);
canvas.removeEventListener("mouseOut", mouseOut);
Expand Down
11 changes: 10 additions & 1 deletion src/EllipseCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ export function createEllipse(original: Point, current: Point): Ellipse {
ctx.stroke();
}

let currentEllipse = new Ellipse(center, rx, ry);
if(tree.canInsert(new CutNode(currentEllipse))){
ctx.strokeStyle = "#00FF00";
}else{
ctx.strokeStyle = "#FF0000";
}
ctx.beginPath();
ctx.ellipse(center.x, center.y, rx, ry, 0, 0, 2 * Math.PI);
//I know this is stupid to constantly make a new ellipse but my brain hurts I'm sorry
ctx.stroke();
currentEllipse = new Ellipse(center, rx, ry);

return currentEllipse;
}

Expand Down Expand Up @@ -110,9 +116,12 @@ function mouseUp() {
if (tree.canInsert(newCut)) {
tree.insert(newCut);
}
console.log(tree.toString());
canvas.removeEventListener("mousemove", mouseMoving);
canvas.removeEventListener("mouseup", mouseUp);
canvas.removeEventListener("mouseout", mouseOut);
ctx.clearRect(0, 0, canvas.width, canvas.height);
redrawCut(tree.sheet);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function redrawCut(incomingNode: CutNode) {
}
}
if (incomingNode.ellipse instanceof Ellipse) {
ctx.strokeStyle = "#000000";
ctx.beginPath();
ctx.ellipse(
incomingNode.ellipse.center.x,
Expand Down

0 comments on commit 3d8ce22

Please sign in to comment.