欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poj 1269 Intersecting Lines (基础数学几何 直线位置)

程序员文章站 2022-03-02 10:21:06
...

给你两条直线(前几次wa都理解成线段了),求 位置状况

1.相离(如果是直线也就是平行)

2.共线

3.相交

 

附上AC代码

#include <stdio.h>
#include <math.h>
struct point{
	double x , y;
};
struct line{
	point l , r;
}t1,t2;
double xmul(point t,point l,point r){
	double x1 , x2 , y1 , y2;
	x1 = l.x - t.x; y1 = l.y - t.y;
	x2 = r.x - t.x; y2 = r.y - t.y;
	return x1 * y2 - x2 * y1;
}
point solve(point u1,point u2,point v1,point v2){
	point ret = u1;
	double t = ((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
					/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
	ret.x += (u2.x - u1.x) * t;
	ret.y += (u2.y - u1.y) * t;
	return ret;
}
int n;
int main(){
	scanf("%d",&n);
	printf("INTERSECTING LINES OUTPUT\n");
	for(int i = 0 ; i < n ; i++){
		scanf("%lf%lf%lf%lf",&t1.l.x,&t1.l.y,&t1.r.x,&t1.r.y);
		scanf("%lf%lf%lf%lf",&t2.l.x,&t2.l.y,&t2.r.x,&t2.r.y);
		if(xmul(t2.l,t1.l,t1.r)==0&&xmul(t2.r,t1.l,t1.r)==0){
			printf("LINE\n");
		}else if(xmul(t2.l,t1.l,t1.r)==xmul(t2.r,t1.l,t1.r)){
			printf("NONE\n");
		}else{
			point ans = solve(t1.l,t1.r,t2.l,t2.r); 
			printf("POINT %.2lf %.2lf\n",ans.x,ans.y);
		}	
	}
	printf("END OF OUTPUT\n");
	return 0;
}

 

相关标签: 几何