POJ1269- Intersecting Lines

题意:给出四个点,判断两条线段是否,相交(求交点),同一条直线,平行

思路:首先判断是否为同一条直线,利用三点共线,求p1 p2 p3 与p1 p2 p4是否同时共线

其次判断是否平行,利用(x1 - x2) * (y3 - y4) = (x3 - x4) * (y1 - y2)

最后判断就是求交点,假设相交的点p(x, y)

那个交点与另外两条线段的端点一定共线,所以可以得到两个方程:(x1 - x)(y2 - y) - (y - y1)(x - x2) = 0, (x3 - x)(y4 - y) - (y3 - y)(x4 - 4) = 0

#include<stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

struct Point {
	double x, y;
};

Point p1,p2,p3,p4;

double Direction(Point p1, Point p2, Point p3) {
	return (p2.x - p1.x)*(p3.y - p1.y) - (p3.x - p1.x)*(p2.y - p1.y);
}

void DealPoint() {
	if (Direction(p1,p2,p3) == 0 && Direction(p1,p2,p4) == 0) {
		printf("LINE\n");
	}
	else if ((p2.x - p1.x) * (p4.y - p3.y) == (p4.x - p3.x) * (p2.y - p1.y)) 	{
		printf("NONE\n");
	}
	else {
		double a1 = p1.y - p2.y;
		double b1 = p2.x - p1.x;
		double c1 = p1.x*p2.y - p2.x*p1.y;
		double a2 = p3.y - p4.y;
		double b2 = p4.x - p3.x;
		double c2 = p3.x*p4.y - p4.x*p3.y;
		double x = (b1*c2 - b2*c1)/(a1*b2 - a2*b1);
		double y = (a2*c1 - a1*c2)/(a1*b2 - a2*b1);
		printf("POINT %.2lf %.2lf\n", x, y);
	}
}

int main() {
	int n;
	scanf("%d", &n);
	printf("INTERSECTING LINES OUTPUT\n");
	for (int i = 0; i < n; ++i) {
		scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &p1.x, &p1.y, &p2.x, &p2.y, &p3.x, &p3.y, &p4.x, &p4.y);
		DealPoint();
	}
	printf("END OF OUTPUT\n");
	return 0; 
}


你可能感兴趣的:(POJ1269- Intersecting Lines)