@@ -195,139 +195,34 @@ extension Renderer {
195195 }
196196}
197197
198- /// Polygon structure definition and sequence extension, along with its iterator.
198+ /// Polygon structure definition
199199public struct Polygon {
200- public var p1 : Point , p2 : Point , p3 : Point
201- public var tail : [ Point ]
202-
203- public init ( _ p1: Point , _ p2: Point , _ p3: Point , tail: [ Point ] = [ ] ) {
204- ( self . p1, self . p2, self . p3) = ( p1, p2, p3)
205- self . tail = tail
206- }
207-
208- public init ( _ p1: Point , _ p2: Point , _ p3: Point , tail: ArraySlice < Point > ) {
209- self . init ( p1, p2, p3, tail: Array ( tail) )
210- }
211-
212- public init ( ) {
213- self . init ( . zero, . zero, . zero)
214- }
215-
216- public init ? ( points: [ Point ] ) {
217- guard points. count >= 3 else { return nil }
218-
219- self . init ( points [ 0 ] , points [ 1 ] , points [ 2 ] , tail: points [ 3 ... ] )
200+ public var points : [ Point ] {
201+ didSet {
202+ let count = points. count
203+ precondition ( count >= 2 , " Polygon: points array should always contain at least 3 points, it now has \( count) . " )
204+ }
220205 }
221- }
222206
223- extension Polygon : Sequence {
224- public struct Iterator {
225- private var state : State
226- private var tailIterator : Array < Point > . Iterator
227- private let polygon : Polygon
228-
229- private enum State {
230- case p1, p2, p3
231- case tail
232- }
207+ public init ? ( _ points: [ Point ] ) {
208+ guard points. count >= 3 else { return nil }
233209
234- public init ( polygon: Polygon ) {
235- state = . p1
236- tailIterator = polygon. tail. makeIterator ( )
237- self . polygon = polygon
238- }
239- }
240-
241- public func makeIterator( ) -> Polygon . Iterator {
242- return Iterator ( polygon: self )
210+ self . points = points
243211 }
244212}
245213
246- extension Polygon . Iterator : IteratorProtocol {
247- public typealias Element = Point
248-
249- public mutating func next( ) -> Point ? {
250- switch state {
251- case . p1:
252- state = . p2
253- return polygon. p1
254- case . p2:
255- state = . p3
256- return polygon. p2
257- case . p3:
258- state = . tail
259- return polygon. p3
260- case . tail:
261- return tailIterator. next ( )
214+ /// Polyline structure definition
215+ public struct Polyline {
216+ public var points : [ Point ] {
217+ didSet {
218+ let count = points. count
219+ precondition ( count >= 2 , " Polyline: points array should always contain at least 2 points, it now has \( count) . " )
262220 }
263221 }
264- }
265222
266- extension Polygon . Iterator : Sequence { }
267-
268- /// Polyline structure definition and sequence extension, along with its iterator.
269- public struct Polyline {
270- public var p1 : Point , p2 : Point
271- public var tail : [ Point ]
272-
273- public init ( _ p1: Point , _ p2: Point , tail: [ Point ] = [ ] ) {
274- ( self . p1, self . p2) = ( p1, p2)
275- self . tail = tail
276- }
277-
278- public init ( _ p1: Point , _ p2: Point , tail: ArraySlice < Point > ) {
279- self . init ( p1, p2, tail: Array ( tail) )
280- }
281-
282- public init ( ) {
283- self . init ( . zero, . zero)
284- }
285-
286- public init ? ( points: [ Point ] ) {
223+ public init ? ( _ points: [ Point ] ) {
287224 guard points. count >= 2 else { return nil }
288225
289- self = Polyline ( points [ 0 ] , points [ 1 ] , tail: points [ 2 ... ] )
290- }
291- }
292-
293- extension Polyline : Sequence {
294- public struct Iterator {
295- private var state : State
296- private var tailIterator : Array < Point > . Iterator
297- private let polyline : Polyline
298-
299- private enum State {
300- case p1, p2
301- case tail
302- }
303-
304- public init ( polyline: Polyline ) {
305- state = . p1
306- tailIterator = polyline. tail. makeIterator ( )
307- self . polyline = polyline
308- }
309- }
310-
311- public func makeIterator( ) -> Polyline . Iterator {
312- return Iterator ( polyline: self )
313- }
314- }
315-
316- extension Polyline . Iterator : IteratorProtocol {
317- public typealias Element = Point
318-
319- public mutating func next( ) -> Point ? {
320- switch state {
321- case . p1:
322- state = . p2
323- return polyline. p1
324- case . p2:
325- state = . tail
326- return polyline. p2
327- case . tail:
328- return tailIterator. next ( )
329- }
226+ self . points = points
330227 }
331228}
332-
333- extension Polyline . Iterator : Sequence { }
0 commit comments