计算圆弧边缘上两点之间的角度Swift SpriteKit [英] calculating angle between two points on edge of circle Swift SpriteKit

查看:409
本文介绍了计算圆弧边缘上两点之间的角度Swift SpriteKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift中如何计算圆弧边缘上两个点之间的度。

How would you calculate the degrees between two points on the edge of a circle in Swift.

推荐答案

给出点 p1 p2 在中心为 center
的圆上,您需要计算先求差向量:

Given points p1, p2 on a circle with center center, you would compute the difference vectors first:

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)

然后

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)

角度(度)。计算值的范围可以是-360 .. 360,因此您
可能希望将其规格化为0 <== deg <

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

if deg < 0 { deg += 360.0 }

这篇关于计算圆弧边缘上两点之间的角度Swift SpriteKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆