MCPcopy Create free account
hub / github.com/SuprDewd/CompetitiveProgramming / line_segment_distance

Function line_segment_distance

code/geometry/lines.cpp:13–28  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11 double t = dot(c - a, b - a) / norm(b - a);
12 return a + t * (b - a); }
13double line_segment_distance(L(a,b), L(c,d)) {
14 double x = INFINITY;
15 if (abs(a - b) < EPS && abs(c - d) < EPS) x = abs(a - c);
16 else if (abs(a - b) < EPS)
17 x = abs(a - closest_point(c, d, a, true));
18 else if (abs(c - d) < EPS)
19 x = abs(c - closest_point(a, b, c, true));
20 else if ((ccw(a, b, c) < 0) != (ccw(a, b, d) < 0) &&
21 (ccw(c, d, a) < 0) != (ccw(c, d, b) < 0)) x = 0;
22 else {
23 x = min(x, abs(a - closest_point(c,d, a, true)));
24 x = min(x, abs(b - closest_point(c,d, b, true)));
25 x = min(x, abs(c - closest_point(a,b, c, true)));
26 x = min(x, abs(d - closest_point(a,b, d, true)));
27 }
28 return x; }
29bool intersect(L(a,b), L(p,q), point &res,
30 bool lseg=false, bool rseg=false) {
31 // NOTE: check parallel/collinear before

Callers

nothing calls this directly

Calls 2

closest_pointFunction · 0.85
ccwFunction · 0.85

Tested by

no test coverage detected