Advertisement
ivnikkk

Untitled

Jun 16th, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define debug(l) cerr<<#l<<' '<<l<<'\n';
  3. #include "bits/stdc++.h"
  4. using namespace std;
  5. #define all(a) a.begin(), a.end()
  6. typedef long long ll;
  7. typedef pair<ll, ll> pll;
  8. typedef long double ld;
  9.  
  10. #define Vec Point
  11. const ld EPS = 1e-7;
  12. const ld Pi = 3.14159265358979323846;
  13. int sign(ld x) {
  14.     if (x > EPS) return 1;
  15.     if (x < -EPS) return -1;
  16.     return 0;
  17. }
  18.  
  19. ld sq(ld x) {
  20.     return x * x;
  21. }
  22.  
  23. struct Point {
  24.     ld x, y;
  25.     Point() : x(0), y(0) {}
  26.     Point(ld _x, ld _y) : x(_x), y(_y) {}
  27.     Point operator-(const Point& other) const {
  28.         return Point(x - other.x, y - other.y);
  29.     }
  30.     Point operator+(const Point& other) const {
  31.         return Point(x + other.x, y + other.y);
  32.     }
  33.     Point operator*(const ld& other) const {
  34.         return Point(x * other, y * other);
  35.     }
  36.     // векторное произведение sin
  37.     ld operator^(const Point& other) const {
  38.         return x * other.y - y * other.x;
  39.     }
  40.     // скалярное произведение cos
  41.     ld operator*(const Point& other) const {
  42.         return x * other.x + y * other.y;
  43.     }
  44.     ld len2() const {
  45.         return sq(x) + sq(y);
  46.     }
  47.     ld len() const {
  48.         return sqrtl(len2());
  49.     }
  50.     Point norm() const {
  51.         ld d = len();
  52.         return Point(x / d, y / d);
  53.     }
  54.     bool operator<(const Point& other) const {
  55.         if (sign(x - other.x) != 0) {
  56.             return x < other.x;
  57.         }
  58.         else if (sign(y - other.y) != 0) {
  59.             return y < other.y;
  60.         }
  61.         return false;
  62.     }
  63.     bool operator==(const Point& other) const {
  64.         return sign(x - other.x) == 0 && sign(y - other.y) == 0;
  65.     }
  66.     Point ort() {
  67.         return Point(-y, x);
  68.     }
  69.     void deb() const {
  70.         std::cerr << "(" << x << ", " << y << ")" << std::endl;
  71.     }
  72. };
  73. ostream& operator << (ostream& out, const Point& val) {
  74.     return out << val.x << ' ' << val.y;
  75. }
  76. istream& operator >> (istream& in, Point& val) {
  77.     return in >> val.x >> val.y;
  78. }
  79. struct Line {
  80.     ld a, b, c;
  81.     Line() : a(0), b(0), c(0) {}
  82.     Line(const Point& x, const Point& y) : a(y.y - x.y), b(x.x - y.x), c(x.y* y.x - y.y * x.x) {
  83.         ////// нормирование прямой
  84.         //ld d = Point(a, b).len();
  85.         //a /= d, b /= d, c /= d;
  86.         //if (sign(a) == -1) {
  87.         //  a = -a;
  88.         //  b = -b;
  89.         //  c = -c;
  90.         //}
  91.         //else if (sign(a) == 0 && sign(b) == 0) {
  92.         //  a = 0;
  93.         //  b = -b;
  94.         //  c = -b;
  95.         //}
  96.     }/*
  97.     Line norm() {
  98.         ld d =
  99.     }*/
  100.     //ax+by+c=0
  101.     ////y=-c/b
  102.     //Line norm() {
  103.     // 
  104.     //}
  105.     Point get() {
  106.         if (sign(b) != 0) {
  107.             return Point(0, -c / b);
  108.         }
  109.         else
  110.             return Point(0, -c);
  111.     }
  112.  
  113.     bool operator==(const Line& other) const {
  114.         return sign(a - other.a) == 0 && sign(b - other.b) == 0 && sign(c - other.c) == 0;
  115.     }
  116. };
  117. ld dist_line(const Point& a, const Line& l) {
  118.     return abs(l.a * a.x + l.b * a.y + l.c) / sqrtl(sq(l.a) + sq(l.b));
  119. }
  120. // проверка принадлежности точки отрезку
  121. bool is_point_on_seg(const Point& a, const Point& b, const Point& x) {
  122.     // проверка в тупую
  123.     return sign((a - b).len() - (a - x).len() - (b - x).len()) == 0;
  124.     //if (a == b) {
  125.     //  return a == x;
  126.     //}
  127.     //else if (a == x || b == x) {
  128.     //  return true;
  129.     //}
  130.     //else {
  131.     //  return sign((x - a) ^ (b - a)) == 0 &&
  132.     //      sign((x - a) * (b - a)) == 1 &&
  133.     //      sign((x - b) * (a - b)) == 1;
  134.     //}
  135. }
  136. Point point_to_seg(Point& x, Point& c, Point& d) {
  137.     Line check(c, d);
  138.     Vec k(check.a, check.b);
  139.     Point suba = x + k;
  140.     Point subb = x - k;
  141.     //dis = dist_points(suba, a);
  142.     if (is_point_on_seg(c, d, suba)) {
  143.         return suba;
  144.     }
  145.     else {
  146.         return subb;
  147.     }
  148. }
  149. bool line_cross(const Line& a, const Line& b, Point& ans) {
  150.     ld d = (a.b * b.a - a.a * b.b);
  151.     if (sign(d) == 0)
  152.         return false;
  153.     ans.x = (b.b * a.c - b.c * a.b) / d;
  154.     ans.y = (b.c * a.a - a.c * b.a) / d;
  155.     return true;
  156. }
  157. signed main() {
  158. #ifdef _DEBUG
  159.     freopen("input.txt", "r", stdin);
  160.     freopen("output.txt", "w", stdout);
  161. #endif
  162.     ios_base::sync_with_stdio(false);
  163.     cin.tie(nullptr);
  164.     cout.tie(nullptr);
  165.     cout << fixed << setprecision(7);
  166.     Point a, b, c;
  167.     cin >> a >> b >> c;
  168.     Line l1(a, (point_to_seg(a, b, c)));
  169.     Line l2(c, (point_to_seg(c, a, b)));
  170.     Point ans;
  171.     line_cross(l1, l2, ans);
  172.     cout << ans;
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement