fork download
  1. #include<stdio.h>
  2. #include<limits.h>
  3. #include<math.h>
  4. struct point
  5. {
  6. float x1;
  7. float y1;
  8. };
  9.  
  10. float findratio(float x,float y, struct point *a)
  11. {
  12. if(x!=a[0].x1)
  13. {
  14. return (a[1].x1-x)/(x-a[0].x1);
  15. }
  16. else
  17. {
  18. return (a[1].y1-y)/(y-a[0].y1);
  19. }
  20. }
  21. float findratioSlopesame(struct point * a, struct point* b)
  22. {
  23. float m;
  24. m=findratio(a[0].x1,a[0].y1,b);
  25. if(m>0)
  26. return m;
  27. m=findratio(a[1].x1,a[1].y1,b);
  28. if(m>0)
  29. return m;
  30. m=findratio(b[0].x1,b[0].y1,a);
  31. if(m>0)
  32. return m;
  33. m=findratio(b[0].x1,b[0].y1,a);
  34. if(m>0)
  35. return m;
  36. return -1;
  37.  
  38. }
  39. float findslope(struct point *a)
  40. {
  41. if(a[0].x1==a[1].x1)
  42. {
  43. return INFINITY;
  44. }
  45. else
  46. {
  47. return (a[1].y1-a[0].y1)/(a[1].x1-a[0].x1);
  48. }
  49. }
  50. int check (struct point *a, struct point *b)
  51. {
  52. if((a[0].y1<=b[0].y1&& a[0].y1>=b[1].y1)||(a[1].y1<=b[0].y1&& a[1].y1>=b[1].y1) )
  53. return 1;
  54. else if((a[0].y1<=b[1].y1&& a[0].y1>=b[0].y1)||(a[1].y1<=b[1].y1&& a[1].y1>=b[0].y1) )
  55. return 1;
  56. return 0;
  57. }
  58. int intersection(struct point *a, struct point *b,struct point *p)
  59. {
  60. float m1=findslope(a);
  61. float m2=findslope(b);
  62.  
  63. if(m1==INFINITY && m2==INFINITY)
  64. {
  65.  
  66. if(a[0].x1==b[1].x1)
  67. {
  68. //check whether 1st line contains another line
  69.  
  70. if(check(a,b)||check(b,a))
  71. {
  72. return 1;
  73. }
  74. }
  75. else
  76. {
  77. p->x1=INFINITY;
  78. p->y1=INFINITY;
  79. return 0;
  80. }
  81.  
  82. }
  83.  
  84. else if (m1!=INFINITY && m2!=INFINITY && m1!=m2)
  85. {
  86.  
  87. float x=(b[0].y1-a[0].y1-(m2*b[0].x1)+(m1*a[0].x1))/(m1-m2);
  88. float y=a[0].y1+m1*(x-a[0].x1);
  89. p->x1=x;
  90. p->y1=y;
  91. if(findratio(x,y,a)>=0 && findratio(x,y,b)>=0)
  92. return 1;
  93. else
  94. return 0;
  95.  
  96. }
  97. else if (m1==m2)
  98. {
  99. //y=mx+c1 y=mx+c2 checking c1-c2
  100. float x=a[0].y1-m1*a[0].x1;
  101. float y=b[0].y1-m1*b[0].x1;
  102. if(x!=y)
  103. return 0;
  104. if((a[0].x1==b[0].x1 && a[0].y1==b[0].y1)||(a[1].x1==b[0].x1 && a[1].y1==b[0].y1)||(a[0].x1==b[1].x1 && a[0].y1==b[1].y1)||(a[1].x1==b[1].x1 && a[1].y1==b[1].y1))
  105. return 1;
  106. if(findratioSlopesame(a,b)>0||findratioSlopesame(b,a)>0)
  107. return 1;
  108. return 0;
  109. }
  110. else if(m1!=m2)
  111. {
  112. if(m1==INFINITY)
  113. {
  114.  
  115. float x=a[0].x1;
  116. float y=b[0].y1+m2*(x-b[0].x1);
  117. if((x==a[0].x1&& y==a[0].y1)||(x==a[1].x1&& y==a[1].y1)||(x==b[0].x1&& y==b[0].y1)||(x==b[1].x1&& y==b[1].y1))
  118. {
  119. return 1;
  120. }
  121. if(findratio(x,y,a)>0|| findratio(x,y,b)>0)
  122. return 1;
  123. else
  124. return 0;
  125. }
  126. else if (m2==INFINITY)
  127. {
  128. float x=b[0].x1;
  129. float y=a[0].y1+m1*(x-a[0].x1);
  130. if((x==a[0].x1&& y==a[0].y1)||(x==a[1].x1&& y==a[1].y1)||(x==b[0].x1&& y==b[0].y1)||(x==b[1].x1&& y==b[1].y1))
  131. {
  132. return 1;
  133. }
  134. if(findratio(x,y,a)>0|| findratio(x,y,b)>0)
  135. return 1;
  136. else
  137. return 0;
  138. }
  139.  
  140. }
  141.  
  142. }
  143.  
  144.  
  145. int main()
  146. {
  147. struct point l1[2];
  148. struct point l2[2];
  149. scanf("%f %f %f %f",&l1[0].x1,&l1[0].y1,&l1[1].x1,&l1[1].y1);
  150.  
  151. scanf("%f %f %f %f",&l2[0].x1,&l2[0].y1,&l2[1].x1,&l2[1].y1);
  152. //findintersection
  153. // tells the
  154. struct point p;
  155. int result=intersection(l1,l2,&p);
  156. if(result==0)
  157. {
  158. printf("\nNo");
  159. }
  160. else
  161. {
  162. printf("\nYes");
  163. }
  164. return 0;
  165. }
Success #stdin #stdout 0s 9432KB
stdin
1 2
4 6
-3 5
10 12
stdout
No