Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using std::cout;
- using std::cin;
- using std::endl;
- using std::abs;
- class fraction {
- private:
- long long int num, den; // num/den
- static long long int gcd(long long int a, long long int b) {
- return (b != 0) ? gcd(b, a % b) : a;
- }
- public:
- fraction() : num(0), den(1) {}
- fraction(long long int n, long long int d) {
- num = n / gcd(abs(n), abs(d));
- den = d / gcd(abs(n), abs(d));
- }
- const long long int getNum() const {
- return num;
- }
- const long long int getDen() const {
- return den;
- }
- const fraction operator+ (fraction other) const {
- long long int newNum, newDen;
- newNum = num * other.getDen() + other.getNum() * den;
- newDen = den * other.getDen();
- return fraction(newNum, newDen);
- }
- const fraction operator- (fraction other) const {
- long long int newNum, newDen;
- newNum = num * other.getDen() - other.getNum() * den;
- newDen = den * other.getDen();
- return fraction(newNum, newDen);
- }
- const fraction operator* (fraction other) const {
- return fraction(num * other.getNum(), den * other.getDen());
- }
- const fraction operator/ (fraction other) const {
- return fraction(num * other.getDen(), den * other.getNum());
- }
- const bool operator== (fraction other) const {
- return (num == other.getNum()) && (den == other.getDen());
- }
- const bool operator> (fraction other) const {
- return num * other.getDen() > other.getNum() * den;
- }
- const bool operator< (fraction other) const {
- return num * other.getDen() < other.getNum() * den;
- }
- const long long int sign() const {
- if (num * den > 0) {
- return 1;
- } else {
- return -1;
- }
- }
- };
- struct point {
- fraction x, y;
- bool err;
- point() {
- err = false;
- }
- point(long long int xx, long long int yy) {
- x = fraction(xx, 1);
- y = fraction(yy, 1);
- err = false;
- }
- };
- class line {
- private:
- fraction k, b; // y = kx + b
- bool vertical;
- fraction x; // x = a
- public:
- line() : k(fraction()), b(fraction()), vertical(false), x(fraction()) {}
- line(long long int x1, long long int y, long long int q, long long int w) {
- vertical = (x1 == q);
- x = fraction(x1, 1);
- k = fraction(y - w, x1 - q);
- b = fraction(y, 1) - k * fraction(x1, 1);
- }
- const fraction getK() const {
- return k;
- }
- const fraction getB() const {
- return b;
- }
- const bool isVertical() const {
- return vertical;
- }
- const fraction getX() const {
- return x;
- }
- bool operator== (const line &other) const {
- if (other.isVertical() && vertical) {
- return (x == other.getX());
- }
- return (k == other.getK()) && (b == other.getB());
- }
- point intersection(const line &other) {
- point result;
- if (vertical) {
- if ( other.isVertical() && !(other.getX() == x) ) {
- result.err = true;
- return result;
- }
- result.x = x;
- result.y = other.getK() * x + other.getB();
- return result;
- }
- if (other.isVertical()) {
- if ( vertical && !(other.getX() == x) ) {
- result.err = true;
- return result;
- }
- result.x = other.getX();
- result.y = k * other.getX() + b;
- return result;
- }
- if (k == other.getK()) {
- result.err = true;
- return result;
- }
- result.x = (other.getB() - b) / (k - other.getK());
- result.y = k * result.x + b;
- return result;
- }
- bool belong(point p) {
- if (vertical) {
- return (p.x == x);
- }
- return (p.y == k * p.x + b);
- }
- };
- bool onOneSide(const point ¢er, const point &one, const point &two) {
- bool r = true;
- r = r && (one.x - center.x).sign() == (two.x - center.x).sign();
- r = r && (one.y - center.y).sign() == (two.y - center.y).sign();
- return r;
- }
- int main() {
- long long int x1, y1, x2, y2;
- cin >> x1 >> y1 >> x2 >> y2;
- line first(x1, y1, x2, y2); // первая прямая
- cin >> x1 >> y1 >> x2 >> y2;
- line second(x1, y1, x2, y2);
- // луч,однако по факту это таже прямая,только обрубленная в какой-то точке
- if ((first == second) || (first.belong(point(x1, y1)))) {
- cout << "YES" << endl;
- } else {
- point beginBeam(x1, y1), secondBeam(x2, y2), inter;
- inter = first.intersection(second);
- // beginBeam - это как раз та "какая-то" точка, которая "рубит" прямую
- // secondBeam - точка указывающая направление луча
- // inter - точка пересечения прямых
- if (inter.err) {
- cout << "NO" << endl;
- return 0;
- }
- if ( onOneSide(beginBeam, secondBeam, inter) ) {
- cout << "YES" << endl;
- } else {
- cout << "NO" << endl;
- }
- // если относительно точки разделения второй прямой точка пересечения
- // находится в части координатной плоскости, где и"направляющая" точка
- // это значит, что луч таки пересек прямую
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement