Back to list of problems
Intersection
191.c
#include <stdio.h> int rx1, ry1, rx2, ry2, sx1, sy1, sx2, sy2; float cortey(int x) { return (float)(sy1)+((float)(sy2-sy1)*(x-sx1))/(float)(sx2-sx1); } float cortex(int y) { return (float)(sx1)+((float)(sx2-sx1)*(y-sy1))/(float)(sy2-sy1); } int calc(void) { float a; if (((sx1>=rx1) && (sx1<=rx2) && (sy1>=ry1) && (sy1<=ry2)) || ((sx2>=rx1) && (sx2<=rx2) && (sy2>=ry1) && (sy2<=ry2))) { return 1; } if (((sx1>rx2) && (sx2>rx2)) || ((sx1<rx1) && (sx2<rx1)) || ((sy1>ry2) && (sy2>ry2)) || ((sy1<ry1) && (sy2<ry1))) { return 0; } a = cortex(ry1); if ((a>=rx1) && (a<=rx2)) { return 1; } a = cortex(ry2); if ((a>=rx1) && (a<=rx2)) { return 1; } a = cortey(rx1); if ((a>=ry1) && (a<=ry2)) { return 1; } a = cortey(rx2); if ((a>=ry1) && (a<=ry2)) { return 1; } return 0; } int main(void) { int i; int n; scanf("%d", &n); for(i=0; i<n; i++) { scanf(" %d %d %d %d %d %d %d %d", &sx1, &sy1, &sx2, &sy2, &rx1, &ry1, &rx2, &ry2); if (rx1 > rx2) { rx1 ^= rx2 ^= rx1 ^= rx2; } if (ry1 > ry2) { ry1 ^= ry2 ^= ry1 ^= ry2; } if (calc()) { printf("T\n"); } else { printf("F\n"); } } exit(0); }