Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using std::cin;
  4. using std::cout;
  5.  
  6. double vector_multiplie(double vector_0_x, double  vector_0_y,
  7.                          double  vector_1_x, double  vector_1_y) {
  8.     return vector_0_x * vector_1_x + vector_0_y * vector_1_y;
  9. }
  10.  
  11.  
  12. bool is_vector_apart(double vector_0_x, double  vector_0_y,
  13.                       double  vector_1_x, double  vector_1_y) {
  14.     if (vector_0_x * vector_1_x < 0 && vector_0_y * vector_1_y < 0) {
  15.         return 1;
  16.     } else {
  17.         return 0;
  18.     }
  19. }
  20.  
  21. bool point_on_the_segment(double left_x, double left_y,
  22.                           double middle_x, double middle_y, double right_x, double right_y) {
  23.     if ((middle_x - left_x) * (right_x - middle_x) > 0
  24.         && (middle_y - left_y) * (right_y - middle_y) > 0) {
  25.             return 1;
  26.     } else {
  27.         return 0;
  28.     }
  29. }
  30.  
  31. struct Ray {
  32.     double axis_0_x, axis_0_y, axis_1_x, axis_1_y;
  33.     double zero_vector_x, zero_vector_y;
  34.     double equation_coefficient_A, equation_coefficient_B, equation_coefficient_C;
  35.     bool is_cross(Ray &other) {
  36.         double cross_x, cross_y;
  37.         if (this -> equation_coefficient_A * other.equation_coefficient_B ==
  38.         this -> equation_coefficient_B * other.equation_coefficient_A) {
  39.             if (this -> equation_coefficient_A * other.axis_0_x
  40.                 + this -> equation_coefficient_B * other.axis_0_y
  41.                 - this -> equation_coefficient_C == 0
  42.                 && !(is_vector_apart(this -> zero_vector_x, this -> zero_vector_y,
  43.                 other.zero_vector_x, other.zero_vector_y)
  44.                 && point_on_the_segment(other.axis_0_x, other.axis_0_y,
  45.                 this -> axis_0_x, this -> axis_0_y,
  46.                 this -> axis_1_x, this -> axis_1_y))) {
  47.                     return 1;
  48.             } else {
  49.                 return 0;
  50.             }
  51.         } else {
  52.             cross_x = -(this -> equation_coefficient_C * other.equation_coefficient_B -
  53.             this -> equation_coefficient_B * other.equation_coefficient_C) /
  54.             (this -> equation_coefficient_A * other.equation_coefficient_B -
  55.             this -> equation_coefficient_B * other.equation_coefficient_A);
  56.             cross_y = -(this -> equation_coefficient_A * other.equation_coefficient_C -
  57.             this -> equation_coefficient_C * other.equation_coefficient_A) /
  58.             (this -> equation_coefficient_A * other.equation_coefficient_B -
  59.             this -> equation_coefficient_B * other.equation_coefficient_A);
  60.  
  61.             if (vector_multiplie(this -> zero_vector_x, this -> zero_vector_y,
  62.                 cross_x - this -> axis_0_x, cross_y - this -> axis_0_y) >= 0
  63.                 && vector_multiplie(other.zero_vector_x, other.zero_vector_y,
  64.                 cross_x - other.axis_0_x, cross_y - other.axis_0_y) >= 0) {
  65.                     return 1;
  66.             } else {
  67.                 return 0;
  68.             }
  69.         }
  70.     }
  71. };
  72.  
  73. Ray NewRay() {
  74.     Ray new_ray;
  75.     cin >> new_ray.axis_0_x >> new_ray.axis_0_y >> new_ray.axis_1_x >> new_ray.axis_1_y;
  76.     new_ray.equation_coefficient_A = new_ray.axis_0_y - new_ray.axis_1_y;
  77.     new_ray.equation_coefficient_B = new_ray.axis_1_x - new_ray.axis_0_x;
  78.     new_ray.equation_coefficient_C = new_ray.axis_0_x * new_ray.axis_1_y -
  79.         new_ray.axis_1_x * new_ray.axis_0_y;
  80.     new_ray.zero_vector_x = new_ray.axis_1_x - new_ray.axis_0_x;
  81.     new_ray.zero_vector_y = new_ray.axis_1_y - new_ray.axis_0_y;
  82.     return new_ray;
  83. }
  84.  
  85. int main() {
  86.     Ray ray_first = NewRay(), ray_second = NewRay();
  87.     if (ray_first.is_cross(ray_second) == 0) {
  88.         cout << "NO";
  89.     } else {
  90.         cout << "YES";
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement