fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5.  
  6. struct point {
  7. double x, y;
  8. // constructor
  9. point() {
  10. // cout << "Constructor kosong" << endl;
  11. }
  12. point(double _x, double _y) {
  13. // cout << "Constructor dgn 2 double" << endl;
  14. x = _x;
  15. y = _y;
  16. }
  17. double euclidean(point other) {
  18. return sqrt((x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));
  19. }
  20. double manhattan(point other) {
  21. return abs(x-other.x) + abs(y-other.y);
  22. }
  23. };
  24.  
  25. struct line {
  26. // Ax + By = C
  27. double A, B, C;
  28.  
  29. line() {}
  30. line(double _A, double _B, double _C) {
  31. A = _A;
  32. B = _B;
  33. C = _C;
  34. }
  35. bool isVertical() {
  36. return B == 0;
  37. }
  38. double gradient() {
  39. if(isVertical())
  40. return 0;
  41. return -A/B;
  42. }
  43. double constant() {
  44. if(isVertical())
  45. return 0;
  46. return C/B;
  47. }
  48. double distanceFromPoint(point other) {
  49. return fabs((A*other.x+B*other.y-C)/sqrt(A*A+B*B));
  50. }
  51. };
  52.  
  53. struct segment {
  54. point p1, p2;
  55. line extension() {
  56. // (y-y1)/(y2-y1) = (x-x1)/(x2-x1)
  57. // (y-y1)*(x2-x1) = (x-x1)*(y2-y1)
  58. // y*(x2-x1) - x*(y2-y1) = (y1*(x2-x1)) - (x1*(y2-y1))
  59. // (y1-y2)*x + (x2-x1)*y = y1*x2 - x1*y2
  60. // A *x + B *y = C
  61. double A, B, C;
  62. A = p1.y - p2.y;
  63. B = p2.x - p1.x;
  64. C = p1.y*p2.x - p1.x*p2.y;
  65. return line(A, B, C);
  66. }
  67. bool isCrossing(segment other) {
  68.  
  69. }
  70. };
  71.  
  72. int main() {
  73. double x1, y1, x2, y2;
  74. segment garis;
  75. cin >> x1 >> y1 >> x2 >> y2
  76. >> garis.p1.x >> garis.p1.y
  77. >> garis.p2.x >> garis.p2.y;
  78. line garis2 = garis.extension();
  79. // cek berpotongan dengan (x1,y1) ke (x1,y2)
  80. // cek perpotongan ruas garis (atau extension) dgn garis x = x1
  81. // Ax1 + By = C --> y = (C-A*x1)/B
  82. double y = (garis2.C - garis2.A * x1)/garis2.B;
  83. if((garis.p1.y < y && y < garis.p2.y) || ((garis.p1.y > y && y > garis.p2.y)))
  84. cout << "diantara" << endl;
  85. cout << y << endl;
  86. return 0;
  87. }
Success #stdin #stdout 0.01s 5300KB
stdin
0 0 6 5 4 3 8 7
stdout
-1