@@ -67,14 +67,14 @@ public protocol Renderer: AnyObject{
6767 strokeColor: Color , isDashed: Bool )
6868
6969 /*drawPlotLines()
70- *params: points p: [Point] ,
70+ *params: polyline: Polyline ,
7171 * strokeWidth thickness: Float,
7272 * strokeColor: Color,
7373 * isDashed: Bool
7474 *description: Draws all the line segments in a single data series for a Line Graph.
7575 * This function always operates in the coordinate system with the shifted origin.
7676 */
77- func drawPlotLines( points p : [ Point ] ,
77+ func drawPlotLines( polyline : Polyline ,
7878 strokeWidth thickness: Float ,
7979 strokeColor: Color , isDashed: Bool )
8080
@@ -263,3 +263,70 @@ extension Polygon.Iterator: IteratorProtocol {
263263}
264264
265265extension Polygon . Iterator : Sequence { }
266+
267+ /// Polyline structure definition and sequence extension, along with its iterator.
268+ public struct Polyline {
269+ public var p1 : Point , p2 : Point
270+ public var tail : [ Point ]
271+
272+ public init ( _ p1: Point , _ p2: Point , tail: [ Point ] = [ ] ) {
273+ ( self . p1, self . p2) = ( p1, p2)
274+ self . tail = tail
275+ }
276+
277+ public init ( _ p1: Point , _ p2: Point , tail: ArraySlice < Point > ) {
278+ self . init ( p1, p2, tail: Array ( tail) )
279+ }
280+
281+ public init ( ) {
282+ self . init ( . zero, . zero)
283+ }
284+
285+ public init ? ( points: [ Point ] ) {
286+ guard points. count >= 2 else { return nil }
287+
288+ self = Polyline ( points [ 0 ] , points [ 1 ] , tail: points [ 2 ... ] )
289+ }
290+ }
291+
292+ extension Polyline : Sequence {
293+ public struct Iterator {
294+ private var state : State
295+ private var tailIterator : Array < Point > . Iterator
296+ private let polyline : Polyline
297+
298+ private enum State {
299+ case p1, p2
300+ case tail
301+ }
302+
303+ public init ( polyline: Polyline ) {
304+ state = . p1
305+ tailIterator = polyline. tail. makeIterator ( )
306+ self . polyline = polyline
307+ }
308+ }
309+
310+ public func makeIterator( ) -> Polyline . Iterator {
311+ return Iterator ( polyline: self )
312+ }
313+ }
314+
315+ extension Polyline . Iterator : IteratorProtocol {
316+ public typealias Element = Point
317+
318+ public mutating func next( ) -> Point ? {
319+ switch state {
320+ case . p1:
321+ state = . p2
322+ return polyline. p1
323+ case . p2:
324+ state = . tail
325+ return polyline. p2
326+ case . tail:
327+ return tailIterator. next ( )
328+ }
329+ }
330+ }
331+
332+ extension Polyline . Iterator : Sequence { }
0 commit comments