@@ -72,14 +72,8 @@ export function doubleCutInsertionMouseMove(event: MouseEvent): void {
72
72
const smallCut : CutNode = new CutNode ( calcSmallEllipse ( < Ellipse > largeCut . ellipse ) ) ;
73
73
redrawProof ( ) ;
74
74
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 ( ) ;
83
77
drawCut ( largeCut , color ) ;
84
78
drawCut ( smallCut , color ) ;
85
79
determineAndChangeCursorStyle ( color , "cursor: crosshair" , "cursor: no-drop" ) ;
@@ -110,18 +104,10 @@ export function doubleCutInsertionMouseUp(event: MouseEvent): void {
110
104
111
105
const nextProof = new ProofNode ( currentProofTree , "DC Insert" ) ;
112
106
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 ) ;
125
111
}
126
112
redrawProof ( ) ;
127
113
}
@@ -148,3 +134,41 @@ function calcSmallEllipse(ellipse: Ellipse): Ellipse {
148
134
Math . floor ( ellipse . radiusY * 0.8 )
149
135
) ;
150
136
}
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