fork(1) download
  1. #include <cmath>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct point{
  7. int x;
  8. int y;
  9. };
  10.  
  11. // line segment p-q intersect with line A-B.
  12. point lineIntersectSeg(point p, point q, point A, point B) {
  13. double a = B.y - A.y;
  14. double b = A.x - B.x;
  15. double c = B.x * A.y - A.x * B.y;
  16. double u = fabs(a * p.x + b * p.y + c);
  17. double v = fabs(a * q.x + b * q.y + c);
  18. return point{(p.x * v + q.x * u) / (u+v), (p.y * v + q.y * u) / (u+v)};
  19. }
  20.  
  21. int main() {
  22. point foo = lineIntersectSeg(point{0, 1}, point{0, 2}, point{1, 1}, point{1, 2});
  23. cout << foo.x << endl << foo.y << endl;
  24. return 0;
  25. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
0
1