Korotkodul

ЗОШ проверка пересечения OK

Jan 10th, 2022 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #include <math.h>
  11. #define pii pair <int,int>
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17. for (auto x: v) cout<<x<<' ';
  18. cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22. for (auto x: v) cout<<x<<' ';
  23. cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28. for (auto x: v) cv(x);
  29. cout<<"\n";
  30. }
  31.  
  32. struct ipn{
  33. int x,y;
  34. };
  35.  
  36. struct rpn{
  37. db x,y;
  38. };
  39.  
  40. rpn rVec(rpn A, rpn B){
  41. rpn ab;
  42. ab.x = A.x - B.x;
  43. ab.y = A.y - B.y;
  44. return ab;
  45. }
  46.  
  47. rpn div(rpn A, rpn B, db p, db q){
  48. rpn res;
  49. res.x = (q * A.x + p * B.x) / (p + q);
  50. res.y = (q * A.y + p * B.y) / (p+q);
  51. return res;
  52. }
  53.  
  54. db dot(rpn a, rpn b){
  55. return a.x * b.x + a.y * b.y;
  56. }
  57.  
  58. db lgth(rpn a){
  59. return sqrt(a.x * a.x + a.y * a.y);
  60. }
  61.  
  62. db angl(rpn a, rpn b){
  63. db an = acos(dot(a, b) / ( lgth(a) * lgth(b)) );
  64. return an;
  65. }
  66.  
  67. db rCross(rpn a, rpn b){
  68. return a.y * b.x - a.x * b.y;
  69. }
  70.  
  71. int anglTp(rpn A, rpn B, rpn C){ //возвращает тип угла между AB и AC то есть угла BAC
  72. rpn ab = rVec(A, B);
  73. rpn ac = rVec(A, C);
  74. if (dot(ab, ac) > 0){
  75. return 1; //острый
  76. }
  77. if (dot(ab, ac) == 0){
  78. return 1;//прямой
  79. }
  80. return 2; // тупой
  81. }
  82.  
  83.  
  84.  
  85. db rArea(rpn A, rpn B, rpn C){
  86. rpn ab = rVec(A, B);
  87. rpn ac = rVec(A, C);
  88. return fabs( rCross(ab, ac) ); // 2.0
  89. }
  90.  
  91.  
  92. db hgt(rpn A, rpn B, rpn C){ //height
  93. db h;
  94. //cout<<"rArea(B, A, C) = "<<rArea(B, A, C) <<"\n";
  95. //cout<<"AB = "<<lgth(rVec(A, B))<<"\n";
  96. h = rArea(B, A, C) / lgth( rVec(A, B) );
  97. //cout<<"h = "<<h<<"\n";
  98. if ( anglTp(A, B, C) == 2){
  99. //cout<<"one\n";
  100. return lgth( rVec(C, A) );
  101. }
  102. if (anglTp (B, A, C) == 2){
  103. //cout<<"two\n";
  104. return lgth( rVec(C, B) );
  105. }
  106. return h;
  107. }
  108.  
  109. void mk(rpn &x){
  110. cin>>x.x>>x.y;
  111. }
  112.  
  113. db eps = 1e-7;
  114.  
  115. int sgn(db x){
  116. if (x > 0.0) return 1;
  117. else if (x == 0.0) return 0;
  118. return -1;
  119. }
  120.  
  121. bool eq(rpn A, rpn B){
  122. return A.x == B.x && A.y == B.y;
  123. }
  124.  
  125. bool inter(rpn A, rpn B, rpn C, rpn D){
  126. if (eq(A,C) || eq(A,D ) || eq(B, C) || eq(B,D)){
  127. return 1;
  128. }
  129. if (A.x > B.x) swap(A, B);
  130. if (C.x > D.x) swap(C, D);
  131. if (B.x < C.x) {
  132. //cout<<"one\n";
  133. return 0;
  134. }
  135. if (D.x < A.x) {
  136. //cout<<"two\n";
  137. return 0;
  138. }
  139. if (A.y > B.y) swap(A, B);
  140. if (C.y > D.y) swap(C, D);
  141. if (B.y < C.y) return 0;
  142. if (D.y < A.y ) return 0;
  143.  
  144. if ( sgn(rCross(rVec(A,B), rVec(C,D)) == 0.0) ){
  145. return 1;
  146. }
  147. if (sgn(rCross(rVec(A,B), rVec(A,C))) == sgn(rCross(rVec(A,B), rVec(A,D)))){
  148. /*cout<<"three\n";
  149. //сout<<"sgn(rCross(rVec(A,B), rVec(A,C))) = "<<sgn(rCross(rVec(A,B), rVec(A,C)))<<"\n";
  150. cout<<"sgn(rCross(rVec(A,B), rVec(A,C))) = "<<sgn(rCross(rVec(A,B), rVec(A,C)))<<"\n";
  151. cout<<"sgn(rCross(rVec(A,B), rVec(A,D))) = "<<sgn(rCross(rVec(A,B), rVec(A,D)))<<"\n";*/
  152. return 0;
  153. }
  154. if (sgn(rCross(rVec(C,D), rVec(C, A))) == sgn(rCross(rVec(C, D), rVec(C,B)))){
  155. /*cout<<"four\n";
  156. cout<<"sgn(rCross(rVec(C,D), rVec(C, A))) = "<<sgn(rCross(rVec(C,D), rVec(C, A)))<<"\n";
  157. cout<<"sgn(rCross(rVec(C, D), rVec(C,B))) = "<<sgn(rCross(rVec(C, D), rVec(C,B)))<<"\n";*/
  158.  
  159. return 0;
  160. }
  161. return 1;
  162. }
  163.  
  164.  
  165. int main()
  166. {
  167. ios::sync_with_stdio(0);
  168. cin.tie(0);
  169. cout.tie(0);
  170. cout.precision(20);
  171. rpn A,B,C,D;
  172. mk(A);
  173. mk(B);
  174. mk(C);
  175. mk(D);
  176. if (inter(A,B, C,D)){
  177. cout<<"YES\n";
  178. }
  179. else cout<<"NO\n";
  180. }
  181.  
  182. /*
  183. 0 0 10 10
  184. 0 4 10 4
  185.  
  186. 0 0 10 0
  187. 9 0 100 0
  188.  
  189. 1 1 1 6
  190. 1 4 3 5
  191.  
  192. 1 1 1 6
  193. 3 1 3 6
  194.  
  195. 3 3 12 12
  196. 6 6 9 9
  197.  
  198.  
  199. 3 3 5 6
  200. 3 5 7 5
  201.  
  202. 1 2 1 2
  203. 1 2 1 2
  204.  
  205. 3 3 5 6
  206. 5 6 3 3
  207.  
  208. 3 3 5 6
  209. 1 2 4 2
  210.  
  211. 5 4 8 4
  212. 3 3 5 6
  213.  
  214. */
  215.  
Add Comment
Please, Sign In to add comment