Inner angle between two lines

2019-01-23 06:07发布

I have two lines: Line1 and Line2. Each line is defined by two points (P1L1(x1, y1), P2L1(x2, y2) and P1L1(x1, y1), P2L3(x2, y3)). I want to know the inner angle defined by these two lines.

For do it I calculate the angle of each line with the abscissa:

double theta1 = atan(m1) * (180.0 / PI);
double theta2 = atan(m2) * (180.0 / PI);

After to know the angle I calculate the following:

double angle = abs(theta2 - theta1);

The problem or doubt that I have is: sometimes I get the correct angle but sometimes I get the complementary angle (for me outer). How can I know when subtract 180º to know the inner angle? There is any algorithm better to do that? Because I tried some methods: dot product, following formula:

result = (m1 - m2) / (1.0 + (m1 * m2));

But always I have the same problem; I never known when I have the outer angle or the inner angle!

8条回答
Juvenile、少年°
2楼-- · 2019-01-23 06:23

I hope I understand your question correctly as wanting the acute angle rather than the obtuse angle of the intersection of two lines. Am I correct?

Acute and obtuse angles of an intersection are 180 deg complements of each other. i.e.

 acute + obtuse = PI.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/atan.html exhibits that an atan is asymptotic at +/- pi/2.

Therefore, the max difference between two results of atan is pi or 180 deg, whether you use the +/- notation or positive 0 to pi notation of a gradient.

Consider the following pseudocode:

acuteAngle(m1, m2){
  a = atan(m1) - atan(m2);

  // if obtuse get the complementary acute angle:
  if (a>PI/2) 
    a = PI - a;
  return a;
} 

The function acuteAngle illustrates what you need to do, mathematically.

However, it cannot be used for values of angles in the neighbourhood of PI/2 because binary comparisons of angles with results in that neighbourhood is questionable whether an obtuse or acute angle is represented.

Therefore, we have to compare the coordinates of the points of the two lines. We find out whether the 3rd line formed from [(x2,y2)(x3,y3)] is shorter, equal or longer than the hypothetical hypotenuse.

By virtue of Pythagoras' theorem, A hypotenuse is formed if the angle is exactly PI/2 or 90 deg. Let's call his hypothetical hypotenuse line L3Hypo.

By geometrical visualisation in your mind,

  • If the 3rd line is longer than L3Hypo, the angle is obtuse.
  • If shorter, the angle is acute.
  • Otherwise, perfect 90.

Therefore,

L1.lengthSquared = sq(x2-x1) + sq(y2-y1)
L2.lengthSquared = sq(x3-x1) + sq(y3-y1)
L3Hypo.lengthSquared = L1.lengthSquared + L2.lengthSquared
L3.lengthSquared = sq(x3-x2) + sq(y3-y2)

Therefore, the following pseudo-code,

struct Point{
  double x, y;
}

// no need to struct, for clarity only
struct Line{
  double lengthSquared;
}

#define sq(n) (n*n)
int isObtuse(Point P1, P2, P3){
  Line L1, L2, L3, L3Hypo;

  L1.lengthSquared = sq(P2.x-P1.x) + sq(P2.y-P1.y);
  L2.lengthSquared = sq(P3.x-P1.x) + sq(P3.y-P1.y);
  L3Hypo.lengthSquared = L1.lengthSquared + L2.lengthSquared;
  L3.lengthSquared = sq(P3.x-P2.x) + sq(P3.y-P2.y);

  if (L3>L3Hypo) return 1; //obtuse
  else if (L3<L3Hypo) return -1; //acute
  else return 0;
}

Presuming you already have the function getGradient(Point P, Q):

double m1m2 = getGradient(P1,P2);
double m1m3 = getGradient(P1,P3);
double a = Abs(atan(m1m2) - atan(m1m3));
if (isObtuse(P1, P2, P3)>0)
  a = PI - a;

I may have committed some typo mistakes in the pseudo-code (hopefully not) but I demonstrated the gist of the concept. If so, someone could be so kind to edit away the typos.

Further However, after mulling over it, I find that the struggle for precision pivots on its weakest link due to the directive

#define PI 3.14159blah..blah..blah.

So, we might as well save all the trouble and simply do this:

double m1m2 = getGradient(P1,P2);
double m1m3 = getGradient(P1,P3);
double a = Abs(atan(m1m2) - atan(m1m3));
double b = PI - a;
return min(a, b);//the smaller of the two is the acute
查看更多
Bombasti
3楼-- · 2019-01-23 06:26

If you want in between angle in 0 degree to 360 degree then use following code; Its fully tested and functional:

static inline CGFloat angleBetweenLinesInRadians(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) {
double angle1 = atan2(line1Start.y-line1End.y, line1Start.x-line1End.x);
double angle2 = atan2(line2Start.y-line2End.y, line2Start.x-line2End.x);
double result = (angle2-angle1) * 180 / 3.14;
if (result<0) {
    result+=360;
}
return result;

}

Note: Rotation will be clockwise;

查看更多
相关推荐>>
4楼-- · 2019-01-23 06:31

Inner angle between 2 vectors (v1, v2) = arc cos ( inner product(v1,v2) / (module(v1) * module(v2)) ).

Where inner product(v1,v2) = xv1*xv2 + yv1*yv2

module(v) = sqrt(pow(xv,2) + pow(yv,2))

So, the answer of your question is implemented on the following example:

#define PI   3.14159258

int main()
{
    double x1,y1,x2,y2,y3;
    double m1, m2;
    double mod1, mod2, innerp, angle;

    cout << "x1 :";
    cin >> x1;
    cout << "y1 :";
    cin >> y1;
    cout << "x2 :";
    cin >> x2;
    cout << "y2 :";
    cin >> y2;
    cout << "y3 :";
    cin >> y3;

    m1 = atan((y2-y1)/(x2-x1)) * 180 / PI;
    m2 = atan((y3-y1)/(x2-x1)) * 180 / PI;

    mod1   = sqrt(pow(y2-y1,2)+pow(x2-x1,2));
    mod2   = sqrt(pow(y3-y1,2)+pow(x2-x1,2));
    innerp = (x2-x1)*(x2-x1) + (y2-y1)*(y3-y1);
    angle  = acos(innerp / (mod1 * mod2)) * 180 / PI;

    cout << "m1 : " << m1 << endl;
    cout << "m2 : " << m2 << endl;
    cout << "angle : " << angle << endl;
}
查看更多
狗以群分
5楼-- · 2019-01-23 06:33

If you use abolute value you will always get the acute angle. That is tangent theta = abs value of m1-m2 over (1 +m1 * m2). If you take inverse tangent your answer will be in radians or degrees however the calculator is set. Sorry this isnt programming lingo, I am a math teacher, not a programmer...

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-23 06:33

Getting the outer angle vs the inner angle is determined entirely by the order of your subtractions (think about it). You need to subtract the smaller theta from the larger in order to reliably always get the inner angle. You also probably want to use the atan2 function because of the type of data you're expecting.

查看更多
男人必须洒脱
7楼-- · 2019-01-23 06:40
if (result > 180)
{
     result = 360 - result;
}

That way it will always be the inner angle. Just add it after you get result.

查看更多
登录 后发表回答