计算几何

模板(不完整)

缺省源

#include<bits/stdc++.h>
#define db double
using namespace std;

const int N=25;
const db pi=acos(-1),eps=1e-8;

int sign(db x){return x<eps? (-x<eps? 0:-1):1;}
struct Point
{
	db x,y;
	void print(){printf("%.3lf %.3lf\n",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 db &B) const
	{return (Point){x*B,y*B};}
	Point operator / (const db &B) const
	{return (Point){x/B,y/B};}
	db operator | (const Point &B) const
	{return (x*B.x)+(y*B.y);}
	db operator ^ (const Point &B) const
	{return (x*B.y)-(y*B.x);}
};
db dis(Point A,Point B){return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));}
struct Line
{
	Point a,b;
	db len(){return dis(a,b);}
};

int cmpPoint(Point A,Point B)
{
	if((A.x<B.x) || (sign(A.x-B.x)==0 && A.y<B.y))
		return -1;
	if(sign(A.x-B.x)==0 && sign(A.y-B.y)==0)
		return 0;
	return 1;
}

bool cmp(Point A,Point B)
{
	if(A.x==B.x)
		return A.y<B.y;
	return A.x<B.x;
}

bool anticlock(Point A,Point B,Point C){return ((A-B)^(C-B))>0;}

db tris(Point A,Point B,Point C){return fabs((B-A)^(C-A));}

bool isinter(Point A,Point B,Point C,Point D)
{
	if(cmpPoint(A,B)>0)
		swap(A,B);
	if(cmpPoint(C,D)>0)
		swap(C,D);
	if(sign((B-A)^(C-D))==0)
	{
		if(sign((B-A)^(C-A))==0)
		{
			if(cmpPoint(A,C)<=0 && cmpPoint(B,C)>=0)
				return 1;
			if(cmpPoint(C,A)<=0 && cmpPoint(D,A)>=0)
				return 1;
			return 0;
		}
	}
	db s1=((B-A)^(C-A));
	db s2=((B-A)^(D-A));
	db s3=((D-C)^(A-C));
	db s4=((D-C)^(B-C));
	if((s1*s2<=0 && s3*s4<0) || (s1*s2<0 && s3*s4<=0))
		return 1;
	return 0;
}

db disl0(Point A,Line L){return abs((L.a-A)^(L.b-A))/L.len();}

int main()
{
	
	return 0;
}

二维凸包 & 旋转卡壳

void hull()
{
    sort(a+1,a+1+n,cmp);

	sta[1]=1;  sta[top=2]=2;
	for(int i=3; i<=n; i++)
	{
		while(top>1 && !anticlock(a[i],a[sta[top]],a[sta[top-1]]))
			top--;
		sta[++top]=i;
	}
	for(int i=1; i<=top; i++)
		t[++tot]=a[sta[i]];

	sta[1]=n;  sta[top=2]=n-1;
	for(int i=n-2; i>=1; i--)
	{
		while(top>1 && !anticlock(a[i],a[sta[top]],a[sta[top-1]]))
			top--;
		sta[++top]=i;
	}
	for(int i=2; i<=top-1; i++)
		t[++tot]=a[sta[i]];
}

void rothull()
{
	if(tot==2)
		return ans=dis(t[1],t[2]),void();
	t[0]=t[tot];
	int pos=2;
	for(int i=1; i<=tot; i++)
	{
		while(sign(tris(t[pos%tot+1],t[i],t[i-1])-tris(t[pos],t[i],t[i-1]))>=0)
			pos=pos%tot+1;
		ans=max(ans,dis(t[pos],t[i]));
		ans=max(ans,dis(t[pos],t[i-1]));
	}
}

平面最近点对

void solve(int l,int r)
{
	if(l==r)
		return;
	int mid=(l+r)>>1;  Point mm=a[mid];
	solve(l,mid);  solve(mid+1,r);
	int pl=l,pr=mid+1,tot=l-1;
	while(tot<r)
	{
		if(pl<=mid && (a[pl].y<a[pr].y || pr>r))
			t[++tot]=a[pl++];
		else
			t[++tot]=a[pr++];
	}
	for(int i=l; i<=r; i++)
		a[i]=t[i];

	tot=0;
	for(int i=l; i<=r; i++)
		if(sign(ans-dis((Point){a[i].x,0},(Point){mm.x,0}))>0)
			t[++tot]=a[i];
	for(int i=1; i<=tot; i++)
	{
		for(int j=i+1; j<=tot; j++)
		{	
			if(sign(ans-dis((Point){0,t[i].y},(Point){0,t[j].y}))>0)
				ans=min(ans,dis(t[i],t[j]));
			else
				break;
		}
	}
}
posted @   xishanmeigao  阅读(4)  评论(0编辑  收藏  举报
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示