fork(2) download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. #define eps 0.000001
  6. #define neps (-1*eps)
  7. #define abs(x) ((x)<0 ? (-1*(x)) : (x))
  8. class Vector;
  9.  
  10. class Point {
  11. private:
  12. double _x;
  13. double _y;
  14. public:
  15. Point (double x = 0, double y = 0) : _x(x),
  16. _y(y) {}
  17. friend class Vector;
  18. };
  19.  
  20. class Vector {
  21. private:
  22. Point _a;
  23. Point _b;
  24. double matrix_det (const Point &A, const Point &B, const Point &C) const {
  25. return (A._x * B._y + B._x * C._y + C._x * A._y - C._x * B._y - A._x * C._y - B._x * A._y);
  26. }
  27.  
  28. public:
  29. Vector (double a_x = 0, double a_y = 0, double b_x = 0, double b_y = 0) : _a(Point(a_x, a_y)),
  30. _b(Point(b_x, b_y)) {}
  31. Vector (const Point &A, const Point &B) : _a(A),
  32. _b(B) {}
  33.  
  34. double length () const {
  35. return sqrt (pow (_a._x - _b._x, 2.0) + pow (_a._y - _b._y, 2.0));
  36. }
  37.  
  38. bool belong (const Point &A) const {
  39. if (abs(Vector(A, _a).length() + Vector(A, _b).length() - this->length()) <= eps)
  40. return true;
  41. return false;
  42. }
  43.  
  44. bool intersection (const Vector &V) const {
  45. double x = ((this->_b._x-this->_a._x)*(V._b._x*V._a._y-V._b._y*V._a._x)-(V._b._x-V._a._x)*(this->_b._x*this->_a._y-this->_b._y*this->_a._x))/((this->_b._y-this->_a._y)*(V._b._x-V._a._x)-(V._b._y-V._a._y)*(this->_b._x-this->_a._x));
  46. double y = ((V._b._y-V._a._y)*(this->_b._x*this->_a._y-this->_b._y*this->_a._x)-(this->_b._y-this->_a._y)*(V._b._x*V._a._y-V._b._y*V._a._x))/((V._b._y-V._a._y)*(this->_b._x-this->_a._x)-(this->_b._y-this->_a._y)*(V._b._x-V._a._x));
  47. if (this->belong(Point(x, y)) && V.belong(Point(x, y)))
  48. return true;
  49. else return false;
  50. }
  51. };
  52.  
  53. int main() {
  54. Vector T2 (0, 0, 100, 100);
  55. Vector V (1, 1, 5, 5);
  56. cout<<T2.intersection(V);
  57. return 0;
  58. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Standard output is empty