解析幾何,用參數方程解
#include <iostream>
#include <math.h>
using namespace std;
typedef struct P
{
double x;
double y;
};
double dist(P p1,P p2)
{
return sqrt((p1.y-p2.y)*(p1.y-p2.y)+(p1.x-p2.x)*(p1.x-p2.x));
}
double gett(P p1,P p2)
{
return sqrt(1-(dist(p1,p2)/2)*(dist(p1,p2)/2));
}
double getsita(P p1,P p2)
{
double sita;
sita=atan(-(p1.x-p2.x)/(p1.y-p2.y));
return sita;
}
int main()
{
int n,i,j,ans,num,k;
double x1,y1,x2,y2,xm,ym,sita,t;
P temp,p[400],mid;
int T;
cin>>T;
while (T--)
{
cin>>n;
ans=1;
for (i=1;i<=n;i++) cin>>p[i].x>>p[i].y;
for (i=1;i<=n;i++)
for (j=i+1;j<=n;j++)
if (dist(p[i],p[j])<1.0001*2)
{
xm=(p[i].x+p[j].x)/2;
ym=(p[i].y+p[j].y)/2;
mid.x=xm;
mid.y=ym;
t=gett(p[i],p[j]);
sita=getsita(p[i],p[j]);
// cout<<xm<<" "<<ym<<" "<<t<<" "<<sita<<endl;
x1=xm+cos(sita)*t;
y1=ym+sin(sita)*t;
x2=xm-cos(sita)*t;
y2=ym-sin(sita)*t;
temp.x=x1;
temp.y=y1;
num=0;
// cout<<temp.x<<" "<<temp.y<<endl;
for (k=1;k<=n;k++)
{
if (dist(temp,p[k])<1.0001) num++;
}
if (num>ans) ans=num;
temp.x=x2;
temp.y=y2;
num=0;
// cout<<temp.x<<" "<<temp.y<<endl;
for (k=1;k<=n;k++)
{
if (dist(temp,p[k])<1.0001) num++;
}
if (num>ans) ans=num;
}
cout<<ans<<endl;
}
return 0;
}