Advertisement
leviathan0117

C++ geometry

Nov 6th, 2021
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. const long double EPS = 0.00000001;
  6.  
  7. class Vector {
  8. public:
  9.     long double x_, y_;
  10.  
  11.     Vector() {
  12.         x_ = 0;
  13.         y_ = 0;
  14.     }
  15.  
  16.     Vector(long double x, long double y) {
  17.         x_ = x;
  18.         y_ = y;
  19.     }
  20.  
  21.     Vector(std::istream &in) {
  22.         in >> x_ >> y_;
  23.     }
  24.     // Functions:
  25.  
  26.     long double Length() const {
  27.         return std::sqrt(x_ * x_ + y_ * y_);
  28.     }
  29.  
  30.     Vector GetPerpedicular() const {
  31.         return {y_, -x_};
  32.     }
  33.  
  34.     // OPERATORS:
  35.  
  36.     void operator+=(const Vector &other) {
  37.         x_ += other.x_;
  38.         y_ += other.y_;
  39.     }
  40.  
  41.     void operator+=(Vector &&other) {
  42.         x_ += other.x_;
  43.         y_ += other.y_;
  44.     }
  45.  
  46.     void operator-=(const Vector &other) {
  47.         x_ -= other.x_;
  48.         y_ -= other.y_;
  49.     }
  50.  
  51.     void operator-=(Vector &&other) {
  52.         x_ -= other.x_;
  53.         y_ -= other.y_;
  54.     }
  55.  
  56.     friend Vector operator+(const Vector &a, const Vector &b) {
  57.         return {a.x_ + b.x_, a.y_ + b.y_};
  58.     }
  59.  
  60.     friend Vector operator+(const Vector &a, Vector &&b) {
  61.         return {a.x_ + b.x_, a.y_ + b.y_};
  62.     }
  63.  
  64.     friend Vector operator+(Vector &&a, Vector &&b) {
  65.         return {a.x_ + b.x_, a.y_ + b.y_};
  66.     }
  67.  
  68.     friend Vector operator-(const Vector &a, const Vector &b) {
  69.         return {a.x_ - b.x_, a.y_ - b.y_};
  70.     }
  71.  
  72.     friend Vector operator-(const Vector &a, Vector &&b) {
  73.         return {a.x_ - b.x_, a.y_ - b.y_};
  74.     }
  75.  
  76.     friend Vector operator-(Vector &&a, Vector &&b) {
  77.         return {a.x_ - b.x_, a.y_ - b.y_};
  78.     }
  79.  
  80.     friend std::istream &operator>>(std::istream &in, Vector &v) {
  81.         in >> v.x_ >> v.y_;
  82.         return in;
  83.     }
  84.  
  85.     friend std::istream &operator>>(std::istream &in, Vector &&v) {
  86.         in >> v.x_ >> v.y_;
  87.         return in;
  88.     }
  89.  
  90.     friend std::ostream &operator<<(std::ostream &out, const Vector &v) {
  91.         out << v.x_ << " " << v.y_;
  92.         return out;
  93.     }
  94.  
  95.     friend std::ostream &operator<<(std::ostream &out, Vector &&v) {
  96.         out << v.x_ << " " << v.y_;
  97.         return out;
  98.     }
  99.  
  100.     friend bool operator==(const Vector &a, const Vector &b) {
  101.         return (std::fabs(b.x_ - a.x_) < EPS && std::fabs(b.y_ - a.y_) < EPS);
  102.     }
  103.  
  104.     friend bool operator==(const Vector &a, Vector &&b) {
  105.         return (std::fabs(b.x_ - a.x_) < EPS && std::fabs(b.y_ - a.y_) < EPS);
  106.     }
  107.  
  108.     friend bool operator==(Vector &&a, Vector &&b) {
  109.         return (std::fabs(b.x_ - a.x_) < EPS && std::fabs(b.y_ - a.y_) < EPS);
  110.     }
  111. };
  112.  
  113. class Line {
  114. public:
  115.     long double A_, B_, C_;
  116.  
  117.     Line() {
  118.         A_ = 1;
  119.         B_ = -1;
  120.         C_ = 0;
  121.     }
  122.  
  123.     Line(long double A, long double B, long double C) {
  124.         A_ = A;
  125.         B_ = B;
  126.         C_ = C;
  127.     }
  128.  
  129.     Line(std::istream &in) {
  130.         in >> A_ >> B_ >> C_;
  131.     }
  132.  
  133.     Line(const Vector &a, const Vector &b) {
  134.         Vector direction = b - a;
  135.         Vector norm = direction.GetPerpedicular();
  136.         A_ = norm.x_;
  137.         B_ = norm.y_;
  138.         C_ = -(A_ * a.x_ + B_ * a.y_);
  139.     }
  140.  
  141.     Vector GetNormal() const {
  142.         return Vector(A_, B_);
  143.     }
  144.  
  145.     friend std::istream &operator>>(std::istream &in, Line &line) {
  146.         in >> line.A_ >> line.B_ >> line.C_;
  147.         return in;
  148.     }
  149.  
  150.     friend std::istream &operator>>(std::istream &in, Line &&line) {
  151.         in >> line.A_ >> line.B_ >> line.C_;
  152.         return in;
  153.     }
  154.  
  155.     friend std::ostream &operator<<(std::ostream &out, const Line &line) {
  156.         out << line.A_ << " " << line.B_ << " " << line.C_;
  157.         return out;
  158.     }
  159.  
  160.     friend std::ostream &operator<<(std::ostream &out, Line &&line) {
  161.         out << line.A_ << " " << line.B_ << " " << line.C_;
  162.         return out;
  163.     }
  164.  
  165.     friend bool operator==(const Line &a, const Line &b) {
  166.         return (std::fabs(a.A_ * b.B_ - b.A_ * a.B_) < EPS && std::fabs(a.B_ * b.C_ - b.B_ * a.C_) < EPS);
  167.     }
  168.  
  169.     friend bool operator==(const Line &a, Line &&b) {
  170.         return (std::fabs(a.A_ * b.B_ - b.A_ * a.B_) < EPS && std::fabs(a.B_ * b.C_ - b.B_ * a.C_) < EPS);
  171.     }
  172.  
  173.     friend bool operator==(Line &&a, Line &&b) {
  174.         return (std::fabs(a.A_ * b.B_ - b.A_ * a.B_) < EPS && std::fabs(a.B_ * b.C_ - b.B_ * a.C_) < EPS);
  175.     }
  176. };
  177.  
  178. class Beam {
  179. public:
  180.     Vector a_, b_;
  181.  
  182.     Beam() {
  183.         a_ = {0, 0};
  184.         b_ = {1, 1};
  185.     }
  186.  
  187.     Beam(const Vector &a, const Vector &b) {
  188.         a_ = a;
  189.         b_ = b;
  190.     }
  191.  
  192.     Beam(std::istream &in) {
  193.         std::cin >> a_ >> b_;
  194.     }
  195.  
  196.     friend std::istream &operator>>(std::istream &in, Beam &v) {
  197.         in >> v.a_ >> v.b_;
  198.         return in;
  199.     }
  200.  
  201.     friend std::istream &operator>>(std::istream &in, Beam &&v) {
  202.         in >> v.a_ >> v.b_;
  203.         return in;
  204.     }
  205. };
  206.  
  207. class Segment {
  208. public:
  209.     Vector a_, b_;
  210.  
  211.     Segment() {
  212.         a_ = {0, 0};
  213.         b_ = {1, 1};
  214.     }
  215.  
  216.     Segment(const Vector &a, const Vector &b) {
  217.         a_ = a;
  218.         b_ = b;
  219.     }
  220.  
  221.     Segment(std::istream &in) {
  222.         std::cin >> a_ >> b_;
  223.     }
  224.  
  225.     friend std::istream &operator>>(std::istream &in, Segment &v) {
  226.         in >> v.a_ >> v.b_;
  227.         return in;
  228.     }
  229.  
  230.     friend std::istream &operator>>(std::istream &in, Segment &&v) {
  231.         in >> v.a_ >> v.b_;
  232.         return in;
  233.     }
  234. };
  235.  
  236. long double ScalarMultiplication(const Vector &a, const Vector &b) {
  237.     return a.x_ * b.x_ + a.y_ * b.y_;
  238. }
  239.  
  240. long double ScalarMultiplication(const Vector &a, Vector &&b) {
  241.     return a.x_ * b.x_ + a.y_ * b.y_;
  242. }
  243.  
  244. long double ScalarMultiplication(Vector &&a, Vector &&b) {
  245.     return a.x_ * b.x_ + a.y_ * b.y_;
  246. }
  247.  
  248. long double VectorMultiplication(const Vector &a, const Vector &b) {
  249.     return a.x_ * b.y_ - a.y_ * b.x_;
  250. }
  251.  
  252. long double VectorMultiplication(const Vector &a, Vector &&b) {
  253.     return a.x_ * b.y_ - a.y_ * b.x_;
  254. }
  255.  
  256. long double VectorMultiplication(Vector &&a, const Vector &b) {
  257.     return a.x_ * b.y_ - a.y_ * b.x_;
  258. }
  259.  
  260. long double VectorMultiplication(Vector &&a, Vector &&b) {
  261.     return a.x_ * b.y_ - a.y_ * b.x_;
  262. }
  263.  
  264. long double AngleCos(const Vector &a, const Vector &b) {
  265.     return ScalarMultiplication(a, b) / a.Length() / b.Length();
  266. }
  267.  
  268. long double AngleCos(const Vector &a, Vector &&b) {
  269.     return ScalarMultiplication(a, b) / a.Length() / b.Length();
  270. }
  271.  
  272. long double AngleCos(Vector &&a, Vector &&b) {
  273.     return ScalarMultiplication(a, b) / a.Length() / b.Length();
  274. }
  275.  
  276. long double Angle(const Vector &a, const Vector &b) {
  277.     return std::acos(AngleCos(a, b));
  278. }
  279.  
  280. long double Angle(const Vector &a, Vector &&b) {
  281.     return std::acos(AngleCos(a, b));
  282. }
  283.  
  284. long double Angle(Vector &&a, Vector &&b) {
  285.     return std::acos(AngleCos(a, b));
  286. }
  287.  
  288. long double DegToRad(const long double x) {
  289.     return x * M_PI / 180.0;
  290. }
  291.  
  292. long double RadToDeg(const long double x) {
  293.     return x * 180.0 / M_PI;
  294. }
  295.  
  296. bool LiesOn(const Segment &segment, const Vector &v) {
  297.     if (segment.a_ == v || segment.b_ == v) {
  298.         return true;
  299.     } else if (segment.a_ == segment.b_) {
  300.         return false;
  301.     }
  302.  
  303.     Vector alpha1 = segment.b_ - segment.a_;
  304.     Vector beta1 = v - segment.a_;
  305.     Vector alpha2 = segment.a_ - segment.b_;
  306.     Vector beta2 = v - segment.b_;
  307.     if (std::fabs(AngleCos(alpha1, beta1) - 1.0) < EPS && std::fabs(AngleCos(alpha2, beta2) - 1.0) < EPS) {
  308.         return true;
  309.     } else {
  310.         return false;
  311.     }
  312. }
  313.  
  314. bool LiesOn(const Segment &segment, Vector &&v) {
  315.     if (segment.a_ == v || segment.b_ == v) {
  316.         return true;
  317.     } else if (segment.a_ == segment.b_) {
  318.         return false;
  319.     }
  320.  
  321.     Vector alpha1 = segment.b_ - segment.a_;
  322.     Vector beta1 = v - segment.a_;
  323.     Vector alpha2 = segment.a_ - segment.b_;
  324.     Vector beta2 = v - segment.b_;
  325.     if (std::fabs(AngleCos(alpha1, beta1) - 1.0) < EPS && std::fabs(AngleCos(alpha2, beta2) - 1.0) < EPS) {
  326.         return true;
  327.     } else {
  328.         return false;
  329.     }
  330. }
  331.  
  332. bool LiesOn(Segment &&segment, const Vector &v) {
  333.     if (segment.a_ == v || segment.b_ == v) {
  334.         return true;
  335.     } else if (segment.a_ == segment.b_) {
  336.         return false;
  337.     }
  338.  
  339.     Vector alpha1 = segment.b_ - segment.a_;
  340.     Vector beta1 = v - segment.a_;
  341.     Vector alpha2 = segment.a_ - segment.b_;
  342.     Vector beta2 = v - segment.b_;
  343.     if (std::fabs(AngleCos(alpha1, beta1) - 1.0) < EPS && std::fabs(AngleCos(alpha2, beta2) - 1.0) < EPS) {
  344.         return true;
  345.     } else {
  346.         return false;
  347.     }
  348. }
  349.  
  350. bool LiesOn(Segment &&segment, Vector &&v) {
  351.     if (segment.a_ == v || segment.b_ == v) {
  352.         return true;
  353.     } else if (segment.a_ == segment.b_) {
  354.         return false;
  355.     }
  356.  
  357.     Vector alpha1 = segment.b_ - segment.a_;
  358.     Vector beta1 = v - segment.a_;
  359.     Vector alpha2 = segment.a_ - segment.b_;
  360.     Vector beta2 = v - segment.b_;
  361.     if (std::fabs(AngleCos(alpha1, beta1) - 1.0) < EPS && std::fabs(AngleCos(alpha2, beta2) - 1.0) < EPS) {
  362.         return true;
  363.     } else {
  364.         return false;
  365.     }
  366. }
  367.  
  368. long double Dist(const Vector &a, const Vector &b) {
  369.     return (b - a).Length();
  370. }
  371.  
  372. long double Dist(const Vector &a, Vector &b) {
  373.     return (b - a).Length();
  374. }
  375.  
  376. long double Dist(Vector &&a, Vector &&b) {
  377.     return (b - a).Length();
  378. }
  379.  
  380. long double Dist(const Line &line, const Vector &v) {
  381.     return std::fabs(line.A_ * v.x_ + line.B_ * v.y_ + line.C_) / line.GetNormal().Length();
  382. }
  383.  
  384. long double Dist(Line &&line, const Vector &v) {
  385.     return std::fabs(line.A_ * v.x_ + line.B_ * v.y_ + line.C_) / line.GetNormal().Length();
  386. }
  387.  
  388. long double Dist(const Line &line, Vector &&v) {
  389.     return std::fabs(line.A_ * v.x_ + line.B_ * v.y_ + line.C_) / line.GetNormal().Length();
  390. }
  391.  
  392. long double Dist(Line &&line, Vector &&v) {
  393.     return std::fabs(line.A_ * v.x_ + line.B_ * v.y_ + line.C_) / line.GetNormal().Length();
  394. }
  395.  
  396. long double Dist(const Beam &beam, const Vector &v) {
  397.     Vector alpha = beam.b_ - beam.a_;
  398.     Vector beta = v - beam.a_;
  399.     if (AngleCos(alpha, beta) >= 0) {
  400.         return Dist(Line(beam.a_, beam.b_), v);
  401.     } else {
  402.         return Dist(beam.a_, v);
  403.     }
  404. }
  405.  
  406. long double Dist(const Beam &beam, Vector &&v) {
  407.     Vector alpha = beam.b_ - beam.a_;
  408.     Vector beta = v - beam.a_;
  409.     if (AngleCos(alpha, beta) >= 0) {
  410.         return Dist(Line(beam.a_, beam.b_), v);
  411.     } else {
  412.         return Dist(beam.a_, v);
  413.     }
  414. }
  415.  
  416. long double Dist(Beam &&beam, const Vector &v) {
  417.     Vector alpha = beam.b_ - beam.a_;
  418.     Vector beta = v - beam.a_;
  419.     if (AngleCos(alpha, beta) >= 0) {
  420.         return Dist(Line(beam.a_, beam.b_), v);
  421.     } else {
  422.         return Dist(beam.a_, v);
  423.     }
  424. }
  425.  
  426. long double Dist(Beam &&beam, Vector &&v) {
  427.     Vector alpha = beam.b_ - beam.a_;
  428.     Vector beta = v - beam.a_;
  429.     if (AngleCos(alpha, beta) >= 0) {
  430.         return Dist(Line(beam.a_, beam.b_), v);
  431.     } else {
  432.         return Dist(beam.a_, v);
  433.     }
  434. }
  435.  
  436. long double Dist(const Segment &segment, const Vector &v) {
  437.     Vector alpha1 = segment.b_ - segment.a_;
  438.     Vector beta1 = v - segment.a_;
  439.     Vector alpha2 = segment.a_ - segment.b_;
  440.     Vector beta2 = v - segment.b_;
  441.     if (AngleCos(alpha1, beta1) >= 0 && AngleCos(alpha2, beta2) >= 0) {
  442.         return Dist(Line(segment.a_, segment.b_), v);
  443.     } else {
  444.         return std::min(Dist(segment.a_, v), Dist(segment.b_, v));
  445.     }
  446. }
  447.  
  448. long double Dist(const Segment &segment, Vector &&v) {
  449.     Vector alpha1 = segment.b_ - segment.a_;
  450.     Vector beta1 = v - segment.a_;
  451.     Vector alpha2 = segment.a_ - segment.b_;
  452.     Vector beta2 = v - segment.b_;
  453.     if (AngleCos(alpha1, beta1) >= 0 && AngleCos(alpha2, beta2) >= 0) {
  454.         return Dist(Line(segment.a_, segment.b_), v);
  455.     } else {
  456.         return std::min(Dist(segment.a_, v), Dist(segment.b_, v));
  457.     }
  458. }
  459.  
  460. long double Dist(Segment &&segment, const Vector &v) {
  461.     Vector alpha1 = segment.b_ - segment.a_;
  462.     Vector beta1 = v - segment.a_;
  463.     Vector alpha2 = segment.a_ - segment.b_;
  464.     Vector beta2 = v - segment.b_;
  465.     if (AngleCos(alpha1, beta1) >= 0 && AngleCos(alpha2, beta2) >= 0) {
  466.         return Dist(Line(segment.a_, segment.b_), v);
  467.     } else {
  468.         return std::min(Dist(segment.a_, v), Dist(segment.b_, v));
  469.     }
  470. }
  471.  
  472. long double Dist(Segment &&segment, Vector &&v) {
  473.     Vector alpha1 = segment.b_ - segment.a_;
  474.     Vector beta1 = v - segment.a_;
  475.     Vector alpha2 = segment.a_ - segment.b_;
  476.     Vector beta2 = v - segment.b_;
  477.     if (AngleCos(alpha1, beta1) >= 0 && AngleCos(alpha2, beta2) >= 0) {
  478.         return Dist(Line(segment.a_, segment.b_), v);
  479.     } else {
  480.         return std::min(Dist(segment.a_, v), Dist(segment.b_, v));
  481.     }
  482. }
  483.  
  484. bool OnSameSideEq(const Line &line, const Vector &a, const Vector &b) {
  485.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  486.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  487.  
  488.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  489.         return true;
  490.     } else {
  491.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  492.             return true;
  493.         } else {
  494.             return false;
  495.         }
  496.     }
  497. }
  498.  
  499. bool OnSameSideEq(const Line &line, const Vector &a, Vector &&b) {
  500.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  501.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  502.  
  503.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  504.         return true;
  505.     } else {
  506.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  507.             return true;
  508.         } else {
  509.             return false;
  510.         }
  511.     }
  512. }
  513.  
  514. bool OnSameSideEq(const Line &line, Vector &&a, Vector &&b) {
  515.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  516.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  517.  
  518.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  519.         return true;
  520.     } else {
  521.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  522.             return true;
  523.         } else {
  524.             return false;
  525.         }
  526.     }
  527. }
  528.  
  529. bool OnSameSideEq(Line &&line, const Vector &a, const Vector &b) {
  530.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  531.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  532.  
  533.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  534.         return true;
  535.     } else {
  536.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  537.             return true;
  538.         } else {
  539.             return false;
  540.         }
  541.     }
  542. }
  543.  
  544. bool OnSameSideEq(Line &&line, const Vector &a, Vector &&b) {
  545.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  546.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  547.  
  548.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  549.         return true;
  550.     } else {
  551.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  552.             return true;
  553.         } else {
  554.             return false;
  555.         }
  556.     }
  557. }
  558.  
  559. bool OnSameSideEq(Line &&line, Vector &&a, Vector &&b) {
  560.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  561.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  562.  
  563.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  564.         return true;
  565.     } else {
  566.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  567.             return true;
  568.         } else {
  569.             return false;
  570.         }
  571.     }
  572. }
  573.  
  574. bool OnSameSide(const Line &line, const Vector &a, const Vector &b) {
  575.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  576.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  577.  
  578.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  579.         return false;
  580.     } else {
  581.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  582.             return true;
  583.         } else {
  584.             return false;
  585.         }
  586.     }
  587. }
  588.  
  589. bool OnSameSide(const Line &line, const Vector &a, Vector &&b) {
  590.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  591.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  592.  
  593.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  594.         return false;
  595.     } else {
  596.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  597.             return true;
  598.         } else {
  599.             return false;
  600.         }
  601.     }
  602. }
  603.  
  604. bool OnSameSide(const Line &line, Vector &&a, Vector &&b) {
  605.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  606.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  607.  
  608.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  609.         return false;
  610.     } else {
  611.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  612.             return true;
  613.         } else {
  614.             return false;
  615.         }
  616.     }
  617. }
  618.  
  619. bool OnSameSide(Line &&line, const Vector &a, const Vector &b) {
  620.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  621.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  622.  
  623.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  624.         return false;
  625.     } else {
  626.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  627.             return true;
  628.         } else {
  629.             return false;
  630.         }
  631.     }
  632. }
  633.  
  634. bool OnSameSide(Line &&line, const Vector &a, Vector &&b) {
  635.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  636.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  637.  
  638.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  639.         return false;
  640.     } else {
  641.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  642.             return true;
  643.         } else {
  644.             return false;
  645.         }
  646.     }
  647. }
  648.  
  649. bool OnSameSide(Line &&line, Vector &&a, Vector &&b) {
  650.     long double alpha1 = line.A_ * a.x_ + line.B_ * a.y_ + line.C_;
  651.     long double alpha2 = line.A_ * b.x_ + line.B_ * b.y_ + line.C_;
  652.  
  653.     if (std::fabs(alpha1) < EPS || std::fabs(alpha2) < EPS) {
  654.         return false;
  655.     } else {
  656.         if ((alpha1 < 0 && alpha2 < 0) || (alpha1 > 0 && alpha2 > 0)) {
  657.             return true;
  658.         } else {
  659.             return false;
  660.         }
  661.     }
  662. }
  663.  
  664. bool Intersect(const Segment &s1, const Segment &s2) {
  665.     if (s1.a_ == s2.a_ || s1.a_ == s2.b_ || s1.b_ == s2.a_ || s1.b_ == s2.b_) {
  666.         return true;
  667.     } else if (s1.a_ == s1.b_) {
  668.         return LiesOn(s2, s1.a_);
  669.     } else if (s2.a_ == s2.b_) {
  670.         return LiesOn(s1, s2.a_);
  671.     }
  672.     if (Line(s1.a_, s1.b_) == Line(s2.a_, s2.b_)) { // проверяем, что хотя бы одна точка s2 лежит на отрезке s1
  673.         if (LiesOn(s1, s2.a_) || LiesOn(s1, s2.b_)) {
  674.             return true;
  675.         } else {
  676.             return false;
  677.         }
  678.     }
  679.     if (OnSameSide(Line(s1.a_, s1.b_), s2.a_, s2.b_) || OnSameSide(Line(s2.a_, s2.b_), s1.a_, s1.b_)) {
  680.         return false;
  681.     } else {
  682.         return true;
  683.     }
  684. }
  685.  
  686. bool Intersect(const Segment &s1, Segment &&s2) {
  687.     if (s1.a_ == s2.a_ || s1.a_ == s2.b_ || s1.b_ == s2.a_ || s1.b_ == s2.b_) {
  688.         return true;
  689.     } else if (s1.a_ == s1.b_) {
  690.         return LiesOn(s2, s1.a_);
  691.     } else if (s2.a_ == s2.b_) {
  692.         return LiesOn(s1, s2.a_);
  693.     }
  694.     if (Line(s1.a_, s1.b_) == Line(s2.a_, s2.b_)) { // проверяем, что хотя бы одна точка s2 лежит на отрезке s1
  695.         if (LiesOn(s1, s2.a_) || LiesOn(s1, s2.b_)) {
  696.             return true;
  697.         } else {
  698.             return false;
  699.         }
  700.     }
  701.     if (OnSameSide(Line(s1.a_, s1.b_), s2.a_, s2.b_) || OnSameSide(Line(s2.a_, s2.b_), s1.a_, s1.b_)) {
  702.         return false;
  703.     } else {
  704.         return true;
  705.     }
  706. }
  707.  
  708. bool Intersect(Segment &&s1, Segment &&s2) {
  709.     if (s1.a_ == s2.a_ || s1.a_ == s2.b_ || s1.b_ == s2.a_ || s1.b_ == s2.b_) {
  710.         return true;
  711.     } else if (s1.a_ == s1.b_) {
  712.         return LiesOn(s2, s1.a_);
  713.     } else if (s2.a_ == s2.b_) {
  714.         return LiesOn(s1, s2.a_);
  715.     }
  716.     if (Line(s1.a_, s1.b_) == Line(s2.a_, s2.b_)) { // проверяем, что хотя бы одна точка s2 лежит на отрезке s1
  717.         if (LiesOn(s1, s2.a_) || LiesOn(s1, s2.b_)) {
  718.             return true;
  719.         } else {
  720.             return false;
  721.         }
  722.     }
  723.     if (OnSameSide(Line(s1.a_, s1.b_), s2.a_, s2.b_) || OnSameSide(Line(s2.a_, s2.b_), s1.a_, s1.b_)) {
  724.         return false;
  725.     } else {
  726.         return true;
  727.     }
  728. }
  729.  
  730. bool IsBetween(const Vector &a, const Vector &b, const Vector &m) {
  731.     long double bm = VectorMultiplication(b, m);
  732.     long double ma = VectorMultiplication(m, a);
  733.     if ((bm >= -EPS && ma >= -EPS) || (bm <= EPS && ma <= EPS)) { // !!!!!!!!!!!!
  734.         return true;
  735.     } else {
  736.         return false;
  737.     }
  738. }
  739.  
  740. bool IsBetween(const Vector &a, const Vector &b, Vector &&m) {
  741.     long double bm = VectorMultiplication(b, m);
  742.     long double ma = VectorMultiplication(m, a);
  743.     if ((bm >= -EPS && ma >= -EPS) || (bm <= EPS && ma <= EPS)) { // !!!!!!!!!!!!
  744.         return true;
  745.     } else {
  746.         return false;
  747.     }
  748. }
  749.  
  750. bool IsBetween(const Vector &a, Vector &&b, const Vector &m) {
  751.     long double bm = VectorMultiplication(b, m);
  752.     long double ma = VectorMultiplication(m, a);
  753.     if ((bm >= -EPS && ma >= -EPS) || (bm <= EPS && ma <= EPS)) { // !!!!!!!!!!!!
  754.         return true;
  755.     } else {
  756.         return false;
  757.     }
  758. }
  759.  
  760. bool IsBetween(const Vector &a, Vector &&b, Vector &&m) {
  761.     long double bm = VectorMultiplication(b, m);
  762.     long double ma = VectorMultiplication(m, a);
  763.     if ((bm >= -EPS && ma >= -EPS) || (bm <= EPS && ma <= EPS)) { // !!!!!!!!!!!!
  764.         return true;
  765.     } else {
  766.         return false;
  767.     }
  768. }
  769.  
  770. bool IsBetween(Vector &&a, const Vector &b, const Vector &m) {
  771.     long double bm = VectorMultiplication(b, m);
  772.     long double ma = VectorMultiplication(m, a);
  773.     if ((bm >= -EPS && ma >= -EPS) || (bm <= EPS && ma <= EPS)) { // !!!!!!!!!!!!
  774.         return true;
  775.     } else {
  776.         return false;
  777.     }
  778. }
  779.  
  780. bool IsBetween(Vector &&a, const Vector &b, Vector &&m) {
  781.     long double bm = VectorMultiplication(b, m);
  782.     long double ma = VectorMultiplication(m, a);
  783.     if ((bm >= -EPS && ma >= -EPS) || (bm <= EPS && ma <= EPS)) { // !!!!!!!!!!!!
  784.         return true;
  785.     } else {
  786.         return false;
  787.     }
  788. }
  789.  
  790. bool IsBetween(Vector &&a, Vector &&b, const Vector &m) {
  791.     long double bm = VectorMultiplication(b, m);
  792.     long double ma = VectorMultiplication(m, a);
  793.     if ((bm >= -EPS && ma >= -EPS) || (bm <= EPS && ma <= EPS)) { // !!!!!!!!!!!!
  794.         return true;
  795.     } else {
  796.         return false;
  797.     }
  798. }
  799.  
  800. bool IsBetween(Vector &&a, Vector &&b, Vector &&m) {
  801.     long double bm = VectorMultiplication(b, m);
  802.     long double ma = VectorMultiplication(m, a);
  803.     if ((bm >= -EPS && ma >= -EPS) || (bm <= EPS && ma <= EPS)) { // !!!!!!!!!!!!
  804.         return true;
  805.     } else {
  806.         return false;
  807.     }
  808. }
  809.  
  810. int main() {
  811.     std::fixed(std::cout);
  812.     std::cout << std::setprecision(6) << "\n";
  813.  
  814.     Vector x, y, z;
  815.     Vector v;
  816.     std::cin >> x >> y >> z >> v;
  817.  
  818.     if (IsBetween(y - x, z - x, v - x) &&
  819.         IsBetween(x - y, z - y, v - y) &&
  820.         IsBetween(y - z, x - z, v - z)) {
  821.         std::cout << "In\n";
  822.     } else {
  823.         std::cout << "Out\n";
  824.     }
  825.  
  826.     return 0;
  827. }
  828.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement