Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
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);
}