ios 从2个位置获取Angular

qybjjes1  于 2023-02-01  发布在  iOS
关注(0)|答案(7)|浏览(138)

我有两个物体,当我移动一个时,我想得到另一个的Angular 。
例如:

Object1X = 211.000000, Object1Y = 429.000000
Object2X = 246.500000, Object2Y = 441.500000

我已经尝试了以下和太阳下的每一个变化:

double radians = ccpAngle(Object1,Object2);
double degrees = ((radians * 180) / Pi);

但是我只得到了2.949023,在我想要的地方,比如45度。

5hcedyr0

5hcedyr01#

另一个答案有用吗?
How to map atan2() to degrees 0-360
我是这样写的:

- (CGFloat) pointPairToBearingDegrees:(CGPoint)startingPoint secondPoint:(CGPoint) endingPoint
{
    CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
    float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
    float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
    bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
    return bearingDegrees;
}

运行代码:

CGPoint p1 = CGPointMake(10, 10);
CGPoint p2 = CGPointMake(20,20);

CGFloat f = [self pointPairToBearingDegrees:p1 secondPoint:p2];

这将返回45。
希望这个有用。

vsikbqxv

vsikbqxv2#

以下是我如何在Swift中为感兴趣的人做的,它基于@bshirley上面的答案,并进行了一些修改,以帮助匹配calayer旋转系统:

extension CGFloat {
    var degrees: CGFloat {
        return self * CGFloat(180) / .pi
    }
}

extension CGPoint {
    func angle(to comparisonPoint: CGPoint) -> CGFloat {
        let originX = comparisonPoint.x - x
        let originY = comparisonPoint.y - y
        let bearingRadians = atan2f(Float(originY), Float(originX))
        var bearingDegrees = CGFloat(bearingRadians).degrees

        while bearingDegrees < 0 {
            bearingDegrees += 360
        }

        return bearingDegrees
    }
}

这将提供如下所示的坐标系:

90
180              0
        270

用法:

point.angle(to: point2)
CGPoint.zero.angle(to: CGPoint(x: 0, y: 1)) // 90
ttisahbt

ttisahbt3#

我修改了@tomas的解决方案,使之更加精简,很可能(对我来说)这个数学运算会被频繁调用。
在我的化身中,你必须自己计算这两个点之间的差(或者如果你幸运的话,(0,0)已经是你的一个点了)。计算的值是从(0,0)开始的点的方向。是的,这很简单,如果你真的想的话,你可以内联它。我更喜欢可读性更强的代码。
我还将其转换为函数调用:

CGFloat CGPointToDegree(CGPoint point) {
  // Provides a directional bearing from (0,0) to the given point.
  // standard cartesian plain coords: X goes up, Y goes right
  // result returns degrees, -180 to 180 ish: 0 degrees = up, -90 = left, 90 = right
  CGFloat bearingRadians = atan2f(point.y, point.x);
  CGFloat bearingDegrees = bearingRadians * (180. / M_PI);
  return bearingDegrees;
}

如果你不想要负值,你需要自己转换它。负值对我来说很好--不需要做不必要的计算。
我在cocos2d环境中使用它,我是这样称呼它的:(数学上,我们平移平面以使p0为原点。因此从p1中减去p0p0-p0 = {0,0})。平移平面时Angular 不变。)

CGPoint p0 = self.position;
CGPoint p1 = other.position;
CGPoint pnormal = ccpSub(p1, p0);
CGFloat angle = CGPointToDegree(pnormal);

ccpSub是由cocos2d提供的,它是元组的减法-如果没有可用的元组,您可以自己执行此操作
顺便说一句:用CG___命名方案来命名这个方法通常是不礼貌的,它把这个函数标识为CoreGraphics的一部分--所以如果你想把它重命名为MyConvertCGPointToBearing()FredLovesWilma(),那么你应该这样做。

gojuced7

gojuced74#

Swift 5中的Tomas' answer

func angle(between starting: CGPoint, ending: CGPoint) -> CGFloat {
    let center = CGPoint(x: ending.x - starting.x, y: ending.y - starting.y)
    let radians = atan2(center.y, center.x)
    let degrees = radians * 180 / .pi
    return degrees > 0 ? degrees : 360 + degrees
}
xbp102n0

xbp102n05#

两点之间没有夹角。如果你想知道从原点(0,0)到物体的向量之间的夹角,使用标量(点)积:

theta = arccos ( (veca dot vecb) / ( |veca| * |vecb| )

你使用的math标准库肯定提供了反余弦、标量积和长度的函数。

h79rfbju

h79rfbju6#

角的顶点是点(0,0)。
考虑对象1X =x1......对象2Y =y2。

Angle(object1-object2) = 
   90       * (  (1 + sign(x1)) * (1 - sign(y1^2))
               - (1 + sign(x2)) * (1 - sign(y2^2)) )
 + 45       * (  (2 + sign(x1)) * sign(y1)
               - (2 + sign(x2)) * sign(y2)         )
 + 180/pi() * sign(x1*y1) * atan( (abs(x1) - abs(y1)) / (abs(x1) + abs(y1)) )
 - 180/pi() * sign(x2*y2) * atan( (abs(x2) - abs(y2)) / (abs(x2) + abs(y2)) )
wr98u20j

wr98u20j7#

将离开这里。更正代码,加上逆时针旋转轴90度。我用它来触摸。viewCenter是刚刚中心的看法

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let location = touch.location(in: self)            
        guard let viewCenter = self.viewCenter else { return }
        let angle = angle(between:  CGPoint(x: location.x, y: location.y) , ending:viewCenter)
        print(angle)
      }
}

func angle(between starting: CGPoint, ending: CGPoint) -> CGFloat {
    let center = CGPoint(x: ending.x - starting.x, y: ending.y - starting.y)
    let angle90 = deg2rad(90)
    //Rotate axis by 90 degrees counter clockwise
    let rotatedX = center.x * cos(angle90) + center.y * sin(angle90)
    let rotatedY = -center.x * sin(angle90) + center.y * cos(angle90)
    let radians = atan2(rotatedY, rotatedX)
    let degrees = radians * 180 / .pi
    return degrees > 0 ? degrees : degrees + 360
}
    
func deg2rad(_ number: CGFloat) -> CGFloat {
    return number * .pi / 180
}

相关问题