Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct point
- {
- int x, y;
- point(int x, int y) : x(x), y(y) {}
- point operator-(point& other)
- {
- return point(x - other.x, y - other.y);
- }
- };
- long long cp(point a, point b)
- {
- return (long long)a.x * b.y - (long long)a.y * b.x;
- }
- bool intersect(point a, point b, point c, point d)
- {
- // Create change vectors and their cross product
- point r = b - a, s = d - c;
- long long cp_rs = cp(r, s);
- if (cp_rs == 0) // Lines are parallel
- {
- if (cp(c - a, r) == 0) // Lines are colinear
- {
- if (r.x == 0 && s.x == 0) // Check if they're overlaping
- {
- if (a.y > b.y) swap(a, b);
- if (c.y > d.y) swap(c, d);
- return (b.y - c.y >= 0 && d.y - a.y >= 0);
- }
- else
- {
- if (a.x > b.x) swap(a, b);
- if (c.x > d.x) swap(c, d);
- return (b.x - c.x >= 0 && d.x - a.x >= 0);
- }
- }
- return false;
- }
- else // Lines aren't parallel
- {
- // Check where they intersect
- double t = (double)cp(c - a, s) / cp_rs;
- double u = (double)cp(c - a, r) / cp_rs;
- if (t <= 1 && t >= 0 && u <= 1 && u >= 0) return true; // Intersect without prolongation
- return false; // Intersect with prolongation
- }
- }
Add Comment
Please, Sign In to add comment