2
$\begingroup$

I'm going to through a list of coordinates and computing the angle between every two adjacent lines. In other words, I'm computing an angle for every 3 consecutive points. Every three consecutive points should form a triangle (where all 3 points are pairwise distinct). In computing their angle, I use the law of cosines and therefore compute

$\gamma = \arccos(\frac{a^2 + b^2 - c^2}{2ab})$

In the process of doing this, I end up with $\frac{a^2 + b^2 - c^2}{2ab} < -1$, which means the inverse cosine has a domain error.

What is flawed about what I am doing?

I do not believe all 3 points lie on the same line, but I will check this.

  • 0
    There's noting wrong with your formula, which leads me to believe there might be a mistake in how you're computing the three numbers $a$, $b$ and $c$.2012-10-17
  • 1
    If the points were collinear, then you'd have $c = a \pm b$ ("$\pm$" depending upon which point is between the other two), so that $(a^2+b^2-c^2)/(2ab) = \mp 1$; this gives a value of either $\pi$ or $0$ for $\gamma$, which is consistent with this degenerate situation and isn't a problem. If your computational logic is sound, then maybe you're just running into round-off errors, in which case you could just clamp the inputs to between $-1$ and $1$.2012-10-17
  • 2
    Given $a$, $b$, $c$ all $>0$ the relation $a^2+b^2-c^2<-2ab$ is equivalent with $c>a+b$, so your triangle would not satisfy the triangle inequality.2012-10-17
  • 0
    It would be nice if you displayed the numbers that generated that data.2018-04-02

0 Answers 0