This documentation is automatically generated by online-judge-tools/verification-helper
#include "geometry/projection.hpp"#pragma once
#include "dot.hpp"
#include "line.hpp"
#include "segment.hpp"
namespace geometry {
// 射影(projection)
// 直線(線分)lに点pから引いた垂線の足を求める
inline Point projection(const Line &l, const Point &p) {
D t = dot(p - l.a, l.a - l.b) / std::norm(l.a - l.b);
return l.a + (l.a - l.b) * t;
}
inline Point projection(const Segment &l, const Point &p) {
D t = dot(p - l.a, l.a - l.b) / std::norm(l.a - l.b);
return l.a + (l.a - l.b) * t;
}
} // namespace geometry#line 2 "geometry/projection.hpp"
#line 2 "geometry/dot.hpp"
#line 2 "geometry/base.hpp"
#include <cmath>
#include <complex>
namespace geometry {
// Point : 複素数型を位置ベクトルとして扱う
// 実軸(real)をx軸、挙軸(imag)をy軸として見る
using D = long double;
using Point = std::complex<D>;
const D EPS = 1e-7;
const D PI = std::acos(D(-1));
inline bool equal(const D &a, const D &b) { return std::fabs(a - b) < EPS; }
} // namespace geometry
#line 4 "geometry/dot.hpp"
namespace geometry {
// 内積(dot product) : a・b = |a||b|cosΘ
inline D dot(const Point &a, const Point &b) {
return (a.real() * b.real() + a.imag() * b.imag());
}
} // namespace geometry
#line 2 "geometry/line.hpp"
#line 4 "geometry/line.hpp"
namespace geometry {
// Line : 直線を表す構造体
// b - a で直線・線分を表せる
struct Line {
Point a, b;
Line() = default;
Line(Point a, Point b) : a(a), b(b) {}
// Ax+By=C
Line(D A, D B, D C) {
if(equal(A, 0)) {
a = Point(0, C / B), b = Point(1, C / B);
} else if(equal(B, 0)) {
a = Point(C / A, 0), b = Point(C / A, 1);
} else if(equal(C, 0)) {
a = Point(0, C / B), b = Point(1, (C - A) / B);
} else {
a = Point(0, C / B), b = Point(C / A, 0);
}
}
};
} // namespace geometry
#line 2 "geometry/segment.hpp"
#line 4 "geometry/segment.hpp"
namespace geometry {
// Segment : 線分を表す構造体
// Lineと同じ
struct Segment : Line {
Segment() = default;
Segment(Point a, Point b) : Line(a, b) {}
D get_dist() { return std::abs(a - b); }
};
} // namespace geometry
#line 6 "geometry/projection.hpp"
namespace geometry {
// 射影(projection)
// 直線(線分)lに点pから引いた垂線の足を求める
inline Point projection(const Line &l, const Point &p) {
D t = dot(p - l.a, l.a - l.b) / std::norm(l.a - l.b);
return l.a + (l.a - l.b) * t;
}
inline Point projection(const Segment &l, const Point &p) {
D t = dot(p - l.a, l.a - l.b) / std::norm(l.a - l.b);
return l.a + (l.a - l.b) * t;
}
} // namespace geometry