NOTE do two line segments intersect https stackoverflow com 565282 sta

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// NOTE: do two line segments intersect?
// https://stackoverflow.com/a/565282
static bool
segments_intersect(f32 ax, f32 ay, f32 bx, f32 by, f32 cx, f32 cy, f32 dx, f32 dy)
{
f32 ca_x = cx - ax;
f32 ca_y = cy - ay;
f32 ba_x = bx - ax;
f32 ba_y = by - ay;
f32 dc_x = dx - cx;
f32 dc_y = dy - cy;
f32 ca_x_ba = ca_x * ba_y - ca_y * ba_x;
f32 ca_x_dc = ca_x * dc_y - ca_y * dc_x;
f32 ba_x_dc = ba_x * dc_y - ba_y * dc_x;
float p = 1.0f / ba_x_dc;
float t = ca_x_dc * p;
float u = ca_x_ba * p;
return((t >= 0.0f) && (t <= 1.0f) && (u >= 0.0f) && (u <= 1.0f));
}