Skip to content

Instantly share code, notes, and snippets.

@smic
Created June 17, 2016 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smic/816d0674aaf3b20a22f49648fafca21c to your computer and use it in GitHub Desktop.
Save smic/816d0674aaf3b20a22f49648fafca21c to your computer and use it in GitHub Desktop.
Operator conflict if using the * operator for CGPoints
import Cocoa
extension CGPoint {
public func length() -> CGFloat {
return hypot(self.x, self.y)
}
}
infix operator ⋅ { associativity left precedence 140 }
public func ⋅ (lhs: CGPoint, rhs: CGPoint) -> CGFloat {
return lhs.x * rhs.x + lhs.y * rhs.y
}
public func * (lhs: CGPoint, rhs: CGPoint) -> CGFloat {
return lhs.x * rhs.x + lhs.y * rhs.y
}
public func / (lhs: CGPoint, rhs: CGFloat) -> CGPoint {
return CGPoint(x:lhs.x / rhs, y:lhs.y / rhs)
}
let d = CGPoint(x: 1.0, y: 1.5)
let d2 = CGPoint(x: 2.0, y: 1.5)
var fraction = (d ⋅ d2) / d.length() / d2.length()
var fraction = (d * d2) / d.length() / d2.length() // <-- gives an error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment