15

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!

dandan78
  • 12,242
  • 12
  • 61
  • 73
osanchezmon
  • 524
  • 1
  • 4
  • 18
  • 1
    How do you know when you have the correct angle; i.e. how would you define an inner angle? I think that'll lead you to the answer. – Owen S. May 31 '10 at 23:01
  • I want an angle defined between 0º and 180º; if I draw and acute angle I want an angle between 0º and 90º. The problem is I can do it if a line has slope 0 (parallel to abscissa) but I can´t do it (or I don´t know how) when a line has other positive or negative slope :-( – osanchezmon May 31 '10 at 23:09

8 Answers8

23

I think what you're looking for is the inner product (you may also want to look over the dot product entry) of the two angles. In your case, that's given by:

float dx21 = x2-x1;
float dx31 = x3-x1;
float dy21 = y2-y1;
float dy31 = y3-y1;
float m12 = sqrt( dx21*dx21 + dy21*dy21 );
float m13 = sqrt( dx31*dx31 + dy31*dy31 );
float theta = acos( (dx21*dx31 + dy21*dy31) / (m12 * m13) );

Answer is in radians.

EDIT: Here's a complete implementation. Substitute the problematic values in p1, p2, and p3 and let me know what you get. The point p1 is the vertex where the two lines intersect, in accordance with your definition of the two lines.

#include <math.h>
#include <iostream>

template <typename T> class Vector2D
{
private:
    T x;
    T y;

public:
    explicit Vector2D(const T& x=0, const T& y=0) : x(x), y(y) {}
    Vector2D(const Vector2D&ltT>& src) : x(src.x), y(src.y) {}
    virtual ~Vector2D() {}

    // Accessors
    inline T X() const { return x; }
    inline T Y() const { return y; }
    inline T X(const T& x) { this->x = x; }
    inline T Y(const T& y) { this->y = y; }

    // Vector arithmetic
    inline Vector2D<T> operator-() const
        { return Vector2D<T>(-x, -y); }

    inline Vector2D<T> operator+() const
        { return Vector2D<T>(+x, +y); }

    inline Vector2D<T> operator+(const Vector2D<T>& v) const
        { return Vector2D<T>(x+v.x, y+v.y); }

    inline Vector2D<T> operator-(const Vector2D<T>& v) const
        { return Vector2D<T>(x-v.x, y-v.y); }

    inline Vector2D<T> operator*(const T& s) const
        { return Vector2D<T>(x*s, y*s); }

    // Dot product
    inline T operator*(const Vector2D<T>& v) const
        { return x*v.x + y*v.y; }

    // l-2 norm
    inline T norm() const { return sqrt(x*x + y*y); }

    // inner angle (radians)
    static T angle(const Vector2D<T>& v1, const Vector2D<T>& v2)
    {
        return acos( (v1 * v2) / (v1.norm() * v2.norm()) );
    }
};

int main()
{
    Vector2D<double> p1(215, 294);
    Vector2D<double> p2(174, 228);
    Vector2D<double> p3(303, 294);

    double rad = Vector2D<double>::angle(p2-p1, p3-p1);
    double deg = rad * 180.0 / M_PI;

    std::cout << "rad = " << rad << "\tdeg = " << deg << std::endl;

    p1 = Vector2D<double>(153, 457);
    p2 = Vector2D<double>(19, 457);
    p3 = Vector2D<double>(15, 470);

    rad = Vector2D<double>::angle(p2-p1, p3-p1);
    deg = rad * 180.0 / M_PI;

    std::cout << "rad = " << rad << "\tdeg = " << deg << std::endl;

    return 0;
}

The code above yields:

rad = 2.12667   deg = 121.849
rad = 0.0939257 deg = 5.38155
andand
  • 15,638
  • 9
  • 48
  • 76
  • Hi andand, thanks for the response but I have the same problem, when I'm calculating an angle from a line that goes left to right (horizontal) with another line that goes right to left (from quad1 to quad3) and in a paper this lines have an acute angle (less that 90) the algorithm shows me an angle of 150 or more degrees and really I have an angle of 30 degrees. For other cases works fine. Thanks a lot for your time! Oscar. – osanchezmon Jun 01 '10 at 09:39
  • 1
    @ocell: all you need to add is `float final_theta = theta > 90degrees ? 180degrees - theta : theta` to get the result you're looking for. – Skizz Jun 01 '10 at 10:00
  • @ocell: What are the actual values of (x1, y1), (x2, y2) and (x3, y3) that are demonstrating the behavior you're describe? – andand Jun 01 '10 at 13:10
  • @Skizz: What youpropose will produce erroneous results when the inner angle actually is obtuse. – andand Jun 01 '10 at 14:52
  • @andand E.g.: Line from ( 174 , 228 ) to ( 215 , 294 ) Line from ( 215 , 294 ) to ( 303 , 294 ) Angle: 120degrees Line from ( 19 , 457 ) to ( 153 , 457 ) Line from ( 153 , 457 ) to ( 15 , 470 ) Angle: 5degrees – osanchezmon Jun 01 '10 at 15:20
  • 1
    @ocell: Okay, I updated the code to reflect the points you gave, and the results are approximately what you indicate they should be. Are you certain you're using the point p1 = (x1, y1) as your vertex in your implementation? – andand Jun 01 '10 at 15:37
  • @andand: Sorry the points are get from the log of View class (a class that has all the visual components: Line, Mark, etc.). Actually when I calculate the angle I'm using P2 (in the log) as P1 for the angle. – osanchezmon Jun 02 '10 at 08:45
  • 1
    @All people involved in the answer: Thanks for your time and for help me to understand better the measurement of angles in a plane; I was so obfuscated in one direction and finally I see different alternatives that can help in development of my dissertation. Once again thanks to everyb@dy and specially to andand that finally put a great code. See you soon! Oscar. – osanchezmon Jun 02 '10 at 08:48
  • If you always want the smaller angle between the two lines, use the absolute value of the dot product when passing it into acos. – LaC May 15 '11 at 20:38
5
if (result > 180)
{
     result = 360 - result;
}

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

  • Thanks thyrgle, but I don't want an angle bigger that 180º. I want the same if I like to know one angle of a triangle knowing only the three points. – osanchezmon May 31 '10 at 23:17
  • I don't think I understand you... The outer angle will always be greater than 180 or equal to it (and in that case the outer angle = the inner angle) so you can't get an angle bigger than 180 using this method considering 180 is half 360... –  May 31 '10 at 23:23
  • 3
    In other words, you can't get an angle greater than 180 with this. –  May 31 '10 at 23:35
  • 1
    Understandably, acute and obtuse angles of an intersection are 180 deg complents 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 or 90 deg, as we all already know. Because the max absolute result of atan is 90 deg or PI/2, the max diff between two atan results is 180 deg or PI. So there is nothing that avails us to deal with 360 deg threshold. So the two guys who voted you up is also in error. – Blessed Geek Jun 01 '10 at 08:12
  • 1
    Also, the arithmetic precision of the test [if(result>PI/4)] in radian is a problem that no known supercomputer could solve because PI/4 is not an exact/proper fraction. At the neighbourhood of 90 deg, the precision of the binary representation is questionable whether it is acute or obtuse. – Blessed Geek Jun 01 '10 at 08:18
  • I meant to say PI/2 not PI/4. – Blessed Geek Jun 01 '10 at 08:48
4

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;

msmq
  • 1,137
  • 15
  • 27
  • Consider the vectors `v1=(-0.5,-0.866025,0)` `v2=(-0.5,0.866025)`. The inner angle between both is 60 degrees. This formula gives 240. The question is clearly about the *inner angle*, not the clockwise angle. – Jorge Leitao Jul 24 '18 at 06:13
2

The whole point is much easier than the given answers:

When you use atan(slope) you lose (literally) one bit of information, that is there are exactly two angles (theta) and (theta+PI) in the range (0..2*PI), which give the same value for the function tan().

Just use atan2(deltax, deltay) and you get the right angle. For instance

atan2(1,1) == PI/4
atan2(-1,-1) == 5*PI/4

Then subtract, take absolute value, and if greater than PI subtract from 2*PI.

rewritten
  • 14,591
  • 2
  • 39
  • 48
1

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;
}
Jorg B Jorge
  • 1,041
  • 9
  • 17
  • What's the purpose of computing m1 and m2? The answer the OP is looking for is in angle and you use neither m1 nor m2 in computing it. – andand Jun 01 '10 at 00:39
  • Using pow for power of two? ouch. It is supposed to be used for fractional powers. – SigTerm Jun 01 '10 at 01:17
  • 1
    m1 and m2 are just to illustrate the program, showing the individual angle of each line. I used pow just because it would be easier to the reader to understand the formula. I am not trying to improve performance right here. I'm just trying to help the guy about the comprehension of the math formula. – Jorg B Jorge Jun 01 '10 at 01:24
1

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...

Steve
  • 11
  • 1
0

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.

Donnie
  • 41,533
  • 8
  • 62
  • 82
  • Let's say smaller theta is 10 deg and larger theta is -10 deg (170 deg). 170 - 10 = 160. 160 is hardly an acute angle. Whereas, its complement, 20 deg would be the answer. – Blessed Geek Jun 01 '10 at 08:32
0

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
Blessed Geek
  • 19,240
  • 21
  • 96
  • 165
  • @h2g2java: The OP asked for the inner angle of two lines defined by 3 points (one point (x1, y1) in common to both lines). In some cases this inner angle will be acute, in others it will be obtuse. – andand Jun 01 '10 at 15:20