fork download
  1. // https://u...content-available-to-author-only...e.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=314&mosmsg=Submission+received+with+ID+17243632
  2.  
  3. #include <stdio.h>
  4.  
  5. typedef struct{
  6. double x;
  7. double y;
  8. }Point;
  9.  
  10. typedef struct{
  11. Point from;
  12. Point to;
  13. }Line;
  14.  
  15. double ABS(double a){
  16. if (a < 0){
  17. return -a;
  18. }
  19.  
  20. return a;
  21. }
  22.  
  23. double getSlopeOfALine(Line line){
  24. return (line.from.y - line.to.y) / (line.from.x - line.to.x);
  25. }
  26.  
  27. double getAreaOfATriangle(Point A, Point B, Point C){
  28. return ABS((A.x * (B.y - C.y) + B.x * (C.y - A.y) + C.x * (A.y - B.y))) / 2;
  29. }
  30.  
  31. int are2LinesSame(Line line1, Line line2){
  32. return getAreaOfATriangle(line1.from, line2.from, line2.to) == 0 && getAreaOfATriangle(line1.to, line2.from, line2.to) == 0;
  33. }
  34.  
  35. int are2LinesParallel(Line line1, Line line2){
  36. return getSlopeOfALine(line1) == getSlopeOfALine(line2);
  37. }
  38.  
  39. void scanAPoint(Point* point){
  40. scanf("%lf %lf", &point->x, &point->y);
  41. }
  42.  
  43. void scanALine(Line* line){
  44. scanAPoint(&line->from);
  45. scanAPoint(&line->to);
  46. }
  47.  
  48. void scan2Lines(Line* line1, Line* line2){
  49. scanALine(line1);
  50. scanALine(line2);
  51. }
  52.  
  53. Point getIntersectionPoint(Line line1, Line line2){
  54. Point intersectionPoint;
  55.  
  56. double denominator = 0, nominatorX = 0, nominatorY = 0;
  57.  
  58. denominator = (line1.from.x - line1.to.x) * (line2.from.y - line2.to.y) - (line2.from.x - line2.to.x) * (line1.from.y - line1.to.y);
  59.  
  60. if (denominator == 0){
  61. //DoesIntersect = 0;
  62. }
  63. else{
  64. //DoesIntersect = 1;
  65.  
  66. nominatorX = (line1.from.x * line1.to.y - line1.to.x * line1.from.y) * (line2.from.x - line2.to.x) - /* (x1y2 - x2y1) X (x3 - x4)*/
  67. (line2.from.x * line2.to.y - line2.to.x * line2.from.y) * (line1.from.x - line1.to.x); /* (x3y4 - x4y3) - (x1 - x2)*/
  68. nominatorY = (line1.from.x * line1.to.y - line1.to.x * line1.from.y) * (line2.from.y - line2.to.y) - /* (x1y2 - x2y1) X (y3 - y4)*/
  69. (line2.from.x * line2.to.y - line2.to.x * line2.from.y) * (line1.from.y - line1.to.y); /* (x3y4 - x4y3) - (y1 - y2)*/
  70.  
  71. intersectionPoint.x = nominatorX / denominator;
  72. intersectionPoint.y = nominatorY / denominator;
  73. }
  74.  
  75. return intersectionPoint;
  76. }
  77.  
  78. void findAndPrintResult(Line line1, Line line2){
  79. if (are2LinesSame(line1, line2)){
  80. printf("LINE");
  81. }
  82. else if (are2LinesParallel(line1, line2)){
  83. printf("NONE");
  84. }
  85. else{
  86. Point intersectionPoint = getIntersectionPoint(line1, line2);
  87. printf("POINT %.2f %.2f", intersectionPoint.x, intersectionPoint.y);
  88. }
  89. printf("\n");
  90. }
  91.  
  92. int main(){
  93. long int T = 0;
  94. Line line1, line2;
  95. #ifdef __FREOPEN__
  96. freopen("378 - Intersecting Lines.in", "r", stdin);
  97. freopen("378 - Intersecting Lines.out", "w", stdout);
  98. #endif
  99. scanf("%ld", &T);
  100.  
  101. printf("INTERSECTING LINES OUTPUT\n");
  102.  
  103. while (T--){
  104. scan2Lines(&line1, &line2);
  105. findAndPrintResult(line1, line2);
  106. }
  107.  
  108. printf("END OF OUTPUT\n");
  109.  
  110. #ifdef __FREOPEN__
  111. fclose(stdout);
  112. #endif
  113.  
  114. return 0;
  115. }
Success #stdin #stdout 0s 2160KB
stdin
10 
0 0 4 4 0 4 4 0 
5 0 7 6 1 0 2 3 
5 0 7 6 3 -6 4 -3 
2 0 2 27 1 5 18 5 
0 3 4 0 1 2 2 5 
0 0 1 1 0 0 1 1 
4 0 6 0 1 2 6 9 
1 0 5 0 8 5 4 5 
1 0 5 0 5 0 1 0 
0 1 0 5 0 5 0 1
stdout
INTERSECTING LINES OUTPUT
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20
LINE
POINT -0.43 0.00
NONE
LINE
LINE
END OF OUTPUT