Rotating a CGPoint around another CGPoint(围绕另一个 CGPoint 旋转一个 CGPoint)
问题描述
好的,所以我想将 CGPoint(A) 围绕 CGPoint(B) 旋转 50 度,有什么好的方法吗?
Okay so I want to rotate CGPoint(A) 50 degrees around CGPoint(B) is there a good way to do that?
CGPoint(A) = CGPoint(x: 50, y: 100)
CGPoint(A) = CGPoint(x: 50, y: 100)
CGPoint(B) = CGPoint(x: 50, y: 0)
CGPoint(B) = CGPoint(x: 50, y: 0)
这是我想做的:
推荐答案
这真的是一道数学题.在 Swift 中,你想要这样的东西:
This is really a maths question. In Swift, you want something like:
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)
}
有很多方法可以简化这一点,这是对 CGPoint 的扩展的完美案例,但为了清楚起见,我将其保留为冗长.
There are lots of ways to simplify this, and it's a perfect case for an extension to CGPoint, but I've left it verbose for clarity.     
这篇关于围绕另一个 CGPoint 旋转一个 CGPoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:围绕另一个 CGPoint 旋转一个 CGPoint
				
        
 
            
        基础教程推荐
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
 - navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
 - Android Volley - 如何动画图像加载? 2022-01-01
 - SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
 - 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
 - 如何比较两个 NSDate:哪个是最近的? 2022-01-01
 - Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
 - 如何将图像从一项活动发送到另一项活动? 2022-01-01
 - UIImage 在开始时不适合 UIScrollView 2022-01-01
 - Play 商店的设备兼容性问题 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				