【POJ 1269】判断两直线相交

利用叉积解方程

#include <cstdio>
#define MAX 1<<31
#define dd double
int xmult(dd x1,dd y1,dd x2,dd y2,dd x,dd y){
	return (x1-x)*(y2-y)-(x2-x)*(y1-y);
}
int main(){
	int n;
	dd x1,y1,x2,y2,x3,y3,x4,y4;
	scanf("%d",&n);
	puts("INTERSECTING LINES OUTPUT");
	while(n--){
		scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
		dd a1=y1-y2;
		dd b1=x2-x1;
		dd c1=xmult(x1,y1,x2,y2,0,0);
		dd a2=y3-y4;
		dd b2=x4-x3;
		dd c2=xmult(x3,y3,x4,y4,0,0);
		if(a1*b2==a2*b1){
			if(xmult(x1,y1,x2,y2,x3,y3)==0)
				puts("LINE");
			else puts("NONE");
		}
		else {
			dd cx,cy;
			cx=(b1*c2-c1*b2)/(a1*b2-a2*b1);
			cy=(a1*c2-c1*a2)/(b1*a2-b2*a1);
			printf("POINT %.2f %.2f\n",cx,cy);
		}
	}
	puts("END OF OUTPUT");
}

利用点斜式解方程

#include <cstdio>
#define MAX 1<<31
#define dd double
struct P{
	dd x,y;
	void input(){
		scanf("%lf%lf",&x,&y);
	}
	void output(){
		printf("POINT %.2f %.2f\n",x,y);
	}
};
struct L{
	P s,e;
	void input(){
		s.input(),e.input();
	}
	dd k(){
		if(s.x==e.x)return MAX;
		return (s.y-e.y)/(s.x-e.x);
	}
}l1,l2;
int xmult(P a,P b,P o){
	return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
}
void getCross(L a,L b){
	P c;
	dd ka=a.k(),kb=b.k();
	if(ka==MAX){//a是竖直的
		c.x=a.s.x;
		c.y=(c.x-b.s.x)*kb+b.s.y;
	}
	else{
		if(kb==MAX)
			c.x=b.s.x;
		else
			c.x=(a.s.y-b.s.y-ka*a.s.x+kb*b.s.x)/(kb-ka);
		c.y=(c.x-a.s.x)*ka+a.s.y;
	}
	c.output();
}
int main(){
	int n;
	scanf("%d",&n);
	puts("INTERSECTING LINES OUTPUT");
	while(n--){
		l1.input(),l2.input();
		dd ka=l1.k(),kb=l2.k();
		if(ka==kb){
			if(xmult(l1.s,l1.e,l2.s)==0)
				puts("LINE");
			else puts("NONE");
		}
		else
			getCross(l1,l2);
	}
	puts("END OF OUTPUT");
}

  

时间: 2024-11-08 13:58:46

【POJ 1269】判断两直线相交的相关文章

POJ 1269 Intersecting Lines 直线相交判断

D - Intersecting Lines Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1269 Appoint description:  System Crawler  (2016-05-08) Description We all know that a pair of distinct points on a plane d

Pick-up sticks(判断两直线相交)

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11335   Accepted: 4250 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

poj 1269 线段与线段相交

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13605   Accepted: 6049 Description We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three

You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6837 Accepted Submission(s): 3303 Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. A

poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are

判断两直线是否平行

公式如下: 若直线A1x十+B1y+十C1=0与直线A2x十+2y+十C2=0平行,则: A1/A2=B1/B2≠C1/C2 ①若B1=B2=0,此时两直线斜率不存在,满足:A1/A1=B1/B2≠C1/;C2: ②若B1≠0.B2≠0,此时也满足A1/A2=B1/B2≠C1/C2 则两直线平行,有:A1/A2=B1/B2≠C1/C2 上代码: 1 /* 2 *函数名:if_not_parallel 3 *功能:两条直线不垂直的情况下,判断是否相交 4 *输入:(x1,y1),(x2,y2)是线

简单地判断判断两矩形相交/重叠 C#

最近需要用到矩形相交算法的简单应用,所以特地拿一个很简单的算法出来供新手参考,为什么说是给新手的参考呢因为这个算法效率并不是很高,但是这个算法只有简简单单的三行.程序使用了两种方法来判断是否重叠/相交,如果有兴趣可以看一下,如果觉得有bug可以留言.代码仅供参考. C#中矩形的方法为Rectangl(起始点坐标, 矩形的大小)或Rectangl(起始点x坐标, 起始点y坐标, 矩形宽, 矩形高),起始点为矩形区域的左上角. 方法一 姑且叫做“井字法”吧,延长其中一个矩形的四边使其形成一“井”字(

POJ 1269 - Intersecting Lines 直线与直线相交

题意:    判断直线间位置关系: 相交,平行,重合 1 include <iostream> 2 #include <cstdio> 3 using namespace std; 4 struct Point 5 { 6 int x , y; 7 Point(int a = 0, int b = 0) :x(a), y(b) {} 8 }; 9 struct Line 10 { 11 Point s, e; 12 int a, b, c;//a>=0 13 Line() {

判断两直线是否相交 hdu1086

1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 struct line 7 { 8 double x1; 9 double y1; 10 double x2; 11 double y2; 12 }l[110]; 13 14 bool test(int i,int j) 15 { 16 int s=0; 17 double acd = (l[j].x1-l[i].x1)*(l[j].y1-l