Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Operator overloads to do scalar vector math on CGPoint, CGSize, CGVector.
import CoreGraphics
protocol VectorType {
typealias Element
var xElement: Element { get }
var yElement: Element { get }
class func buildFrom(#x: Element, y: Element) -> Self
init<T: VectorType where T.Element == Element>(fromVector: T)
}
func +<T: VectorType where T.Element == CGFloat>(lhs: T, rhs: T) -> T {
return T.buildFrom(x: lhs.xElement + rhs.xElement, y: lhs.yElement + rhs.yElement)
}
func +<T: VectorType where T.Element == CGFloat>(lhs: T, rhs: T.Element) -> T {
return T.buildFrom(x: lhs.xElement + rhs, y: lhs.yElement + rhs)
}
func +<T: VectorType where T.Element == CGFloat>(lhs: T, rhs: (T.Element, T.Element)) -> T {
return T.buildFrom(x: lhs.xElement + rhs.0, y: lhs.yElement + rhs.1)
}
func -<T: VectorType where T.Element == CGFloat>(lhs: T, rhs: T) -> T {
return T.buildFrom(x: lhs.xElement - rhs.xElement, y: lhs.yElement - rhs.yElement)
}
func -<T: VectorType where T.Element == CGFloat>(lhs: T, rhs: T.Element) -> T {
return T.buildFrom(x: lhs.xElement - rhs, y: lhs.yElement - rhs)
}
func -<T: VectorType where T.Element == CGFloat>(lhs: T, rhs: (T.Element, T.Element)) -> T {
return T.buildFrom(x: lhs.xElement - rhs.0, y: lhs.yElement - rhs.1)
}
func *<T: VectorType where T.Element == CGFloat>(lhs: T, rhs: T.Element) -> T {
return T.buildFrom(x: lhs.xElement * rhs, y: lhs.yElement * rhs)
}
func *<T: VectorType where T.Element == CGFloat>(lhs: T, rhs: (T.Element, T.Element)) -> T {
return T.buildFrom(x: lhs.xElement * rhs.0, y: lhs.yElement * rhs.1)
}
func /<T: VectorType where T.Element == CGFloat>(lhs: T, rhs: T.Element) -> T {
return T.buildFrom(x: lhs.xElement / rhs, y: lhs.yElement / rhs)
}
func /<T: VectorType where T.Element == CGFloat>(lhs: T, rhs: (T.Element, T.Element)) -> T {
return T.buildFrom(x: lhs.xElement / rhs.0, y: lhs.yElement / rhs.1)
}
prefix func -<T: VectorType where T.Element == CGFloat>(lhs: T) -> T {
return T.buildFrom(x: -lhs.xElement, y: -lhs.yElement)
}
extension CGPoint: VectorType {
var xElement: CGFloat { return x }
var yElement: CGFloat { return y }
static func buildFrom(#x: CGFloat, y: CGFloat) -> CGPoint {
return CGPoint(x: x, y: y)
}
init<T: VectorType where T.Element == CGFloat>(fromVector: T) {
self.init(x: fromVector.xElement, y: fromVector.yElement)
}
}
extension CGSize: VectorType {
var xElement: CGFloat { return width }
var yElement: CGFloat { return height }
static func buildFrom(#x: CGFloat, y: CGFloat) -> CGSize {
return CGSize(width: x, height: y)
}
init<T: VectorType where T.Element == CGFloat>(fromVector: T) {
self.init(width: fromVector.xElement, height: fromVector.yElement)
}
}
extension CGVector: VectorType {
var xElement: CGFloat { return dx }
var yElement: CGFloat { return dy }
static func buildFrom(#x: CGFloat, y: CGFloat) -> CGVector {
return CGVector(dx: x, dy: y)
}
init<T: VectorType where T.Element == CGFloat>(fromVector: T) {
self.init(dx: fromVector.xElement, dy: fromVector.yElement)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.