@@ -88,87 +88,118 @@ class TriangleBlock: PeggleObject {
8888 return area1 + area2 + area3 <= areaOfSelf
8989 }
9090
91- // TODO: Refactor this
92- private func trianglePegOverlapCheck ( peg : Peg ) -> Bool {
93- // TEST 1: Vertex within circle
94- let c1x = peg. center . xCoordinate - vertexOne . xCoordinate
95- let c1y = peg . center . yCoordinate - vertexOne . yCoordinate
96- let radiusSqr = peg . radius * peg . radius
97- let c1sqr = c1x * c1x + c1y * c1y - radiusSqr
98-
99- if c1sqr <= 0 {
100- return true
101- }
91+ private func vertexWithinPeg ( vertex : Point , peg : Peg ) -> Bool {
92+ let c1x = peg . center . xCoordinate - vertex . xCoordinate
93+ let c1y = peg . center . yCoordinate - vertex . yCoordinate
94+ let radiusSqr = peg. radius * peg . radius
95+ let c1sqr = c1x * c1x + c1y * c1y - radiusSqr
96+
97+ if c1sqr <= 0 {
98+ return true
99+ }
100+ return false
101+ }
102102
103- let c2x = peg. center. xCoordinate - vertexTwo. xCoordinate
104- let c2y = peg. center. yCoordinate - vertexTwo. yCoordinate
105- let c2sqr = c2x * c2x + c2y * c2y - radiusSqr
103+ private func pegWithinTriangle( peg: Peg ) -> Bool {
104+ let e1x = vertexTwo. xCoordinate - vertexOne. xCoordinate
105+ let e1y = vertexTwo. yCoordinate - vertexOne. yCoordinate
106+ let c1x = peg. center. xCoordinate - vertexOne. xCoordinate
107+ let c1y = peg. center. yCoordinate - vertexOne. yCoordinate
108+
109+ let e2x = vertexThree. xCoordinate - vertexTwo. xCoordinate
110+ let e2y = vertexThree. yCoordinate - vertexTwo. yCoordinate
111+ let c2x = peg. center. xCoordinate - vertexTwo. xCoordinate
112+ let c2y = peg. center. yCoordinate - vertexTwo. yCoordinate
113+
114+ let e3x = vertexOne. xCoordinate - vertexThree. xCoordinate
115+ let e3y = vertexOne. yCoordinate - vertexThree. yCoordinate
116+ let c3x = peg. center. xCoordinate - vertexThree. xCoordinate
117+ let c3y = peg. center. yCoordinate - vertexThree. yCoordinate
118+
119+ if e1y * c1x - e1x * c1y >= 0
120+ && e2y * c2x - e2x * c2y >= 0
121+ && e3y * c3x - e3x * c3y >= 0 {
122+ return true
123+ }
106124
107- if c2sqr <= 0 {
108- return true
109- }
125+ return false
126+ }
110127
111- let c3x = peg. center . xCoordinate - vertexThree . xCoordinate
112- let c3y = peg. center . yCoordinate - vertexThree . yCoordinate
128+ private func pegIntersectEdge ( peg: Peg ) -> Bool {
129+ let radiusSqr = peg. radius * peg . radius
113130
114- let c3sqr = c3x * c3x + c3y * c3y - radiusSqr
131+ let e1x = vertexTwo. xCoordinate - vertexOne. xCoordinate
132+ let e1y = vertexTwo. yCoordinate - vertexOne. yCoordinate
133+ let c1x = peg. center. xCoordinate - vertexOne. xCoordinate
134+ let c1y = peg. center. yCoordinate - vertexOne. yCoordinate
135+ let c1sqr = c1x * c1x + c1y * c1y - radiusSqr
115136
116- if c3sqr <= 0 {
117- return true
118- }
137+ let e2x = vertexThree. xCoordinate - vertexTwo. xCoordinate
138+ let e2y = vertexThree. yCoordinate - vertexTwo. yCoordinate
139+ let c2x = peg. center. xCoordinate - vertexTwo. xCoordinate
140+ let c2y = peg. center. yCoordinate - vertexTwo. yCoordinate
141+ let c2sqr = c2x * c2x + c2y * c2y - radiusSqr
119142
120- // TEST 2: Circle centre within triangle
121- let e1x = vertexTwo. xCoordinate - vertexOne. xCoordinate
122- let e1y = vertexTwo. yCoordinate - vertexOne. yCoordinate
143+ let e3x = vertexOne. xCoordinate - vertexThree. xCoordinate
144+ let e3y = vertexOne. yCoordinate - vertexThree. yCoordinate
145+ let c3x = peg. center. xCoordinate - vertexThree. xCoordinate
146+ let c3y = peg. center. yCoordinate - vertexThree. yCoordinate
147+ let c3sqr = c3x * c3x + c3y * c3y - radiusSqr
123148
124- let e2x = vertexThree. xCoordinate - vertexTwo. xCoordinate
125- let e2y = vertexThree. yCoordinate - vertexTwo. yCoordinate
149+ var kValue = c1x * e1x + c1y * e1y
126150
127- let e3x = vertexOne . xCoordinate - vertexThree . xCoordinate
128- let e3y = vertexOne . yCoordinate - vertexThree . yCoordinate
151+ if kValue > 0 {
152+ let len = e1x * e1x + e1y * e1y
129153
130- if e1y * c1x - e1x * c1y >= 0
131- && e2y * c2x - e2x * c2y >= 0
132- && e3y * c3x - e3x * c3y >= 0 {
133- return true
134- }
154+ if kValue < len {
155+ if c1sqr * len <= kValue * kValue {
156+ return true
157+ }
158+ }
159+ }
135160
136- // TEST 3: Circle intersects edge
137- var kValue = c1x * e1x + c1y * e1y
161+ kValue = c2x * e2x + c2y * e2y
138162
139- if kValue > 0 {
140- let len = e1x * e1x + e1y * e1y
163+ if kValue > 0 {
164+ let len = e2x * e2x + e2y * e2y
141165
142- if kValue < len {
143- if c1sqr * len <= kValue * kValue {
144- return true
145- }
166+ if kValue < len {
167+ if c2sqr * len <= kValue * kValue {
168+ return true
146169 }
147- }
170+ }
171+ }
148172
149- kValue = c2x * e2x + c2y * e2y
173+ kValue = c3x * e3x + c3y * e3y
150174
151- if kValue > 0 {
152- let len = e2x * e2x + e2y * e2y
175+ if kValue > 0 {
176+ let len = e3x * e3x + e3y * e3y
153177
154- if kValue < len {
155- if c2sqr * len <= kValue * kValue {
156- return true
157- }
178+ if kValue < len {
179+ if c3sqr * len <= kValue * kValue {
180+ return true
158181 }
159- }
182+ }
183+ }
160184
161- kValue = c3x * e3x + c3y * e3y
185+ return false
162186
163- if kValue > 0 {
164- let len = e3x * e3x + e3y * e3y
187+ }
165188
166- if kValue < len {
167- if c3sqr * len <= kValue * kValue {
168- return true
169- }
170- }
171- }
172- return false
189+ private func trianglePegOverlapCheck( peg: Peg ) -> Bool {
190+ if vertexWithinPeg ( vertex: vertexOne, peg: peg)
191+ || vertexWithinPeg ( vertex: vertexTwo, peg: peg)
192+ || vertexWithinPeg ( vertex: vertexThree, peg: peg) {
193+ return true
194+ }
195+
196+ if pegWithinTriangle ( peg: peg) {
197+ return true
198+ }
199+
200+ if pegIntersectEdge ( peg: peg) {
201+ return true
202+ }
203+ return false
173204 }
174205}
0 commit comments