-2
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. calculated the slope of each line
  2. calculated angle by tan inverse
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Manohar
  • 55
  • 2
  • 6

1 Answers1

1

Use the atan2() function. From the manual page:

   #include <math.h>

   double
   atan2(double y, double x);

The atan2() function computes the principal value of the arc tangent of y/x, using the signs of both arguments to determine the quadrant of the return value.

To do this for the three points you'll need to call atan2() twice: once to find the angle of AB, and once for the angle of BC. Take the difference between these two to find the angle between AB and 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;

Note that this is assuming that B is the "center" point of the angle you're interested in. Adjust appropriately if I've assumed wrongly.