其他分享
首页 > 其他分享> > Acm模板-计算几何(寄算几何)

Acm模板-计算几何(寄算几何)

作者:互联网

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define eps 1e-8
#define int128 __int128
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a/gcd(a,b)*b
#define lowbit(x) (x&-x)
#define all(x) x.begin(), x.end()
#define debug(x...) do { cout<< #x <<" -> "; re_debug(x); } while (0)
void re_debug() { cout<<'\n'; }
template<class T, class... Ts> void re_debug(const T& arg,const Ts&... args) { cout<<arg<<" "; re_debug(args...); }
int test=1;
void cut(){cout<<"test:"<<' '<<test++<<'\n';}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF=0x3f3f3f3f;
const ll LNF=0x3f3f3f3f3f3f3f3fll;
const double PI=acos(-1.0);
int sign(double x)//符号函数
{
	if(abs(x)<eps) return 0;//算是0
	if(x<0) return -1;
	return 1;
}
struct Point
{
	double x,y;
	Point operator +(const Point &b) const
	{
		return Point{x+b.x,y+b.y};
	}
	Point operator -(const Point &b) const
	{
		return Point{x-b.x,y-b.y};
	}
	Point operator *(const double &k) const
	{
		return Point{x*k,y*k};	
	}
	Point operator /(const double &k) const
	{
		return Point{x/k,y/k};
	}
	bool operator ==(const Point &b) const
	{
		return sign(x-b.x)==0&&sign(y-b.y)==0;
	}
	bool operator <(const Point &b) const
	{
		return x < b.x || (x == b.x && y < b.y); 
	}
	
};


int cmp(double x,double y)//比较函数
{
	if(abs(x-y)<eps) return 0;
	if(x<y) return -1;
	return 1;
}
double dot(Point a,Point b)//点乘
{
	return a.x*b.x+a.y*b.y;
}
double cross(Point a,Point b)//外积:表示向量A,B形成的平行四边形面积
{
	return a.x*b.y-a.y*b.x;
}
double get_lenth(Point a)//求模长 用的是向量
{
	return sqrt(dot(a,a));
}
double get_lenth(Point a,Point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double get_angle(Point a,Point b)//返回的是弧度
{
	return acos(dot(a,b)/get_lenth(a)/get_lenth(b));
}
double get_area(Point a,Point b,Point c)//返回三点构成的平行四边形的有向面积
{
	return cross(b-a,c-a);
}
Point rotate(Point a,double angle)//向量A顺指针旋转C的角度
{
	return Point{a.x*cos(angle)+a.y*sin(angle),-a.x*sin(angle)+a.y*cos(angle)};
}
Point get_line_intersection(Point p,Point v,Point q,Point w)//两个直线相交的点
{
	//两个直线是p+tv和q+tw
	Point u=p-q;
	double t=cross(w,u)/cross(v,w);
	return p+v*t;
}
double distance_to_line(Point a,Point b,Point p)//点p到直线ab的距离
{
	Point v1=b-a,v2=p-a;
	return abs(cross(v1,v2)/get_lenth(a,b));
}
double distance_to_segment(Point a,Point b,Point p)//点p到线段ab的距离
{
	if(a==b) return get_lenth(p-a);
	Point v1=b-a,v2=p-a,v3=p-b;
	if(sign(dot(v1,v2))<0) return get_lenth(v2);
	if(sign(dot(v1,v3))>0) return get_lenth(v3);
	return distance_to_line(a,b,p);
}
Point get_line_projection(Point a,Point b,Point p)//点p在向量ab的投影的坐标
{
	Point v=b-a;
	return a+v*(dot(v,p-a)/dot(v,v));
}
bool is_on_segment(Point a,Point b,Point p)//点p是否在线段ab上
{
	return sign(cross(p-a,p-b))==0&&sign(dot(p-a,p-b))<=0;
}
bool is_segment_intersection(Point a1,Point a2,Point b1,Point b2)//线段a和b是否相交
{
	/*
	  double c1 = cross(a2 - a1, b1 - a1), c2 = cross(a2 - a1, b2 - a1);
	  double c3 = cross(b2 - b1, a2 - b1), c4 = cross(b2 - b1, a1 - b1);
	  return sign(c1) * sign(c2) <= 0 && sign(c3) * sign(c4) <= 0;

	 */
	
	/* 
	 a1    b2 
	   \   /
		\ /
	    /\
    b1 /  \ a2
	
	*/
	double c1=cross(a2-a1,b1-a1),c2=cross(a2-a1,b2-a1);
	double c3=cross(b2-b1,a2-b1),c4=cross(b2-b1,a1-b1);
	return sign(c1)*sin(c2)<=0&&sign(c3)*sign(c4)<=0;
}
double get_triangle_area(Point a,Point b,Point c)//得到三个点围城的三角形面积 
{
	//海伦公式 p=(a+b+c)/2 S=sqrt((p-a)*(p-b)*(p-c)));
	double len_a=get_lenth(a-b);
	double len_b=get_lenth(a-c);
	double len_c=get_lenth(b-c);
	double p=(len_a+len_b+len_c)/2;
	return sqrt(p*(p-len_a)*(p-len_b)*(p-len_c));
}
double polygon_area(Point p[],int n)//求多边形面积
{
	double ans=0;
	for(int i=1;i+1<n;i++)
	{
		ans+=cross(p[i]-p[0],p[i+1]-p[i]);
	}
	return ans/2;
}
void solve()
{
	cout<<get_triangle_area({0,0},{1,1},{1,1})<<'\n';
}
int main()
{
	IOS;
	int T=1;
	//	cin>>T;
	while(T--) solve();
	return 0^0;
}

标签:const,cout,Point,Acm,return,几何,debug,寄算,define
来源: https://www.cnblogs.com/Meteor-Z/p/16560351.html