From 397a4570e0452cb280424dc6e4ba03243ade79b3 Mon Sep 17 00:00:00 2001 From: Yuri Strot Date: Fri, 24 May 2019 19:41:40 +0700 Subject: [PATCH] Add compact API for geometry classes and Color --- Source/model/draw/Color.swift | 4 ++ Source/model/geom2d/Line.swift | 13 +++-- Source/model/geom2d/Locus.swift | 16 ++++++ Source/model/geom2d/MoveTo.swift | 4 ++ Source/model/geom2d/Point.swift | 7 ++- Source/model/geom2d/Polygon.swift | 4 ++ Source/model/geom2d/Polyline.swift | 4 ++ Source/model/geom2d/Rect.swift | 7 +++ Source/model/geom2d/Size.swift | 7 ++- Source/model/geom2d/Transform.swift | 82 +++++++++++++++++++++++------ 10 files changed, 126 insertions(+), 22 deletions(-) diff --git a/Source/model/draw/Color.swift b/Source/model/draw/Color.swift index 092dc445..e8393c95 100644 --- a/Source/model/draw/Color.swift +++ b/Source/model/draw/Color.swift @@ -20,6 +20,10 @@ open class Color: Fill { public static let purple: Color = Color( val: 0x800080 ) public static let clear: Color = Color.rgba(r: 0, g: 0, b: 0, a: 0) + public init(_ val: Int = 0) { + self.val = val + } + public init(val: Int = 0) { self.val = val } diff --git a/Source/model/geom2d/Line.swift b/Source/model/geom2d/Line.swift index 77a2395d..e37c2d29 100644 --- a/Source/model/geom2d/Line.swift +++ b/Source/model/geom2d/Line.swift @@ -5,6 +5,13 @@ open class Line: Locus { public let x2: Double public let y2: Double + public init(_ x1: Double, _ y1: Double, _ x2: Double, _ y2: Double) { + self.x1 = x1 + self.y1 = y1 + self.x2 = x2 + self.y2 = y2 + } + public init(x1: Double = 0, y1: Double = 0, x2: Double = 0, y2: Double = 0) { self.x1 = x1 self.y1 = y1 @@ -13,11 +20,7 @@ open class Line: Locus { } override open func bounds() -> Rect { - return Rect( - x: min(x1, x2), - y: min(y1, y2), - w: abs(x1 - x2), - h: abs(y1 - y2)) + return Rect(x: min(x1, x2), y: min(y1, y2), w: abs(x1 - x2), h: abs(y1 - y2)) } override open func toPath() -> Path { diff --git a/Source/model/geom2d/Locus.swift b/Source/model/geom2d/Locus.swift index 1a7539d6..06e3747e 100644 --- a/Source/model/geom2d/Locus.swift +++ b/Source/model/geom2d/Locus.swift @@ -15,10 +15,26 @@ open class Locus { return Shape(form: self, fill: with) } + open func fill(_ hex: Int) -> Shape { + return Shape(form: self, fill: Color(val: hex)) + } + + open func fill(_ fill: Fill) -> Shape { + return Shape(form: self, fill: fill) + } + open func stroke(fill: Fill = Color.black, width: Double = 1, cap: LineCap = .butt, join: LineJoin = .miter, dashes: [Double] = []) -> Shape { return Shape(form: self, stroke: Stroke(fill: fill, width: width, cap: cap, join: join, dashes: dashes)) } + open func stroke(color: Color, width: Double = 1, cap: LineCap = .butt, join: LineJoin = .miter, dashes: [Double] = []) -> Shape { + return Shape(form: self, stroke: Stroke(fill: color, width: width, cap: cap, join: join, dashes: dashes)) + } + + open func stroke(color: Int, width: Double = 1, cap: LineCap = .butt, join: LineJoin = .miter, dashes: [Double] = []) -> Shape { + return Shape(form: self, stroke: Stroke(fill: Color(color), width: width, cap: cap, join: join, dashes: dashes)) + } + open func toPath() -> Path { fatalError("Unsupported locus: \(self)") } diff --git a/Source/model/geom2d/MoveTo.swift b/Source/model/geom2d/MoveTo.swift index 7d4a04ae..9f488153 100644 --- a/Source/model/geom2d/MoveTo.swift +++ b/Source/model/geom2d/MoveTo.swift @@ -1,5 +1,9 @@ open class MoveTo: PathBuilder { + public init(_ x: Double, _ y: Double) { + super.init(segment: PathSegment(type: .M, data: [x, y])) + } + public init(x: Double, y: Double) { super.init(segment: PathSegment(type: .M, data: [x, y])) } diff --git a/Source/model/geom2d/Point.swift b/Source/model/geom2d/Point.swift index cac4f508..234ae075 100644 --- a/Source/model/geom2d/Point.swift +++ b/Source/model/geom2d/Point.swift @@ -5,7 +5,12 @@ open class Point: Locus { public let x: Double public let y: Double - public static let origin: Point = Point( x: 0, y: 0 ) + public static let origin = Point(0, 0) + + public init(_ x: Double, _ y: Double) { + self.x = x + self.y = y + } public init(x: Double = 0, y: Double = 0) { self.x = x diff --git a/Source/model/geom2d/Polygon.swift b/Source/model/geom2d/Polygon.swift index 62c4c972..f47fdf5c 100644 --- a/Source/model/geom2d/Polygon.swift +++ b/Source/model/geom2d/Polygon.swift @@ -4,6 +4,10 @@ open class Polygon: Locus { public let points: [Double] + public init(_ points: [Double]) { + self.points = points + } + public init(points: [Double] = []) { self.points = points } diff --git a/Source/model/geom2d/Polyline.swift b/Source/model/geom2d/Polyline.swift index 9ee50871..83de29ce 100644 --- a/Source/model/geom2d/Polyline.swift +++ b/Source/model/geom2d/Polyline.swift @@ -4,6 +4,10 @@ open class Polyline: Locus { public let points: [Double] + public init(_ points: [Double]) { + self.points = points + } + public init(points: [Double] = []) { self.points = points } diff --git a/Source/model/geom2d/Rect.swift b/Source/model/geom2d/Rect.swift index c2a7c4c5..6f31771e 100644 --- a/Source/model/geom2d/Rect.swift +++ b/Source/model/geom2d/Rect.swift @@ -5,6 +5,13 @@ open class Rect: Locus { public let w: Double public let h: Double + public init(_ x: Double, _ y: Double, _ w: Double, _ h: Double) { + self.x = x + self.y = y + self.w = w + self.h = h + } + public init(x: Double = 0, y: Double = 0, w: Double = 0, h: Double = 0) { self.x = x self.y = y diff --git a/Source/model/geom2d/Size.swift b/Source/model/geom2d/Size.swift index b4a09805..9f5ef0e6 100644 --- a/Source/model/geom2d/Size.swift +++ b/Source/model/geom2d/Size.swift @@ -5,7 +5,12 @@ open class Size { public let w: Double public let h: Double - public static let zero: Size = Size(w: 0, h: 0) + public static let zero = Size(0, 0) + + public init(_ w: Double, _ h: Double) { + self.w = w + self.h = h + } public init(w: Double = 0, h: Double = 0) { self.w = w diff --git a/Source/model/geom2d/Transform.swift b/Source/model/geom2d/Transform.swift index 8f731b6c..463d50d3 100644 --- a/Source/model/geom2d/Transform.swift +++ b/Source/model/geom2d/Transform.swift @@ -9,7 +9,16 @@ public final class Transform { public let dx: Double public let dy: Double - public static let identity: Transform = Transform() + public static let identity = Transform() + + public init(_ m11: Double, _ m12: Double, _ m21: Double, _ m22: Double, _ dx: Double, _ dy: Double) { + self.m11 = m11 + self.m12 = m12 + self.m21 = m21 + self.m22 = m22 + self.dx = dx + self.dy = dy + } public init(m11: Double = 1, m12: Double = 0, m21: Double = 0, m22: Double = 1, dx: Double = 0, dy: Double = 0) { self.m11 = m11 @@ -20,39 +29,74 @@ public final class Transform { self.dy = dy } - public func move(dx: Double, dy: Double) -> Transform { - return Transform(m11: m11, m12: m12, m21: m21, m22: m22, - dx: dx * m11 + dy * m21 + self.dx, dy: dx * m12 + dy * m22 + self.dy) + public func move(_ dx: Double, _ dy: Double) -> Transform { + return move(dx: dx, dy: dy) } - public func scale(sx: Double, sy: Double) -> Transform { + public func move(dx: Double = 0, dy: Double = 0) -> Transform { + return Transform(m11: m11, m12: m12, m21: m21, m22: m22, + dx: dx * m11 + dy * m21 + self.dx, + dy: dx * m12 + dy * m22 + self.dy) + } + + public func scale(sx: Double = 0, sy: Double = 0) -> Transform { return Transform(m11: m11 * sx, m12: m12 * sx, m21: m21 * sy, m22: m22 * sy, dx: dx, dy: dy) } - public func shear(shx: Double, shy: Double) -> Transform { + public func scale(_ sx: Double, _ sy: Double) -> Transform { + return scale(sx: sx, sy: sy) + } + + public func shear(shx: Double = 0, shy: Double = 0) -> Transform { return Transform(m11: m11 + m21 * shy, m12: m12 + m22 * shy, m21: m11 * shx + m21, m22: m12 * shx + m22, dx: dx, dy: dy) } - public func rotate(angle: Double) -> Transform { - let asin = sin(angle); let acos = cos(angle) - return Transform(m11: acos * m11 + asin * m21, m12: acos * m12 + asin * m22, - m21: -asin * m11 + acos * m21, m22: -asin * m12 + acos * m22, dx: dx, dy: dy) + public func shear(_ shx: Double, _ shy: Double) -> Transform { + return shear(shx: shx, shy: shy) } - public func rotate(angle: Double, x: Double, y: Double) -> Transform { + public func rotate(angle: Double) -> Transform { + let asin = sin(angle) + let acos = cos(angle) + return Transform(m11: acos * m11 + asin * m21, m12: acos * m12 + asin * m22, + m21: -asin * m11 + acos * m21, m22: -asin * m12 + acos * m22, + dx: dx, dy: dy) + } + + public func rotate(_ angle: Double) -> Transform { + return rotate(angle: angle) + } + + public func rotate(angle: Double, x: Double = 0, y: Double = 0) -> Transform { return move(dx: x, dy: y).rotate(angle: angle).move(dx: -x, dy: -y) } - public class func move(dx: Double, dy: Double) -> Transform { + public func rotate(_ angle: Double, _ x: Double, _ y: Double) -> Transform { + return rotate(angle: angle, x: x, y: y) + } + + public class func move(dx: Double = 0, dy: Double = 0) -> Transform { return Transform(dx: dx, dy: dy) } - public class func scale(sx: Double, sy: Double) -> Transform { + public class func move(_ dx: Double, _ dy: Double) -> Transform { + return Transform(dx: dx, dy: dy) + } + + public class func scale(sx: Double = 0, sy: Double = 0) -> Transform { return Transform(m11: sx, m22: sy) } - public class func shear(shx: Double, shy: Double) -> Transform { + public class func scale(_ sx: Double, _ sy: Double) -> Transform { + return Transform(m11: sx, m22: sy) + } + + public class func shear(shx: Double = 0, shy: Double = 0) -> Transform { + return Transform(m12: shy, m21: shx) + } + + public class func shear(_ shx: Double, _ shy: Double) -> Transform { return Transform(m12: shy, m21: shx) } @@ -61,10 +105,18 @@ public final class Transform { return Transform(m11: acos, m12: asin, m21: -asin, m22: acos) } - public class func rotate(angle: Double, x: Double, y: Double) -> Transform { + public class func rotate(_ angle: Double) -> Transform { + return rotate(angle: angle) + } + + public class func rotate(angle: Double, x: Double = 0, y: Double = 0) -> Transform { return Transform.move(dx: x, dy: y).rotate(angle: angle).move(dx: -x, dy: -y) } + public class func rotate(_ angle: Double, _ x: Double, _ y: Double) -> Transform { + return rotate(angle: angle, x: x, y: y) + } + public func concat(with: Transform) -> Transform { let nm11 = with.m11 * m11 + with.m12 * m21 let nm21 = with.m21 * m11 + with.m22 * m21