cf#21-B-Intersection-计算几何-求直线交点个数

http://codeforces.com/problemset/problem/21/B 

计算几何-给出2条线段求交点个数,AX+BY+C=0,求交点用公式, 

 

 需要先判断是否重合,重合输出-1,有一个坑点是,ABC,可能全为0。AB=0时,若C=0,表示所有点集。若C!=0,表示空集,答案一定为0。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#include <iostream>
using namespace std;
const double pi=acos(-1.0);
const double eps=1e-6;

struct POINT
{
	double x;
	double y;
	POINT(double a=0, double b=0) { x=a; y=b;}
};

struct LINESEG
{
	POINT s;
	POINT e;
	LINESEG(POINT a, POINT b) { s=a; e=b;}
	LINESEG() { }
};struct LINE
{
	double a;
	double b;
	double c;
	LINE(double d1=1, double d2=-1, double d3=0) {a=d1; b=d2; c=d3;}
};

bool lineintersect(LINE l1,LINE l2,POINT &p) // 是 L1,L2
{
	double d=l1.a*l2.b-l2.a*l1.b;
	if(abs(d)<eps) // 不相交
		return false;
	//p.x = (l2.c*l1.b-l1.c*l2.b)/d;
	//p.y = (l2.a*l1.c-l1.a*l2.c)/d;
	return true;
}
int equals(double a,double b)
{ 
	return fabs(a-b)<eps;
}
int main()
{ 
	POINT pp;
	double a,b,c;
	double A,B,C;
	scanf("%lf%lf%lf",&a,&b,&c);
	LINE l1(a,b,c);
	scanf("%lf%lf%lf",&A,&B,&C);
	LINE l2(A,B,C); 
 
	//重合   xie:-a/b
	if (a==0&&b==0&&c!=0)
	{
			printf("0\n"); 
		return 0;
	}if (A==0&&B==0&&C!=0)
	{ 
			printf("0\n"); 
		return 0;
	}
		if (a==0&&b==0&&c==0)
	{
			printf("-1\n"); 
		return 0;
	}
if (A==0&&B==0&&C==0)
	{ 
			printf("-1\n"); 
		return 0;
	}
	  if (B==0&&b==0)
	{
		 if (equals(c/a,C/A))
		 {printf("-1\n");return 0;}


	}
	else
		if (A==0&&a==0)
		{ 
			if (equals(c/b,C/B))
			{	printf("-1\n");return 0;}
		}
		else
		if (equals(a/a,A/A)&&equals(b/a,B/A)&&equals(c/a,C/A))
		 
			{	printf("-1\n");return 0;}
		 
			bool ret =lineintersect(l1,l2,pp);
			if (ret==true)
				printf("1\n");
			else
				printf("0\n");
	 
		
		return 0;
		
}


你可能感兴趣的:(cf#21-B-Intersection-计算几何-求直线交点个数)