简体   繁体   English

计算圆Swift SpriteKit边缘上两点之间的角度

[英]calculating angle between two points on edge of circle Swift SpriteKit

How would you calculate the degrees between two points on the edge of a circle in Swift. 你如何计算Swift中圆圈边缘上两点之间的度数。

在此输入图像描述

Given points p1 , p2 on a circle with center center , you would compute the difference vectors first: 由于点p1p2与中央的圆center ,你会首先计算该差矢量:

let v1 = CGVector(dx: p1.x - center.x, dy: p1.y - center.y)
let v2 = CGVector(dx: p2.x - center.x, dy: p2.y - center.y)

Then 然后

let angle = atan2(v2.dy, v2.dx) - atan2(v1.dy, v1.dx)

is the (directed) angle between those vectors in radians, and 是弧度中这些向量之间的(有向)角度,和

var deg = angle * CGFloat(180.0 / M_PI)

the angle in degrees. 以度为单位的角度。 The computed value can be in the range -360 .. 360, so you might want to normalize it to the range 0 <= deg < 360 with 计算出的值可以在-360 .. 360范围内,因此您可能希望将其标准化为0 <= deg <360范围

if deg < 0 { deg += 360.0 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM