Advertisement
Guest User

lab_1: class_otrezok

a guest
Feb 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. struct Point
  7. {
  8. public:
  9. double x, y;
  10. };
  11.  
  12. class otrezok {
  13. private:
  14. double aX,aY,bX,bY;
  15. public:
  16.  
  17. otrezok(double ax, double ay, double bx, double by)
  18. {
  19. this->aX = ax;
  20. this->aY = ay;
  21. this->bX = bx;
  22. this->bY = by;
  23. }
  24.  
  25. void ToConsole()
  26. {
  27. cout<<"Otrerzok ("<<aX<<","<<aY<<") - ("<<bX<<","<<bY<<")"<<endl;
  28. }
  29.  
  30. bool Equals (otrezok Drugoi_otrezok)
  31. {
  32. return this->Lenght()==Drugoi_otrezok.Lenght();
  33. }
  34.  
  35. int Lenght()
  36. {
  37. return sqrt( (bX - aX)*(bX - aX) + (bY - aY) * (bY - aY) );
  38. }
  39.  
  40. int CompareTo(otrezok Drugoi_otrezok)
  41. {
  42. if(this->Equals(Drugoi_otrezok))
  43. return 0;
  44. else
  45. return this->Lenght() > Drugoi_otrezok.Lenght();
  46. }
  47.  
  48. Point Intersect(otrezok O)
  49. {
  50. Point toReturn;
  51. toReturn.x = 0;
  52. toReturn.y = 0;
  53.  
  54. double t = 0;
  55.  
  56. t = - ( (O.aX - this->aX) * (this->aY - this->bY) + (O.aY - this->aY ) * (this->bX - this->aX) )/
  57. ( (O.bX - O.aX ) * (this->aY - this->bY) + ( O.bY - O.aY ) * (this->bX - this->aX) );
  58.  
  59. if(t >= 0 && t <= 1)
  60. {
  61. toReturn.x = O.aX + t * (O.bX - O.aX);
  62.  
  63. toReturn.y = O.aY + t * (O.bY - O.aY);
  64. return toReturn;
  65. }
  66. else
  67. {
  68. cout<<"NET TOCHKI PERESECHENIYA"<<endl;
  69. return toReturn;
  70. }
  71. }
  72.  
  73. double Distance (otrezok Drugoi_otrezok)
  74. {
  75. double first = Distance_between_2_points(this->aX, this->aY, Drugoi_otrezok.aX, Drugoi_otrezok.aY);
  76. double second = Distance_between_2_points(this->aX, this->aY, Drugoi_otrezok.bX, Drugoi_otrezok.bY);
  77.  
  78. double third = Distance_between_2_points(this->bX, this->bY, Drugoi_otrezok.aX, Drugoi_otrezok.aY);
  79. double fourth = Distance_between_2_points(this->bX, this->bY, Drugoi_otrezok.bX, Drugoi_otrezok.bY);
  80.  
  81. double tmp1, tmp2;
  82. /// вспомогательные переменные для того, чтобы сохранить читаемость кода (не делать всё во вложенных if)
  83.  
  84. /// Это - все пары расстояний между отрезками, минимальное из них - наш ответ
  85.  
  86. if(first < second)
  87. {
  88. tmp1 = first;
  89. }
  90. else
  91. {
  92. tmp1 = second;
  93. }
  94.  
  95. if(third < fourth)
  96. {
  97. tmp2 = third;
  98. }
  99. else
  100. {
  101. tmp2 = fourth;
  102. }
  103.  
  104. if(tmp1 < tmp2)
  105. {
  106. return tmp1;
  107. }
  108. else
  109. {
  110. return tmp2;
  111. }
  112. }
  113.  
  114. double Distance_between_2_points(double x1, double y1, double x2, double y2)
  115. {
  116. return sqrt( (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) );
  117. }
  118. };
  119.  
  120. int main()
  121. {
  122. otrezok a = otrezok(2,-1,2,1);
  123. otrezok b = otrezok(0,0,3,0);
  124. a.Intersect(b);
  125. //cout << "Hello world!" << endl;
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement