Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cmath>
- #include <iomanip>
- #include <iostream>
- using i64 = long long;
- using i128 = __int128;
- using ld = long double;
- const i128 INF = 1e9;
- namespace {
- i128 Abs(i128 x) {
- if (x < 0) {
- x *= -1;
- }
- return x;
- }
- }// namespace
- struct Point {
- i128 x, y;
- friend std::istream& operator>>(std::istream& is, Point& point) {
- i64 a, b;
- is >> a >> b;
- point.x = a;
- point.y = b;
- return is;
- }
- Point operator-(const Point& point) const {
- return {x - point.x, y - point.y};
- }
- Point operator+(const Point& point) const {
- return {x + point.x, y + point.y};
- }
- Point operator*(i128 k) const {
- return {x * k, y * k};
- }
- i128 operator*(const Point& point) const {
- return x * point.x + y * point.y;
- }
- i128 operator%(const Point& point) const {
- return x * point.y - y * point.x;
- }
- };
- ld Distance_Point_Point(Point a, Point b) {
- return sqrt((a - b) * (a - b));
- }
- ld Distance_Point_Segment(Point a, Point b1, Point b2) {
- if ((a - b1) * (b2 - b1) >= 0 && (a - b2) * (b1 - b2) >= 0) {
- return Abs((b1 - a) % (b1 - b2)) / Distance_Point_Point(b1, b2);
- }
- return std::min(Distance_Point_Point(a, b1), Distance_Point_Point(a, b2));
- }
- ld Distance_Point_Ray(Point a, Point b1, Point b2) {
- b2 = b2 + (b2 - b1) * INF;
- return Distance_Point_Segment(a, b1, b2);
- }
- ld Distance_Point_Line(Point a, Point b1, Point b2) {
- b2 = b2 + (b2 - b1) * INF;
- b1 = b2 + (b1 - b2) * 2;
- return Distance_Point_Segment(a, b1, b2);
- }
- bool Intersect_Segment_Segment(Point a, Point b, Point c, Point d) {
- if ((a - d) % (c - d) == 0 && (b - d) % (c - d) == 0) {
- i128 l1, r1, l2, r2;
- if (a.x == b.x && a.x == c.x && a.x == d.x) {
- l1 = a.y;
- r1 = b.y;
- l2 = c.y;
- r2 = d.y;
- } else {
- l1 = a.x;
- r1 = b.x;
- l2 = c.x;
- r2 = d.x;
- }
- i128 l3 = std::max(l1, l2);
- i128 r3 = std::min(r1, r2);
- return l3 <= r3;
- }
- i128 value1 = (a - d) % (c - d);
- i128 value2 = (b - d) % (c - d);
- i128 value3 = (c - b) % (a - b);
- i128 value4 = (d - b) % (a - b);
- if ((value1 >= 0 && value2 <= 0) || (value1 <= 0 && value2 >= 0)) {
- if ((value3 >= 0 && value4 <= 0) || (value3 <= 0 && value4 >= 0)) {
- return true;
- }
- return false;
- }
- return false;
- }
- ld Distance_Segment_Segment(Point a, Point b, Point c, Point d) {
- if (Intersect_Segment_Segment(a, b, c, d)) {
- return 0;
- }
- ld value1 = Distance_Point_Segment(a, c, d);
- ld value2 = Distance_Point_Segment(b, c, d);
- ld value3 = Distance_Point_Segment(c, a, b);
- ld value4 = Distance_Point_Segment(d, a, b);
- return std::min({value1, value2, value3, value4});
- }
- ld Distance_Segment_Ray(Point a, Point b, Point c, Point d) {
- d = d + (d - c) * INF;
- return Distance_Segment_Segment(a, b, c, d);
- }
- ld Distance_Segment_Line(Point a, Point b, Point c, Point d) {
- d = d + (d - c) * INF;
- c = d + (c - d) * 2;
- return Distance_Segment_Segment(a, b, c, d);
- }
- ld Distance_Ray_Ray(Point a, Point b, Point c, Point d) {
- d = d + (d - c) * INF;
- b = b + (b - a) * INF;
- return Distance_Segment_Segment(a, b, c, d);
- }
- ld Distance_Ray_Line(Point a, Point b, Point c, Point d) {
- b = b + (b - a) * INF;
- d = d + (d - c) * INF;
- c = d + (c - d) * 2;
- return Distance_Segment_Segment(a, b, c, d);
- }
- ld Distance_Line_Line(Point a, Point b, Point c, Point d) {
- d = d + (d - c) * INF;
- c = d + (c - d) * 2;
- b = b + (b - a) * INF;
- a = b + (a - b) * 2;
- return Distance_Segment_Segment(a, b, c, d);
- }
- // 5.65685
- int main() {
- std::cout << std::fixed << std::setprecision(15);
- Point a, b, c, d;
- std::cin >> a >> b >> c >> d;
- std::cout << Distance_Point_Point(a, c) << std::endl;
- std::cout << Distance_Point_Segment(a, c, d) << std::endl;
- std::cout << Distance_Point_Ray(a, c, d) << std::endl;
- std::cout << Distance_Point_Line(a, c, d) << std::endl;
- std::cout << Distance_Point_Segment(c, a, b) << std::endl;
- std::cout << Distance_Segment_Segment(a, b, c, d) << std::endl;
- std::cout << Distance_Segment_Ray(a, b, c, d) << std::endl;
- std::cout << Distance_Segment_Line(a, b, c, d) << std::endl;
- std::cout << Distance_Point_Ray(c, a, b) << std::endl;
- std::cout << Distance_Segment_Ray(c, d, a, b) << std::endl;
- std::cout << Distance_Ray_Ray(a, b, c, d) << std::endl;
- std::cout << Distance_Ray_Line(a, b, c, d) << std::endl;
- std::cout << Distance_Point_Line(c, a, b) << std::endl;
- std::cout << Distance_Segment_Line(c, d, a, b) << std::endl;
- std::cout << Distance_Ray_Line(c, d, a, b) << std::endl;
- std::cout << Distance_Line_Line(a, b, c, d) << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement