imzyf / ios-swift-learning-notes

📝 iOS Swift Learning Notes - see Issues
MIT License
0 stars 0 forks source link

CGPoint 计算两点的距离 #37

Open imzyf opened 6 years ago

imzyf commented 6 years ago

https://stackoverflow.com/questions/1906511/how-to-find-the-distance-between-two-cg-points

CGFloat distance = hypotf(p1.x - p2.x, p1.y - p2.y);
extension CGPoint {

    func distance(toPoint p:CGPoint) -> CGFloat {
        return sqrt(pow(x - p.x, 2) + pow(y - p.y, 2))
    }
}