Skip to content

Commit 94583ed

Browse files
committed
Make requested changes and corrections from review
- Correct precondition check - Remove label from `drawSolidPolygon` - Make a variadic version of the initialiser for `Polyline` and `Polygon`
1 parent 6f07800 commit 94583ed

File tree

6 files changed

+30
-24
lines changed

6 files changed

+30
-24
lines changed

Sources/AGGRenderer/AGGRenderer/AGGRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public class AGGRenderer: Renderer{
140140
agg_object);
141141
}
142142

143-
public func drawSolidPolygon(polygon: SwiftPlot.Polygon,
143+
public func drawSolidPolygon(_ polygon: SwiftPlot.Polygon,
144144
fillColor: Color) {
145145
var x = [Float]()
146146
var y = [Float]()

Sources/QuartzRenderer/QuartzRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public class QuartzRenderer: Renderer {
369369
context.fillPath()
370370
}
371371

372-
public func drawSolidPolygon(polygon: SwiftPlot.Polygon,
372+
public func drawSolidPolygon(_ polygon: SwiftPlot.Polygon,
373373
fillColor: Color) {
374374
let polygonPath = CGMutablePath()
375375
polygonPath.addLines(between: polygon.points.map { CGPoint(x: CGFloat($0.x), y: CGFloat($0.y)) },

Sources/SVGRenderer/SVGRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public class SVGRenderer: Renderer{
156156
lines.append(triangle)
157157
}
158158

159-
public func drawSolidPolygon(polygon: SwiftPlot.Polygon,
159+
public func drawSolidPolygon(_ polygon: SwiftPlot.Polygon,
160160
fillColor: Color) {
161161
var pointsString = ""
162162
for point in polygon.points {

Sources/SwiftPlot/PlotStyleHelpers.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ struct Arrow : Annotation {
184184
let wedgeRotateAngle = -atan2(end.x - start.x, end.y - start.y)
185185
p1 = rotatePoint(point: p1, center: start, angleRadians: wedgeRotateAngle + 0.5 * Float.pi)
186186
p2 = rotatePoint(point: p2, center: start, angleRadians: wedgeRotateAngle + 0.5 * Float.pi)
187-
let head = Polygon([p1, p2, end])!
188-
renderer.drawSolidPolygon(polygon: head,
187+
let head = Polygon(p1, p2, end)!
188+
renderer.drawSolidPolygon(head,
189189
fillColor: color)
190190
default:
191-
renderer.drawPolyline(Polyline([start, end])!,
191+
renderer.drawPolyline(Polyline(start, end)!,
192192
strokeWidth: strokeWidth,
193193
strokeColor: color,
194194
isDashed: isDashed)
@@ -205,20 +205,20 @@ struct Arrow : Annotation {
205205
// Draws arrow head points.
206206
switch headStyle {
207207
case .skeletal:
208-
let head = Polyline([p1, b, p2])!
208+
let head = Polyline(p1, b, p2)!
209209
renderer.drawPolyline(head,
210210
strokeWidth: strokeWidth,
211211
strokeColor: color,
212212
isDashed: isDashed)
213213
case .filled:
214-
let head = Polygon([p1, b, p2])!
215-
renderer.drawSolidPolygon(polygon: head,
214+
let head = Polygon(p1, b, p2)!
215+
renderer.drawSolidPolygon(head,
216216
fillColor: color)
217217
case .dart:
218218
var p3 = end + Point(-headLength/2, 0.0)
219219
p3 = rotatePoint(point: p3, center: b, angleRadians: rotateAngle + 0.5 * Float.pi)
220-
let head = Polygon([p1, p3, p2, b])!
221-
renderer.drawSolidPolygon(polygon: head,
220+
let head = Polygon(p1, p3, p2, b)!
221+
renderer.drawSolidPolygon(head,
222222
fillColor: color)
223223
default:
224224
break

Sources/SwiftPlot/Renderer.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public protocol Renderer: AnyObject{
154154
* without shifted origin.
155155
* This is decided by the boolean parameter isOriginShifted.
156156
*/
157-
func drawSolidPolygon(polygon: Polygon,
157+
func drawSolidPolygon(_ polygon: Polygon,
158158
fillColor: Color)
159159

160160
/*getTextWidth()
@@ -200,7 +200,8 @@ public struct Polygon {
200200
public var points: [Point] {
201201
didSet {
202202
let count = points.count
203-
precondition(count >= 2, "Polygon: points array should always contain at least 3 points, it now has \(count).")
203+
precondition(count >= 3,
204+
"Polygon: points array should always contain at least 3 points, it now has \(count).")
204205
}
205206
}
206207

@@ -209,14 +210,17 @@ public struct Polygon {
209210

210211
self.points = points
211212
}
213+
214+
public init?(_ points: Point...) { self.init(points) }
212215
}
213216

214217
/// Polyline structure definition
215218
public struct Polyline {
216219
public var points: [Point] {
217220
didSet {
218221
let count = points.count
219-
precondition(count >= 2, "Polyline: points array should always contain at least 2 points, it now has \(count).")
222+
precondition(count >= 2,
223+
"Polyline: points array should always contain at least 2 points, it now has \(count).")
220224
}
221225
}
222226

@@ -225,4 +229,6 @@ public struct Polyline {
225229

226230
self.points = points
227231
}
232+
233+
public init?(_ points: Point...) { self.init(points) }
228234
}

Sources/SwiftPlot/ScatterChart.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ extension ScatterPlot: HasGraphLayout {
368368
tR = rotatePoint(point: tR, center: p, angleDegrees: 45.0)
369369
bL = rotatePoint(point: bL, center: p, angleDegrees: 45.0)
370370
bR = rotatePoint(point: bR, center: p, angleDegrees: 45.0)
371-
let diamond = Polygon([tL, tR, bR, bL])!
372-
renderer.drawSolidPolygon(polygon: diamond,
371+
let diamond = Polygon(tL, tR, bR, bL)!
372+
renderer.drawSolidPolygon(diamond,
373373
fillColor: color)
374374
case .hexagon:
375375
var hexagonPoint = Point(p.x + 0.0,
@@ -382,7 +382,7 @@ extension ScatterPlot: HasGraphLayout {
382382
hexagonPoints.append(hexagonPoint)
383383
}
384384
let hexagon = Polygon(hexagonPoints)!
385-
renderer.drawSolidPolygon(polygon: hexagon,
385+
renderer.drawSolidPolygon(hexagon,
386386
fillColor: color)
387387
case .pentagon:
388388
var pentagonPoint = Point(p.x + 0.0,
@@ -395,7 +395,7 @@ extension ScatterPlot: HasGraphLayout {
395395
pentagonPoints.append(pentagonPoint)
396396
}
397397
let pentagon = Polygon(pentagonPoints)!
398-
renderer.drawSolidPolygon(polygon: pentagon,
398+
renderer.drawSolidPolygon(pentagon,
399399
fillColor: color)
400400

401401
case .star:
@@ -417,7 +417,7 @@ extension ScatterPlot: HasGraphLayout {
417417
starPoints.append(starInnerPoint)
418418
}
419419
let star = Polygon(starPoints)!
420-
renderer.drawSolidPolygon(polygon: star,
420+
renderer.drawSolidPolygon(star,
421421
fillColor: color)
422422
}
423423
}
@@ -465,8 +465,8 @@ extension ScatterPlotSeriesOptions.ScatterPattern {
465465
let p2 = rotatePoint(point: tR, center: c, angleDegrees: 45.0)
466466
let p3 = rotatePoint(point: bR, center: c, angleDegrees: 45.0)
467467
let p4 = rotatePoint(point: bL, center: c, angleDegrees: 45.0)
468-
let diamond = Polygon([p1, p2, p3, p4])!
469-
renderer.drawSolidPolygon(polygon: diamond,
468+
let diamond = Polygon(p1, p2, p3, p4)!
469+
renderer.drawSolidPolygon(diamond,
470470
fillColor: color)
471471
case .hexagon:
472472
let c = Point((tL.x+bR.x)*Float(0.5),
@@ -481,7 +481,7 @@ extension ScatterPlotSeriesOptions.ScatterPattern {
481481
hexagonPoints.append(hexagonPoint)
482482
}
483483
let hexagon = Polygon(hexagonPoints)!
484-
renderer.drawSolidPolygon(polygon: hexagon,
484+
renderer.drawSolidPolygon(hexagon,
485485
fillColor: color)
486486
case .pentagon:
487487
let c = Point((tL.x+bR.x)*Float(0.5),
@@ -496,7 +496,7 @@ extension ScatterPlotSeriesOptions.ScatterPattern {
496496
pentagonPoints.append(pentagonPoint)
497497
}
498498
let pentagon = Polygon(pentagonPoints)!
499-
renderer.drawSolidPolygon(polygon: pentagon,
499+
renderer.drawSolidPolygon(pentagon,
500500
fillColor: color)
501501
case .star:
502502
let c = Point((tL.x+bR.x)*Float(0.5),
@@ -519,7 +519,7 @@ extension ScatterPlotSeriesOptions.ScatterPattern {
519519
starPoints.append(starInnerPoint)
520520
}
521521
let star = Polygon(starPoints)!
522-
renderer.drawSolidPolygon(polygon: star,
522+
renderer.drawSolidPolygon(star,
523523
fillColor: color)
524524
}
525525
}

0 commit comments

Comments
 (0)