|
///Shorthand for assigning value only if value is not nil |
|
infix operator ?= |
|
public func ?= <T>(left: inout T, right: T?) { |
|
if let right = right { |
|
left = right |
|
} |
|
} |
|
|
|
let fl0 = CGFloat(0) |
|
|
|
public protocol Operatable { |
|
var float: CGFloat {get} |
|
init(value: CGFloat) |
|
} |
|
|
|
extension Float: Operatable { |
|
public var float: CGFloat {return CGFloat(self)} |
|
public init(value: CGFloat) {self = Float(value)} |
|
} |
|
extension Double: Operatable { |
|
public var float: CGFloat {return CGFloat(self)} |
|
public init(value: CGFloat) {self = Double(value)} |
|
} |
|
|
|
extension Int: Operatable { |
|
public var float: CGFloat {return CGFloat(self)} |
|
public init(value: CGFloat) {self = Int(value)} |
|
} |
|
extension CGFloat: Operatable { |
|
public var float: CGFloat {return CGFloat(self)} |
|
public init(value: CGFloat) {self = value} |
|
} |
|
|
|
public protocol Tuple { |
|
var first : CGFloat {get} |
|
var second: CGFloat {get} |
|
init(_ first: CGFloat, _ second: CGFloat) |
|
} |
|
|
|
extension CGPoint: Tuple { |
|
public var first: CGFloat {return self.x} |
|
public var second: CGFloat {return self.y} |
|
public init(_ first: CGFloat, _ second: CGFloat) { |
|
self.x = first |
|
self.y = second |
|
} |
|
public var size: CGSize {return sz(self.x, self.y)} |
|
} |
|
|
|
extension CGSize: Tuple { |
|
public var first: CGFloat {return self.width} |
|
public var second: CGFloat {return self.height} |
|
public init(_ first: CGFloat, _ second: CGFloat) { |
|
self.width = first |
|
self.height = second |
|
} |
|
public var point: CGPoint {return pt(self.width, self.height)} |
|
} |
|
|
|
extension CGVector: Tuple { |
|
public var first: CGFloat {return self.dx} |
|
public var second: CGFloat {return self.dy} |
|
public init(_ first: CGFloat, _ second: CGFloat) { |
|
self.dx = first |
|
self.dy = second |
|
} |
|
} |
|
|
|
//Numeric to Numeric operators ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– |
|
public func +<T: Operatable>(left: Operatable, right: T) -> T { |
|
return T(value: left.float + right.float) |
|
} |
|
|
|
public func -<T: Operatable>(left: Operatable, right: T) -> T { |
|
return T(value: left.float - right.float) |
|
} |
|
|
|
public func *<T: Operatable>(left: Operatable, right: T) -> T { |
|
return T(value: left.float * right.float) |
|
} |
|
|
|
public func /<T: Operatable>(left: Operatable, right: T) -> T { |
|
var result = fl0 |
|
|
|
//Prevent number from being divided by 0 |
|
if right.float != 0 { |
|
result = left.float / right.float |
|
} |
|
|
|
return T(value: result) |
|
} |
|
|
|
//Forces swift to return 0 even when both left and right are the same type // alt + / to get the symbol |
|
infix operator ÷ |
|
public func ÷<T: Operatable>(left: Operatable, right: T) -> T { |
|
var result = fl0 |
|
|
|
//Prevent number from being divided by 0 |
|
if right.float != 0 { |
|
result = left.float / right.float |
|
} |
|
|
|
return T(value: result) |
|
} |
|
|
|
//Assignment |
|
//swift doesn't allow overriding the "=" operator, use alt + x to get the symbol |
|
infix operator ≈ |
|
public func ≈<T: Operatable>(left: inout T, right: Operatable) { |
|
left = T(value: right.float) |
|
} |
|
|
|
public func +=<T: Operatable>(left: inout T, right: Operatable) { |
|
left = T(value: left.float + right.float) |
|
} |
|
|
|
public func -=<T: Operatable>(left: inout T, right: Operatable) { |
|
left = T(value: left.float - right.float) |
|
} |
|
|
|
public func *=<T: Operatable>(left: inout T, right: Operatable) { |
|
left = T(value: left.float * right.float) |
|
} |
|
|
|
public func /=<T: Operatable>(left: inout T, right: Operatable) { |
|
var result = fl0 |
|
|
|
//Prevent number from being divided by 0 |
|
if right.float != 0 { |
|
result = left.float / right.float |
|
} |
|
|
|
left = T(value: result) |
|
} |
|
|
|
//Tuple to Tuple Operators –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– |
|
public func +<T: Tuple>(left: T, right: Tuple) -> T { |
|
return T(left.first + right.first, left.second + right.second) |
|
} |
|
|
|
public func -<T: Tuple>(left: T, right: Tuple) -> T { |
|
return T(left.first - right.first, left.second - right.second) |
|
} |
|
|
|
public prefix func -<T: Tuple>(value: Tuple) -> T { |
|
return T(-value.first, -value.second) |
|
} |
|
|
|
public func *<T: Tuple>(left: T, right: Tuple) -> T { |
|
return T(left.first * right.first, left.second * right.second) |
|
} |
|
|
|
public func /<T: Tuple>(left: T, right: Tuple) -> T { |
|
var firstResult = fl0 |
|
var secondResult = fl0 |
|
|
|
//Prevent number from being divided by 0 |
|
if right.first != 0 { |
|
firstResult = left.first / right.first |
|
} |
|
|
|
if right.second != 0 { |
|
secondResult = left.second / right.second |
|
} |
|
|
|
return T(firstResult, secondResult) |
|
} |
|
|
|
//Assignment |
|
//swift doesn't allow overriding the "=" operator, use alt + x to get the symbol |
|
public func ≈<T: Tuple>(left: inout T, right: Tuple) { |
|
left = T(right.first, right.second) |
|
} |
|
|
|
public func +=<T: Tuple>(left: inout T, right: Tuple) { |
|
left = T(left.first + right.first, left.second + right.second) |
|
} |
|
|
|
public func -=<T: Tuple>(left: inout T, right: Tuple) { |
|
left = T(left.first - right.first, left.second - right.second) |
|
} |
|
|
|
public func *=<T: Tuple>(left: inout T, right: Tuple) { |
|
left = T(left.first * right.first, left.second * right.second) |
|
} |
|
|
|
public func /=<T: Tuple>(left: inout T, right: Tuple) { |
|
var firstResult = fl0 |
|
var secondResult = fl0 |
|
|
|
//Prevent number from being divided by 0 |
|
if right.first != 0 { |
|
firstResult = left.first / right.first |
|
} |
|
|
|
if right.second != 0 { |
|
secondResult = left.second / right.second |
|
} |
|
|
|
left = T(firstResult, secondResult) |
|
} |
|
|
|
//Tuple to Numeric Operators ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– |
|
public func +<T: Tuple>(left: T, right: Operatable) -> T { |
|
return T(left.first + right.float, left.second + right.float) |
|
} |
|
|
|
public func -<T: Tuple>(left: T, right: Operatable) -> T { |
|
return T(left.first - right.float, left.second - right.float) |
|
} |
|
|
|
public func *<T: Tuple>(left: T, right: Operatable) -> T { |
|
return T(left.first * right.float, left.second * right.float) |
|
} |
|
|
|
public func /<T: Tuple>(left: T, right: Operatable) -> T { |
|
//Prevent number from being divided by 0 |
|
if right.float == 0 { |
|
return T(0, 0) |
|
} else { |
|
return T(left.first / right.float, left.second / right.float) |
|
} |
|
} |
|
|
|
//Assignment |
|
//swift doesn't allow overriding the "=" operator, use alt + x to get the symbol |
|
public func ≈<T: Tuple>(left: inout T, right: Operatable) { |
|
left = T(right.float, right.float) |
|
} |
|
|
|
public func +=<T: Tuple>(left: inout T, right: Operatable) { |
|
left = T(left.first + right.float, left.second + right.float) |
|
} |
|
|
|
public func -=<T: Tuple>(left: inout T, right: Operatable) { |
|
left = T(left.first - right.float, left.second - right.float) |
|
} |
|
|
|
public func *=<T: Tuple>(left: inout T, right: Operatable) { |
|
left = T(left.first * right.float, left.second * right.float) |
|
} |
|
|
|
public func /=<T: Tuple>(left: inout T, right: Operatable) { |
|
//Prevent number from being divided by 0 |
|
if right.float == 0 { |
|
left = T(0, 0) |
|
} else { |
|
left = T(left.first / right.float, left.second / right.float) |
|
} |
|
} |
|
|
|
//Extra Operators ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– |
|
|
|
public func max<T: Tuple>(_ L: T, _ R: Tuple) -> T { |
|
return T(max(L.first, R.first), max(L.second, R.second)) |
|
} |
|
|
|
public func min<T: Tuple>(_ L: T, _ R: Tuple) -> T { |
|
return T(min(L.first, R.first), min(L.second, R.second)) |
|
} |
|
|
|
prefix operator √ |
|
public prefix func √<T: Operatable>(value: T) -> T { |
|
return T(value: sqrt(value.float)) |
|
} |
|
|
|
|
|
infix operator ** |
|
public func **<T: Operatable>(left: T, right: Operatable) -> T { |
|
return T(value: pow(left.float, right.float)) |
|
} |
|
|
|
public func %<T: Operatable>(left: T, right: Operatable) -> T { |
|
return T(value: left.float.truncatingRemainder(dividingBy: right.float)) |
|
} |
|
|
|
public prefix func √<T: Tuple>(value: T) -> T { |
|
return T(sqrt(value.first), sqrt(value.second)) |
|
} |
|
|
|
public func **<T: Tuple>(left: T, right: Operatable) -> T { |
|
return T(pow(left.first, right.float), pow(left.second, right.float)) |
|
} |
|
|
|
public func %<T: Tuple>(left: T, right: Operatable) -> T { |
|
return T(left.first.truncatingRemainder(dividingBy: right.float), left.second.truncatingRemainder(dividingBy: right.float)) |
|
} |
|
|
|
public func %<T: Tuple>(left: T, right: Tuple) -> T { |
|
return T(left.first.truncatingRemainder(dividingBy: right.first), left.second.truncatingRemainder(dividingBy: right.second)) |
|
} |