跳到正文

题解

题解:CF23D Tetragon

yangmuguang约 9 分钟
原文
文章信息与版本

文章信息

收录
2026年7月12日
更新
2026年10月27日
篇幅
约 9 分钟
获赞
2
题目
CF23D

版本

  1. v1当前

题意

给定二维平面上的三个点,已知这三个点分别是三个相等长度线段的中点,且这三条线段是一个凸四边形的三个边,求这个凸多边形。

思路

该题图示

分类讨论 A,B,CA,B,C 三点所在线段(设为 l1,l2,l3l_1,l_2,l_3)哪两条不相交,即隔着第四边 l4l_4。此处假设是 l1,l3l_1,l_3。显然,l1,l2l_1,l_2 交点 EE 在 ABAB 的垂直平分线上,l2,l3l_2,l_3 交点 DD 在 BCBC 的垂直平分线上。又因为 BE=BDBE=BD,所以 DD 也一定在一条过 FF 的一条平行于 ABAB 垂直平分线上,其中 BF=12ABBF=\frac{1}{2}AB 且 FF 在 ABAB 延长线上。这样就得到了 DD 的位置,倍长 DBDB 得到 EE,再分别倍长 DC,EADC,EA 得到另外两个点得到四边形,最后判断是否是凸的即可。此处我使用了 O(n2)O(n^2) 判断,可以通过。

代码
C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define faster ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0)
namespace ymg
{
	namespace geo
	{
		struct point
		{
			double x=0,y=0;
			bool flag=0;
		};
		struct line
		{
			double k=0,b=0;
			bool flag=0;
			bool ver=0;
		};
		struct polygon
		{
			basic_string<point>V;
		};
		istream& operator>>(istream& st,point& A)
		{
			st>>A.x>>A.y;
			return st;
		}
		istream& operator>>(istream& st,line& l)
		{
			st>>l.k>>l.b;
			return st;
		}
		istream& operator>>(istream& st,polygon& P)
		{
			for (ll i=0;i<P.V.size();i++)
			{
				st>>P.V[i];
			}
			return st;
		}
		ostream& operator<<(ostream& st,point A)
		{
			st<<A.x<<" "<<A.y;
			return st;
		}
		ostream& operator<<(ostream& st,line l)
		{
			st<<l.k<<" "<<l.b;
			return st;
		}
		ostream& operator<<(ostream& st,polygon P)
		{
			for (point A:P.V)
			{
				st<<A<<" ";
			}
			return st;
		}
		point cross(line l1,line l2)
		{
			if (l1.flag||l2.flag)
			{
				return {0,0,1};
			}
			if (l1.ver&&l2.ver)
			{
				return {0,0,1};
			}
			if (l1.ver)
			{
				return {l1.b,l1.b*l2.k+l2.b};
			}
			if (l2.ver)
			{
				return {l2.b,l2.b*l1.k+l1.b};
			}
			if (fabsl(l1.k-l2.k)<1e-10)
			{
				return {0,0,1};
			}
			double x=(l2.b-l1.b)/(l1.k-l2.k);
			return {x,l1.k*x+l1.b,0};
		}
		point midpoint(point A,point B)
		{
			return {(A.x+B.x)/2,(A.y+B.y)/2};
		}
		line connect(point A,point B)
		{
			if (fabsl(A.x-B.x)<1e-10)
			{
				if (fabsl(A.y-B.y)<1e-10)return {0,0,1,0};
				return {0,A.x,0,1};
			}
			return {(B.y-A.y)/(B.x-A.x),A.y-A.x*(B.y-A.y)/(B.x-A.x),0,0};
		}
		line bisect(point A,point B)
		{
			if (fabs(A.y-B.y)<1e-10)
			{
				if (fabs(A.x-B.x)<1e-10)
				{
					return {0,A.y,1,0};
				}
				return {0,(A.x+B.x)/2,0,1};
			}
			if (fabs(A.x-B.x)<1e-10)
			{
				return {0,(A.y+B.y)/2,0,0};
			}
			point C=midpoint(A,B);
			line l=connect(A,B);
			return {-1/(l.k),C.y-(-1/(l.k)*C.x),0,0};
		}
		ll dis(point A,point B)
		{
			return sqrtl((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
		}
		ll on(line l,point A)
		{
			if (l.flag||A.flag)return -1;
			if (l.ver)
			{
				if (fabs(A.x-l.b)<1e-10)return 1;
				return 0;
			}
			if (fabsl(l.k*A.x+l.b-A.y)<1e-10)return 1;
			return 0;
		}
		line parallel(line l,point A)
		{
			if (l.flag||A.flag)
			{
				return {0,0,1,0};
			}
			if (l.ver)
			{
				return {0,A.x,0,1};
			}
			return {l.k,A.y-A.x*l.k,0,0};
		}
		point flip(point A,point B)
		{
			if (A.flag||B.flag)
			{
				return {0,0,1};
			}
			return {2*B.x-A.x,2*B.y-A.y}; 
		}
		ll above(line l,point A)
		{
			if (l.flag||A.flag)return -1;
			if (l.ver)
			{
				if (A.x>l.b)return 1;
				return 0;
			}
			if (l.k*A.x+l.b<A.y)return 1;
			return 0;
		}
		bool valid(polygon P)
		{
			for (point A:P.V)
			{
				if (A.flag)return 0;
			}
			return 1;
		}
		bool convex(polygon P)
		{
			if (!valid(P))
			{
				return 0;
			}
			P.V.pb(P.V[0]);
			for (ll i=1;i<P.V.size();i++)
			{
				line l=connect(P.V[i-1],P.V[i]);
				if (l.flag)return 0;
				bool f0=0,f1=1;
				ll cnt=0;
				for (ll j=0;j<P.V.size();j++)
				{
					point A=P.V[j];
					if (on(l,A))
					{
						if (j!=0)cnt++;
						continue;
					}
					ll asd=above(l,A);
					f0|=asd;
					f1&=asd;
				}
				if ((f0&&(!f1))||cnt>2)
				{
					return 0;
				}
			}
			P.V.pop_back();
			return 1;
		}
	}
}
ll t;
using namespace ymg::geo;
bool chk(point A,point B,point C)
{
	point D=cross(bisect(B,C),parallel(bisect(A,B),flip(midpoint(A,B),B)));
	if (D.flag)return 0;
	polygon P={{flip(flip(D,B),A),flip(D,B),D,flip(D,C)}};
	if (convex(P))
	{
		cout<<"YES\n"<<P<<"\n";
		return 1;
	}
	return 0;
}
point A,B,C;
int main()
{
	faster;
	#ifdef YMG
	freopen("input.txt","r",stdin);
	#endif
	cout<<fixed<<setprecision(10);
	cin>>t;
	for (ll ii=1;ii<=t;ii++)
	{
		cin>>A>>B>>C;
		if (chk(A,B,C))continue;
		if (chk(B,C,A))continue;
		if (chk(C,A,B))continue;
		cout<<"NO\n\n";
	}
	return 0;
}
原文