Skip to content

Commit c17e6d9

Browse files
author
Ryan R
authored
Merge pull request #376 from RAIRLab/374-double-cut-can-have-different-atoms-on-both-layers
374 Double Cut can have different Atoms on both layers!
2 parents d19f7fa + 179c1df commit c17e6d9

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

src/ProofTools/DoubleCutInsertionTool.ts

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,8 @@ export function doubleCutInsertionMouseMove(event: MouseEvent): void {
7272
const smallCut: CutNode = new CutNode(calcSmallEllipse(<Ellipse>largeCut.ellipse));
7373
redrawProof();
7474

75-
if (!wasOut && largeCut.ellipse !== null && smallCut.ellipse !== null) {
76-
const legal =
77-
currentProofTree.canInsert(largeCut) &&
78-
ellipseLargeEnough(largeCut.ellipse) &&
79-
currentProofTree.canInsert(smallCut) &&
80-
ellipseLargeEnough(smallCut.ellipse);
81-
82-
const color = legal ? legalColor() : illegalColor();
75+
if (!wasOut) {
76+
const color = selectAndHighlightHandler(largeCut, smallCut) ? legalColor() : illegalColor();
8377
drawCut(largeCut, color);
8478
drawCut(smallCut, color);
8579
determineAndChangeCursorStyle(color, "cursor: crosshair", "cursor: no-drop");
@@ -110,18 +104,10 @@ export function doubleCutInsertionMouseUp(event: MouseEvent): void {
110104

111105
const nextProof = new ProofNode(currentProofTree, "DC Insert");
112106

113-
if (!wasOut && largeCut.ellipse !== null && smallCut.ellipse !== null) {
114-
const legal =
115-
currentProofTree.canInsert(largeCut) &&
116-
ellipseLargeEnough(largeCut.ellipse) &&
117-
currentProofTree.canInsert(smallCut) &&
118-
ellipseLargeEnough(smallCut.ellipse);
119-
120-
if (legal) {
121-
nextProof.tree.insert(largeCut);
122-
nextProof.tree.insert(smallCut);
123-
TreeContext.pushToProof(nextProof);
124-
}
107+
if (!wasOut && selectAndHighlightHandler(largeCut, smallCut)) {
108+
nextProof.tree.insert(largeCut);
109+
nextProof.tree.insert(smallCut);
110+
TreeContext.pushToProof(nextProof);
125111
}
126112
redrawProof();
127113
}
@@ -148,3 +134,41 @@ function calcSmallEllipse(ellipse: Ellipse): Ellipse {
148134
Math.floor(ellipse.radiusY * 0.8)
149135
);
150136
}
137+
138+
/**
139+
* Determines if the two cuts are both in legal positions, are considered larger enough to be legal
140+
* and ensures the larger cut will not have any children except the smaller cut.
141+
* @param largeCut The outer cut of the double cut being drawn
142+
* @param smallCut The inner cut of the double cut being drawn
143+
* @returns If the current double cut is in a valid position for placement
144+
*/
145+
function selectAndHighlightHandler(largeCut: CutNode, smallCut: CutNode): boolean {
146+
return (
147+
largeCut.ellipse !== null &&
148+
smallCut.ellipse !== null &&
149+
currentProofTree.canInsert(largeCut) &&
150+
ellipseLargeEnough(largeCut.ellipse) &&
151+
currentProofTree.canInsert(smallCut) &&
152+
ellipseLargeEnough(smallCut.ellipse) &&
153+
largeCutChildrenCheck(largeCut, smallCut)
154+
);
155+
}
156+
157+
/**
158+
* Creates a copy of the current tree, and the current cuts and inserts them into the copied tree.
159+
* If the inserted larger cut has any children besides the inner cut then it is not a valid double cut
160+
* and returns false.
161+
* @param largeCut The outer cut of the double cut being drawn
162+
* @param smallCut The inner cut of the double cut being drawn
163+
* @returns Whether or not the larger cut will only have the inner cut as a child
164+
*/
165+
function largeCutChildrenCheck(largeCut: CutNode, smallCut: CutNode): boolean {
166+
const treeCopy: AEGTree = new AEGTree(currentProofTree.sheet);
167+
const largeCutCopy: CutNode = new CutNode(largeCut.ellipse);
168+
const smallCutCopy: CutNode = new CutNode(smallCut.ellipse);
169+
170+
treeCopy.insert(largeCutCopy);
171+
treeCopy.insert(smallCutCopy);
172+
173+
return largeCutCopy.children.length === 1;
174+
}

0 commit comments

Comments
 (0)