交差判定・交点・距離・射影
概要
タイトルのとおりです.分けづらかったのでまとめました.参考文献のコピーです.
実装
bool intersectLL(const L &l, const L &m) {
return std::abs(cross(l[1] - l[0], m[1] - m[0])) > eps || // non-parallel
std::abs(cross(l[1] - l[0], m[0] - l[0])) < eps; // same line
}
bool intersectLS(const L &l, const L &s) {
return cross(l[1] - l[0], s[0] - l[0]) * // s[0] is left of l
cross(l[1] - l[0], s[1] - l[0]) <
eps; // s[1] is right of l
}
bool intersectLP(const L &l, const P &p) { return std::abs(cross(l[1] - p, l[0] - p)) < eps; }
bool intersectSS(const L &s, const L &t) {
return ccw(s[0], s[1], t[0]) * ccw(s[0], s[1], t[1]) <= 0 &&
ccw(t[0], t[1], s[0]) * ccw(t[0], t[1], s[1]) <= 0;
}
bool intersectSP(const L &s, const P &p) {
return std::abs(s[0] - p) + std::abs(s[1] - p) - std::abs(s[1] - s[0]) <
eps; // triangle inequality
}
P projection(const L &l, const P &p) {
ld t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
return l[0] + t * (l[0] - l[1]);
}
P reflection(const L &l, const P &p) { return p + (ld)2 * (projection(l, p) - p); }
ld distanceLP(const L &l, const P &p) { return std::abs(p - projection(l, p)); }
ld distanceLL(const L &l, const L &m) { return intersectLL(l, m) ? 0 : distanceLP(l, m[0]); }
ld distanceLS(const L &l, const L &s) {
if (intersectLS(l, s)) return 0;
return std::min(distanceLP(l, s[0]), distanceLP(l, s[1]));
}
ld distanceSP(const L &s, const P &p) {
const P r = projection(s, p);
if (intersectSP(s, r)) return std::abs(r - p);
return std::min(std::abs(s[0] - p), std::abs(s[1] - p));
}
ld distanceSS(const L &s, const L &t) {
if (intersectSS(s, t)) return 0;
return std::min(std::min(distanceSP(s, t[0]), distanceSP(s, t[1])),
std::min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}
P crosspointLL(const L &l, const L &m) {
ld A = cross(l[1] - l[0], m[1] - m[0]);
ld B = cross(l[1] - l[0], l[1] - m[0]);
if (std::abs(A) < eps && std::abs(B) < eps) return m[0]; // same line
if (std::abs(A) < eps) assert(false); // !!!PRECONDITION NOT SATISFIED!!!
return m[0] + B / A * (m[1] - m[0]);
}