Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <algorithm>
- int vmul(int x0, int y0, int x1, int y1, int x2, int y2) {
- x1 -= x0;
- y1 -= y0;
- x2 -= x0;
- y2 -= y0;
- int c = x1 * y2 - x2 * y1;
- return c < 0 ? -1 : c > 0 ? 1 : 0;
- }
- int main() {
- freopen("segments.in", "r", stdin);
- freopen("segments.out", "w", stdout);
- int x1, y1, x2, y2, x3, y3, x4, y4;
- scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
- scanf("%d%d%d%d", &x3, &y3, &x4, &y4);
- int v1 = vmul(x1, y1, x3, y3, x4, y4);
- int v2 = vmul(x2, y2, x3, y3, x4, y4);
- int u1 = vmul(x3, y3, x1, y1, x2, y2);
- int u2 = vmul(x4, y4, x1, y1, x2, y2);
- if (v1 == 0 && v2 == 0 && u1 == 0 && u2 == 0) {
- int lx = std::max(std::min(x1, x2), std::min(x3, x4));
- int rx = std::min(std::max(x1, x2), std::max(x3, x4));
- int ly = std::max(std::min(y1, y2), std::min(y3, y4));
- int ry = std::min(std::max(y1, y2), std::max(y3, y4));
- if (lx > rx || ly > ry)
- puts("Empty");
- else {
- if (lx < rx || ly < ry) {
- if (vmul(lx, ly, x1, y1, x2, y2) != 0)
- std::swap(ly, ry);
- printf("%d %d\n%d %d\n", lx, ly, rx, ry);
- } else {
- printf("%d %d\n", lx, ly);
- }
- }
- } else if (v1 != v2 && u1 != u2) {
- double a1 = y2 - y1;
- double b1 = x1 - x2;
- double c1 = -x1 * a1 - y1 * b1;
- double a2 = y4 - y3;
- double b2 = x3 - x4;
- double c2 = -x3 * a2 - y3 * b2;
- double z = a1 * b2 - b1 * a2;
- printf("%.17lf %.17lf\n", (b1 * c2 - c1 * b2) / z, (c1 * a2 - a1 * c2) / z);
- } else {
- puts("Empty");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment