代码之家  ›  专栏  ›  技术社区  ›  Manohar

xcode三点之间的角度

  •  -2
  • Manohar  · 技术社区  · 10 年前
    CGPoint pointA = [self.appDelegate.points[0] CGPointValue];//first point
    CGPoint pointB = [self.appDelegate.points[1] CGPointValue];// second point
    CGPoint pointC = [self.appDelegate.points[2] CGPointValue];//third point
    CGFloat slopeAB = (pointB.y - pointA.y)/(pointB.x - pointA.x);//slope ab
    CGFloat slopeBC = (pointC.y - pointB.y)/(pointC.x - pointB.x);//slope bc
    self.ang=(slopeAB-slopeBC)/(1+(slopeAB)*(slopeBC));//slope
    CGFloat finalAngle = atanf(self.ang);// angle tan inverse slope
    CGFloat angle = (finalAngle * (180.0/M_PI));
    NSLog(@"The angle is: %.2f degrees",angle);
    
    1. 计算每条线的斜率
    2. 通过tan逆计算角度
    1 回复  |  直到 10 年前
        1
  •  1
  •   user149341 user149341    10 年前

    使用 atan2() 作用从手册页面:

       #include <math.h>
    
       double
       atan2(double y, double x);
    

    函数的作用是计算 y/x ,使用两个参数的符号确定 返回值。

    为了 您需要调用的点 atan2() 两次:一次求AB角,一次求BC角。取这两者之间的差值,找出AB和BC之间的角度:

    double angle_ab = atan2(pointA.y - pointB.y, pointA.x - pointB.x);
    double angle_cb = atan2(pointC.y - pointB.y, pointC.x - pointB.x);
    double angle_abc = angle_ab - angle_cb;
    

    注意,这是假设B是您感兴趣的角度的“中心”点。如果我假设错误,请适当调整。

    推荐文章