1

The problem of line segment intersection seems simple enough using orientation, but the explanation in this blog leading up to formula used says one thing, and the math used is doing another.

The solution involves determining if three points are listed in a counterclockwise order. So say you have three points A, B and C. If the slope of the line AB is less than the slope of the line AC then the three points are listed in a counterclockwise order.

http://bryceboe.com/2006/10/23/line-segment-intersection-algorithm/

If you look at the function, that is not using slopes to compare as was just stated, I am not sure what he is doing. Regardless, I plugged in some test values and it does indeed work, but it is not "slope" as is described in the blog.

Am I missing something simple here?

2 Answers2

1

if you have two points $A=(a_x, a_y)$ and $B=(b_x, b_y)$ then the slope $m_{AB}$ of the line from $A$ to $B$ is $$ \frac{\Delta y}{\Delta x} = \frac{b_y-a_y}{b_x - a_x}$$

Similarly for $m_{AC}$. So $AC$ is steeper than $AB$ if $$\frac{c_y-a_y}{c_x - a_x} =m_{AC}> m_{AB} = \frac{b_y-a_y}{b_x - a_x}$$ Now just multiply by the denominators.

Thomas
  • 22,361
  • Well wait one moment. What does it matter if I cross multiply or not? If I use the regular slope formula (using division), the presented algorithm in the link does not work. – Lanet Rino Aug 25 '17 at 18:12
0

To compare two ratios, it is more efficient to remove the denominators:

$$\frac ab<\frac cd\iff ad<bc.$$

This avoids divisions.