Calculation of angle between joined line segments

68 Views Asked by At

I'm struggling with this. What is the best way to calculate the angles identified in the image, see link. Is there a general solution for all cases.

Example problem

Thanks once again

2

There are 2 best solutions below

3
On

As I understand your question, you are looking for a practical method.

Do you know that there exist a function ((which exist in Matlab but has an equivalent in many scientific packages) overcoming the difficulty of the "$\pi$ barrier" : it is $atan2$ (with two parameters)

Indeed, working with the formula $\cos \theta = \dfrac{xx'+yy'}{\sqrt{x^2+y^2} \sqrt{x'^2+y'^2}}$ is insufficient for dealing, as you need it, with angles in the $[0, 2 \pi)$ range. In fact, knowing $\cos \theta$, you cannot decide between $\theta$ and $2 \pi - \theta$. A remedy for the elimination of this ambiguity would be to compute as well $\sin \theta$.

But, in fact, $atan2(Y,X)$ ($Y$ first, $X$ second) does all the work !

For example $atan2(-1,-1)$ will give $5\pi/4$, $atan2(-\sqrt{3}/2,1/2)$ will give $5\pi/3$, etc.


Edit 1: Your problem will be easier to work on if you replace exterior angles by interior angles. See figure below. Doing that, the "current polygon", having the sum of its interior angles equal to $(k-1)\pi$ (think to this polygon as decomposed into $n-1$ triangles), considered as "closed" by $V_k=\sum_{i=1}^k V_i$, or more exactly by taking $-V_k$. In this way, you can follow the angles' modification by reference to this "invariant" $(k-1)\pi$.

Remark: the fact that the polygon is convex or not is unimportant (one deals with signed quantities, either signed angles, signed areas, etc.)

enter image description here


Edit 2: Here is a Matlab program that "solves" your problem, more or less in the spirit of Edit1 ; it uses complex numbers. I wish you can work with complex numbers in your environment ; otherwise, take real and imaginary parts when necessary...

Two keypoints : %(1) for the minus sign in order to reverse the next coming vector and %(2) for dealing with the good side (left hand side, when you imagine you follow the broken line).

clear all;close all;hold on;
X=[0,2,5,6.8,10,8,8.8];
Y=[0,2,2,-2,5,-3,-4.2];
plot(X,Y);
Z=X+i*Y;
D=diff(Z);
for k=1:length(D)-1
    an=angle(-D(k+1)/D(k))*180/pi;%(1)
    if an<0
        a(k)=abs(an);
        else a(k)=360-an;%(2)
    end
    text(X(k+1),Y(k+1),[num2str(k),' ',num2str(a(k))]);hold on;
end;
a
3
On

Hint:

If you have three points $A=(x_A,y_A)$,$B=(x_B,y_B)$ and $C=(x_C,y_C)$ find the vectors: $\vec a=(x_A-x_B,y_A-y_B)^T=(a_x,a_y)^T$ and $\vec b=(x_C-x_B,y_C-y_B)^T=(b_x,b_y)^T$ ( this is a translation of the origin at $B$).

Find the angles $\alpha$ and $\beta$ such that: $$ \cos \alpha=\frac{a_x}{|\vec a|} \quad \land \quad \sin \alpha=\frac{a_y}{|\vec a|} $$ $$ \cos \beta=\frac{b_x}{|\vec b|} \quad \land \quad \sin \beta=\frac{b_y}{|\vec b|} $$ these angles are uniquely determined and $0\le \alpha <360°$,$0\le \beta <360°$ and, if I well interpret your picture we have always $\alpha \le \beta$.

Now, using the knowledge of the signs os the components of $\vec a$ and $\vec b$, you can separate the cases when the searched angle is $\theta= \alpha-\beta$ or $\theta'=360°-\theta$.

(Eventually you can solve analogously also the case $\alpha \ge \beta$).