围绕另一个CGPoint旋转CGPoint

Ben*_*906 5 math rotation degrees cgpoint swift

好的,所以我想在CGPoint周围旋转CGPoint(A)50度(B)有一个很好的方法吗?

CGPoint(A)= CGPoint(x: 50, y: 100)

CGPoint(B)= CGPoint(x: 50, y: 0)

这就是我想要做的事情:

插图

Gri*_*mxn 10

这真是一个数学问题.在Swift中,您需要以下内容:

func rotatePoint(target: CGPoint, aroundOrigin origin: CGPoint, byDegrees: CGFloat) -> CGPoint {
    let dx = target.x - origin.x
    let dy = target.y - origin.y
    let radius = sqrt(dx * dx + dy * dy)
    let azimuth = atan2(dy, dx) // in radians
    let newAzimuth = azimuth + byDegrees * CGFloat(M_PI / 180.0) // convert it to radians
    let x = origin.x + radius * cos(newAzimuth)
    let y = origin.y + radius * sin(newAzimuth)
    return CGPoint(x: x, y: y)
}
Run Code Online (Sandbox Code Playgroud)

有很多方法可以简化这个,它是扩展的完美案例CGPoint,但为了清晰起见,我把它留下了冗长.