poj 1269 Intersecting Lines (判断两条直线的状态)

题目连接:http://poj.org/problem?id=1269


题目思路:给出两条直线,给出的方式是两个点。 判断两条直线是平行,重合还是相交,相交的话求出交点。


解题思路:确定两点求直线, 根据直线的参数去判断状态,要注意一些特殊情况,比如平行x,y的直线等。


#include <stdio.h>
#include <string.h>

double a, b, c, d;
double x[4], y[4];
double xi, yi;

int main() {
    int n;
    while (scanf("%d", &n) == 1) {
	printf("INTERSECTING LINES OUTPUT\n");
	while (n--) {
	    for (int i = 0; i < 4; i++)
		scanf("%lf%lf", &x[i], &y[i]);

	    a = y[1] - y[0];
	    b = x[1] - x[0];
	    c = y[3] - y[2];
	    d = x[3] - x[2];

	    if (a * d == b * c && (d || a) && (b || c)) {
		if (b == 0 && d == 0) {
		    if (x[0] == x[2])
			printf("LINE\n");
		    else
			printf("NONE\n");
		}
		else if (y[0] - a * x[0] / b == y[2] - c * x[2] / d)
		    printf("LINE\n");
		else
		    printf("NONE\n");
	    }
	    else {
		if (b == 0) {
		    xi = x[0];
		    yi = c * (xi - x[2]) / d + y[2];
		}
		else if (d == 0) {
		    xi = x[2];
		    yi = a * (xi - x[0]) / b + y[0];
		}
		else {
		    xi = (b * d * (y[2] - y[0]) + a * d * x[0] - b * c * x[2]) / (a * d - b * c);
		    yi = a * (xi - x[0]) / b + y[0];
		}
		printf("POINT %.2lf %.2lf\n", xi, yi);
	    }
	}
	printf("END OF OUTPUT\n");
    }
    return 0;
}


你可能感兴趣的:(poj 1269 Intersecting Lines (判断两条直线的状态))