Skip to content

Instantly share code, notes, and snippets.

@FredrikSjoberg
Created May 12, 2016 20:02
Show Gist options
  • Save FredrikSjoberg/ced4ad5103863ab95dc8b49bdfd99eb2 to your computer and use it in GitHub Desktop.
Save FredrikSjoberg/ced4ad5103863ab95dc8b49bdfd99eb2 to your computer and use it in GitHub Desktop.
extension CGPoint : Hashable {
func distance(point: CGPoint) -> Float {
let dx = Float(x - point.x)
let dy = Float(y - point.y)
return sqrt((dx * dx) + (dy * dy))
}
public var hashValue: Int {
// iOS Swift Game Development Cookbook
// https://books.google.se/books?id=QQY_CQAAQBAJ&pg=PA304&lpg=PA304&dq=swift+CGpoint+hashvalue&source=bl&ots=1hp2Fph274&sig=LvT36RXAmNcr8Ethwrmpt1ynMjY&hl=sv&sa=X&ved=0CCoQ6AEwAWoVChMIu9mc4IrnxgIVxXxyCh3CSwSU#v=onepage&q=swift%20CGpoint%20hashvalue&f=false
return x.hashValue << 32 ^ y.hashValue
}
}
func ==(lhs: CGPoint, rhs: CGPoint) -> Bool {
return lhs.distance(rhs) < 0.000001 //CGPointEqualToPoint(lhs, rhs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment