题目地址:pdf版
六类子问题:
1 求三角形的外接圆 // 白书上的模板还没有理解清楚。
2 求三角形的内切圆
3 过定点求圆的切线 (的极角) ,注意,极角返回的是-PI~PI ,所以如果小于0要加上PI。
4 给定两条不平行的直线,给定半径,求圆心坐标使得圆与两条直线相切。
5 给定一条直线,给定一个点,求半径为r,过这个点,与直线相切的圆。
6 给定两个相离的圆,给半径r,求圆心坐标使得两两外切。
代码很长啊,还好1A了,不然查错查死了。
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
const long double eps=1e-10;
const long double PI=acos(-1);
using namespace std;
struct Point{
long double x;
long double y;
Point(long double x=0,long double y=0):x(x),y(y){}
//void operator<<(Point &A) {cout<<A.x<<' '<<A.y<<endl;}
};
int dcmp(long double x) {return (x>eps)-(x<-eps); }
typedef Point Vector;
Vector operator +(Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y);}
Vector operator -(Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); }
Vector operator *(Vector A,long double p) { return Vector(A.x*p,A.y*p); }
Vector operator /(Vector A,long double p) {return Vector(A.x/p,A.y/p);}
ostream &operator<<(ostream & out,Point & P) { out<<P.x<<' '<<P.y<<endl; return out;}
//
bool operator< (const Point &A,const Point &B) { return dcmp(A.x-B.x)<0||(dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)<0); }
bool operator== ( const Point &A,const Point &B) { return dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)==0;}
long double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}
long double Cross(Vector A,Vector B) {return A.x*B.y-B.x*A.y; }
long double Length(Vector A) { return sqrt(Dot(A, A));}
long double Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));}
long double Area2(Point A,Point B,Point C ) {return Cross(B-A, C-A);}
Vector Rotate(Vector A,long double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}
Vector Normal(Vector A) {long double L=Length(A);return Vector(-A.y/L,A.x/L);}
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
Vector u=P-Q;
long double t=Cross(w, u)/Cross(v,w);
return P+v*t;
}
long double DistanceToLine(Point P,Point A,Point B)
{
Vector v1=P-A; Vector v2=B-A;
return fabs(Cross(v1,v2))/Length(v2);
}
long double DistanceToSegment(Point P,Point A,Point B)
{
if(A==B) return Length(P-A);
Vector v1=B-A;
Vector v2=P-A;
Vector v3=P-B;
if(dcmp(Dot(v1,v2))==-1) return Length(v2);
else if(Dot(v1,v3)>0) return Length(v3);
else return DistanceToLine(P, A, B);
}
Point GetLineProjection(Point P,Point A,Point B)
{
Vector v=B-A;
Vector v1=P-A;
long double t=Dot(v,v1)/Dot(v,v);
return A+v*t;
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
long double c1=Cross(b1-a1, a2-a1);
long double c2=Cross(b2-a1, a2-a1);
long double c3=Cross(a1-b1, b2-b1);
long double c4=Cross(a2-b1, b2-b1);
return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0 ;
}
bool OnSegment(Point P,Point A,Point B)
{
return dcmp(Cross(P-A, P-B))==0&&dcmp(Dot(P-A,P-B))<0;
}
long double PolygonArea(Point *p,int n)
{
long double area=0;
for(int i=1;i<n-1;i++)
{
area+=Cross(p[i]-p[0], p[i+1]-p[0]);
}
return area/2;
}
Point read_point()
{
Point P;
scanf("%Lf%Lf",&P.x,&P.y);
return P;
}
// ---------------与圆有关的--------
struct Circle
{
Point c;
long double r;
Circle(Point c=Point(0,0),long double r=0):c(c),r(r) {}
Point point(long double a)
{
return Point(c.x+r*cos(a),c.y+r*sin(a));
}
};
struct Line
{
Point p;
Vector v;
Line(Point p=Point(0,0),Vector v=Vector(0,1)):p(p),v(v) {}
Point point(long double t)
{
return Point(p+v*t);
}
};
int getLineCircleIntersection(Line L,Circle C,long double &t1,long double &t2,vector<Point> &sol)
{
long double a=L.v.x;
long double b=L.p.x-C.c.x;
long double c=L.v.y;
long double d=L.p.y-C.c.y;
long double e=a*a+c*c;
long double f=2*(a*b+c*d);
long double g=b*b+d*d-C.r*C.r;
long double delta=f*f-4*e*g;
if(dcmp(delta)<0) return 0;
if(dcmp(delta)==0)
{
t1=t2=-f/(2*e);
sol.push_back(L.point(t1));
return 1;
}
else
{
t1=(-f-sqrt(delta))/(2*e);
t2=(-f+sqrt(delta))/(2*e);
sol.push_back(L.point(t1));
sol.push_back(L.point(t2));
return 2;
}
}
// 向量极角公式
long double angle(Vector v) {return atan2(v.y,v.x);}
int getCircleCircleIntersection(Circle C1,Circle C2,vector<Point> &sol)
{
long double d=Length(C1.c-C2.c);
if(dcmp(d)==0)
{
if(dcmp(C1.r-C2.r)==0) return -1; // 重合
else return 0; // 内含 0 个公共点
}
if(dcmp(C1.r+C2.r-d)<0) return 0; // 外离
if(dcmp(fabs(C1.r-C2.r)-d)>0) return 0; // 内含
long double a=angle(C2.c-C1.c);
long double da=acos((C1.r*C1.r+d*d-C2.r*C2.r)/(2*C1.r*d));
Point p1=C1.point(a-da);
Point p2=C1.point(a+da);
sol.push_back(p1);
if(p1==p2) return 1; // 相切
else
{
sol.push_back(p2);
return 2;
}
}
// 求点到圆的切线
int getTangents(Point p,Circle C,Vector *v)
{
Vector u=C.c-p;
long double dist=Length(u);
if(dcmp(dist-C.r)<0) return 0;
else if(dcmp(dist-C.r)==0)
{
v[0]=Rotate(u,PI/2);
return 1;
}
else
{
long double ang=asin(C.r/dist);
v[0]=Rotate(u,-ang);
v[1]=Rotate(u,+ang);
return 2;
}
}
// 求两圆公切线
bool flag=0;
int getTangents(Circle A,Circle B,Point *a,Point *b)
{
int cnt=0;
if(A.r<B.r)
{
swap(A,B); swap(a, b); // 有时需标记
}
long double d=Length(A.c-B.c);
long double rdiff=A.r-B.r;
long double rsum=A.r+B.r;
if(dcmp(d-rdiff)<0) return 0; // 内含
long double base=angle(B.c-A.c);
if(dcmp(d)==0&&dcmp(rdiff)==0) return -1 ; // 重合 无穷多条切线
if(dcmp(d-rdiff)==0) // 内切 外公切线
{
a[cnt]=A.point(base);
b[cnt]=B.point(base);
cnt++;
return 1;
}
// 有外公切线的情形
long double ang=acos(rdiff/d);
a[cnt]=A.point(base+ang);
b[cnt]=B.point(base+ang);
cnt++;
a[cnt]=A.point(base-ang);
b[cnt]=B.point(base-ang);
cnt++;
if(dcmp(d-rsum)==0) // 外切 有内公切线
{
a[cnt]=A.point(base);
b[cnt]=B.point(base+PI);
cnt++;
}
else if(dcmp(d-rsum)>0) // 外离 又有两条外公切线
{
long double ang_in=acos(rsum/d);
a[cnt]=A.point(base+ang_in);
b[cnt]=B.point(base+ang_in+PI);
cnt++;
a[cnt]=A.point(base-ang_in);
b[cnt]=B.point(base-ang_in+PI);
cnt++;
}
return cnt;
}
// 求内切圆坐标
Circle InscribledCircle(Point p1,Point p2,Point p3)
{
double a=Length(p2-p3);
double b=Length(p3-p1);
double c=Length(p1-p2);
Point I=(p1*a+p2*b+p3*c)/(a+b+c);
return Circle(I,DistanceToLine(I, p1, p2));
}
// 外接圆模板怎么证明?
Circle CircumscribedCircle(Point p1,Point p2,Point p3)
{
double Bx=p2.x-p1.x,By=p2.y-p1.y;
double Cx=p3.x-p1.x,Cy=p3.y-p1.y;
double D=2*(Bx*Cy-By*Cx);
double cx=(Cy*(Bx*Bx+By*By)-By*(Cx*Cx+Cy*Cy))/D+p1.x;
double cy=(Bx*(Cx*Cx+Cy*Cy)-Cx*(Bx*Bx+By*By))/D+p1.y;
Point p=Point(cx,cy);
return Circle(p,Length(p-p1));
}
int main()
{
string s;
Point A,B,C;
while(cin>>s)
{
if(s=="CircumscribedCircle")
{
A=read_point();
B=read_point();
C=read_point();
Circle ans=CircumscribedCircle(A, B, C);
printf("(%.6Lf,%.6Lf,%.6Lf)\n",ans.c.x,ans.c.y,ans.r);
}
else if(s=="InscribedCircle")
{
A=read_point();
B=read_point();
C=read_point();
Circle ans=InscribledCircle(A, B, C);
printf("(%.6Lf,%.6Lf,%.6Lf)\n",ans.c.x,ans.c.y,ans.r);
}
else if(s=="TangentLineThroughPoint")
{
Circle C;
C.c=read_point();
scanf("%Lf",&C.r);
Point p=read_point();
Vector v[2];
long double deg[2];
int cnt=getTangents(p, C, v);
if(cnt)
{
for(int i=0;i<cnt;i++) // atan2 返回的极角范围是-PI到PI
if(dcmp(angle(v[i]))<0)
{
deg[i]=(angle(v[i])+PI)/PI*180;
}
else
deg[i]=angle(v[i])/PI*180;
sort(deg,deg+cnt);
cout<<"[";
for(int i=0;i<cnt-1;i++)
printf("%.6Lf,",deg[i]);
printf("%.6Lf]\n",deg[cnt-1]);
}
else
{
cout<<"[]"<<endl;
}
}
else if(s=="CircleThroughAPointAndTangentToALineWithRadius")
{
Point p=read_point();
Point A=read_point();
Point B=read_point();
long double r;
scanf("%Lf",&r);
Vector v=B-A;
Vector normal_v1=Normal(v);
Vector normal_v2=Rotate(normal_v1, PI);
Point p1=A+normal_v1*r;
Point p2=A+normal_v2*r;
Line L1=Line(p1,v);
Line L2=Line(p2,v);
vector<Point> ans;
long double t1=0,t2=0;
int cnt=getLineCircleIntersection(L1, Circle(p,r), t1, t2, ans);
cnt+=getLineCircleIntersection(L2, Circle(p,r), t1, t2, ans);
if(!cnt)
{
cout<<"[]"<<endl;
}
else
{
sort(ans.begin(),ans.end());
cout<<"[";
for(int i=0;i<cnt-1;i++)
printf("(%.6Lf,%.6Lf),",ans[i].x,ans[i].y);
printf("(%.6Lf,%.6Lf)]\n",ans[cnt-1].x,ans[cnt-1].y);
}
}
else if(s=="CircleTangentToTwoLinesWithRadius")
{
Point A1=read_point(),B1=read_point(),A2=read_point(),B2=read_point();
long double r;
scanf("%Lf",&r);
Vector v1=B1-A1;
Vector v2=B2-A2;
Vector normal_1_1=Normal(v1);
Vector normal_1_2=Rotate(normal_1_1, PI);
Point p1=A1+normal_1_1*r;
Point p2=A1+normal_1_2*r;
// Line L1=Line(p1,v1);
// Line L2=Line(p2,v1);
Vector normal_2_1=Normal(v2);
Vector normal_2_2=Rotate(normal_2_1, PI);
Point p3=A2+normal_2_1*r;
Point p4=A2+normal_2_2*r;
// Line L3=Line(p3,v2);
// Line L4=Line(p4,v2);
vector<Point> ans;
ans.push_back(GetLineIntersection(p1, v1, p3, v2));
ans.push_back(GetLineIntersection(p1, v1, p4, v2));
ans.push_back(GetLineIntersection(p2, v1, p3, v2));
ans.push_back(GetLineIntersection(p2, v1, p4, v2));
sort(ans.begin(),ans.end());
cout<<"[";
for(int i=0;i<3;i++)
printf("(%.6Lf,%.6Lf),",ans[i].x,ans[i].y);
printf("(%.6Lf,%.6Lf)]\n",ans[3].x,ans[3].y);
}
else if(s=="CircleTangentToTwoDisjointCirclesWithRadius")
{
Circle A,B;
A.c=read_point();
scanf("%Lf",&A.r);
B.c=read_point();
scanf("%Lf",&B.r);
long double r;
scanf("%Lf",&r);
vector<Point> ans;
int cnt=getCircleCircleIntersection(Circle(A.c,A.r+r), Circle(B.c,B.r+r), ans);
if(!cnt)
{
cout<<"[]"<<endl;
}
else
{
sort(ans.begin(),ans.end());
cout<<"[";
for(int i=0;i<cnt-1;i++)
printf("(%.6Lf,%.6Lf),",ans[i].x,ans[i].y);
printf("(%.6Lf,%.6Lf)]\n",ans[cnt-1].x,ans[cnt-1].y);
}
}
}
}