计算直线ax+by+c=0的表达式[模板]

d o u b l e 线 实现了不用double表示直线

a , b , c a x + b y + c = 0 也就是计算了a,b,c得到ax+by+c=0

#include <bits/stdc++.h>
using namespace std;
const int maxn=1009;
int X[maxn],Y[maxn],tot,ans,n;
int gcd(int a,int b){
	return b==0?a:gcd(b,a%b);
}
map< pair<int,int>,set<int> >mp;
int main()
{
	cin >> n;
	for(int i=1;i<=n;i++)	cin >> X[i] >> Y[i];
	for(int i=1;i<=n;i++)
	for(int j=i+1;j<=n;j++)
	{
		int x=X[i],y=Y[i],A,B,C;
		int x2=X[j],y2=Y[j];
		int q=x-x2,w=y-y2;
		if( q&&w )
		{
			int s=gcd(q,w);
			A=w/s,B=q/s;
			if( A<0 )	A=-A,B=-B;//让直线符号统一
		}
		else if( w==0&&q )	A=0,B=1;//w是0,斜率为0,表达式是和x平行的线,y=()
		else if( w&&q==0 )	A=1,B=0;//q是0,斜率不存在,表达式是和y轴平行的线,x=()
		A=-A;//A应该是负,见上面的分析
		C=-A*x-B*y;
		pair<int,int> sp(A,B);
		if( !mp[sp].count(C) )//没有这条直线 
		{
			tot++;//记录直线总数 
			mp[sp].insert(C);//插入这条直线 
		}
	}
}

猜你喜欢

转载自blog.csdn.net/jziwjxjd/article/details/107759348
今日推荐