您的位置:首页 > 其它

uva 12304 2D Geometry 110 in 1! 计算几何

2015-01-15 16:10 281 查看
刚开始学计算几何。 写了一天。也积累了很多经验。向量旋转不要轻易用,主要是不知道是是顺时针旋转还是逆时针旋转,容易错误。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<ctime>
#include<stdlib.h>
using namespace std;
const int mod=99999997;
const int mmax=200010;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3fffffff;
#define debug
#define mmax 200010
//typedef __int64 LL;

/*

double Dot(Vector A,Vector B)  点积
double Length(Vector A)  取模
double Angle(Vector A,Vector B) 夹角
double Cross(Vector A,Vector B) 叉积
double Area2(Point A,Point B,Point C) 有向面积
Vector Ratate(Vector A,double rad)  向量旋转
Vector Normal(Vector A) 法向量
Point GetLineIntersection(Point P,Point Q,Vector v,Vector w) 直线相交  line1 P+tv  line2 Q+tw
double Dis_Point_Line(Point P,Point A,Point B) 点到直线距离
Point GetLineProjection(Point P,Point A,Point B) 点在直线上投影
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2) 线段规范相交
bool OnSegment(Point p,Point a1,Point a2)  点在直线上
double PolygonArea(Point *p,int n) 多边形面积

*/
struct Point
{
double x,y;
Point (double x=0.0,double y=0.0):x(x),y(y) {}
void read()
{
scanf("%lf %lf",&x,&y);
}
};//点集
typedef Point Vector;
struct Line
{
Point p;
Vector v;
Line(Point p=Point(0,0),Vector v=0):p(p),v(v) {}
Point point(double t)
{
return Point(p.x+v.x*t,p.y+v.y*t);
}
};
//向量+向量=向量,向量+点=点
Vector operator + (Vector A,Vector B)
{
return Vector(A.x+B.x,A.y+B.y);
}
//点-点=向量
Vector operator - (Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
//向量*数=向量
Vector operator * (Vector A,double p)
{
return Vector(A.x*p,A.y*p);
}
//向量/数=向量
Vector operator / (Vector A,double p)
{
return Vector(A.x/p,A.y/p);
}

int sgn(double x)// 符号函数
{
if(fabs(x)<eps)
return 0;
return x<0?-1:1;
}

bool operator < (const Point &a,const Point &b)
{
return a.x<b.x || (a.x==b.x&&a.y<b.y);
}
bool operator == (const Point &a,const Point &b)
{
return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;
}

//运算部分
double Dot(Vector A,Vector B)
{
return A.x*B.x+A.y*B.y;
}
double Length(Vector A)
{
return sqrt(Dot(A,A));
}
double Angle(Vector A,Vector B)
{
return acos(Dot(A,B)/Length(A)/Length(B));
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
double Area2(Point A,Point B,Point C)
{
return Cross(B-A,C-A);
}
//向量逆时针旋转
Vector Rotate(Vector A,double rad)
{
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
Vector Normal(Vector A) //单位法线
{
double L=Length(A);
return Vector(-A.y/L,A.x/L);
}

//直线相交  line1 P+tv  line2 Q+tw
Point GetLineIntersection(Point P,Point Q,Vector v,Vector w)
{
Vector u=P-Q;
double t=Cross(w,u) / Cross(v,w);
return P+v*t;
}
//点到直线距离
double Dis_Point_Line(Point P,Point A,Point B)
{
Vector v1=B-A,v2=P-A;
return fabs(Cross(v1,v2)/Length(v1));
}
double Dis_Point_Line(Point P,Line L)
{
Vector v1=L.v,v2=P-L.p;
return fabs(Cross(v1,v2)/Length(v1));
}

double DistanceToSegment(Point p,Point a,Point b)
{
if(a==b)
return Length(p-a);
Vector v1=b-a,v2=p-a,v3=p-b;
if(sgn(Dot(v1,v2))<0) return Length(v2);
if(sgn(Dot(v1,v3))>0) return Length(v3);
return fabs(Cross(v1,v2)) / Length(v1);
}
//点在直线上的投影
Point GetLineProjection(Point P,Point A,Point B)
{
Vector v=B-A;
return A+v*(Dot(v,P-A) / Dot(v,v) );
}
//线段规范相交
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1);
double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return sgn(c1)*sgn(c2)<0&&sgn(c3)*sgn(c4)<0;
}

//点是否在线段上
bool OnSegment(Point p,Point a1,Point a2)
{
return sgn(Cross(a1-p,a2-p))==0 && sgn(Dot(a1-p,a2-p))<0;
}

//多边形又向面积
double PolygonArea(Point *p,int n)
{
double area=0.0;
for(int i=1;i<n-1;i++)
area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}

/*

圆和球相关
-------------------------------------------------

-------------------------------------------------

*/

struct Circle
{
Point c;
double r;
Circle(Point c=Point(0,0),double r=0): c(c),r(r) {}
Point point(double a)
{
return Point(c.x+cos(a)*r,c.y+sin(a)*r);
}
void read()
{
scanf("%lf %lf %lf",&c.x,&c.y,&r);
}
};
double angle(Vector v)
{
return atan2(v.y,v.x);
}

int getLineCircleIntersection(Line L,Circle C,vector<Point>& sol)//圆和直线相交
{
double a=L.v.x,b=L.p.x-C.c.x,c=L.v.y,d=L.p.y-C.c.y;
double e=a*a+c*c,f=2.0*(a*b+c*d),g=b*b+d*d-C.r*C.r;
double delta=f*f-4.0*e*g;
double t1,t2;
if(sgn(delta)<0) return 0;
if(sgn(delta)==0)
{
t1=t2=-f/(2*e);sol.push_back(L.point(t1));
return 1;
}
t1=(-f-sqrt(delta))/(2.0*e),t2=(-f+sqrt(delta))/(2.0*e);
sol.push_back(L.point(t1)),sol.push_back(L.point(t2));
return 2;
}

int getCircleCircleIntersection(Circle C1,Circle C2,vector<Point>& sol) //圆和圆相交
{
double d=Length(C1.c-C2.c);
if(sgn(d)==0)
{
if(sgn(C1.r-C2.r))
return -1; //重合
return 0;
}
if(sgn(C1.r+C2.r-d)<0)
return 0;
if(sgn(fabs(C1.r-C2.r)-d )>0)
return 0;

double a=angle(C2.c-C1.c);
double da=acos( (C1.r*C1.r+d*d-C2.r*C2.r)/ (2 * C1.r*d) );
Point p1=C1.point(a-da),p2=C1.point(a+da);
sol.push_back(p1);
if(p1==p2)
return 1;
sol.push_back(p2);
return 2;
}

int getTangents(Point p,Circle C, Vector* v)//过点p到圆c的切线,v[i]是第i条切线的向量
{
Vector u=C.c-p;
double dist=Length(u);
if(dist<C.r) return 0;
else if(sgn(dist-C.r)==0)
{
v[0]=Rotate(u,pi/2);
return 1;
}
else
{
double ang=asin(C.r/dist);
v[0]=Rotate(u,ang);
v[1]=Rotate(u,-ang);
return 2;
}
}

//圆的公切线 返回切条条数 返回-1表示无数条切线
//a[i] 和 b[i]分别是第条切线在圆A,圆B上的切点
int getTangents(Circle A,Circle B,Point *a, Point *b)
{
int cnt=0;
if(A.r<B.r)
{
swap(A,B);swap(a,b);
}
double d2=(A.c.x-B.c.x)*(A.c.x-B.c.x)+(A.c.y-B.c.y)*(A.c.y-B.c.y);
double rdiff=A.r-B.r;
double rsum=A.r+B.r;
if(sgn(d2-rdiff*rdiff)<0)
return 0;

double base=atan2(B.c.y-A.c.y,B.c.x-A.c.x);
if(sgn(d2)==0&&sgn(A.r-B.r)==0)
return -1;
if(sgn(d2-rdiff*rdiff)==0)
{
a[cnt]=A.point(base),b[cnt]=B.point(base),cnt++;
return 1;
}

double ang=acos((A.r-B.r) / sqrt(d2));
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(sgn(d2-rsum*rsum)==0)
{
a[cnt]=A.point(base);b[cnt]=B.point(base+pi);cnt++;
}
else if(sgn(d2-rsum*rsum)>0)
{
double ang=acos( (A.r+B.r)/sqrt(d2) );
a[cnt]=A.point(base+ang);b[cnt]=B.point(base+ang+pi);cnt++;
a[cnt]=A.point(base-ang);b[cnt]=B.point(base-ang+pi);cnt++;
}
return cnt;
}

//题目相关

Vector build(Vector v)
{
return v/Length(v);
}

void CircumscribedCircle()
{
Point A,B,C;
A.read();
B.read();
C.read();
Circle CC;
Vector v1,v2;
v1=Rotate(A-B,pi/2.0);
v2=Rotate(C-B,pi/2.0);
CC.c=GetLineIntersection((A+B)/2,(C+B)/2,v1,v2);
CC.r=Length(CC.c-B);
printf("(%.6lf,%.6lf,%.6lf)\n",CC.c.x,CC.c.y,CC.r);
}

void InscribedCircle()
{
Point A,B,C;
A.read();
B.read();
C.read();
Circle CC;
Vector v3=build(A-B),v4=build(C-B),v1,v2;
v1=v3+v4;
v3=build(B-A),v4=build(C-A);
v2=v3+v4;
CC.c=GetLineIntersection(B,A,v1,v2);
CC.r=Dis_Point_Line(CC.c,A,B);
printf("(%.6lf,%.6lf,%.6lf)\n",CC.c.x,CC.c.y,CC.r);
}

void TangentLineThroughPoint()
{
Circle C;
Point x;
C.read();
x.read();
Vector v[4];
int cnt=getTangents(x,C,v);
printf("[");
double ang[4];
for(int i=0;i<cnt;i++)
{
ang[i]=atan2(v[i].y,v[i].x)/pi*180.0;
while(sgn(ang[i])<0)
ang[i]+=180.0;
while(sgn(ang[i]-180.0)>=0)
ang[i]-=180.0;
}
sort(ang,ang+cnt);
for(int i=0;i<cnt;i++)
{
printf("%.6lf",ang[i]);
if(i!=cnt-1)
printf(",");
}
printf("]\n");
}

bool cmp(Circle C1,Circle C2)
{
return C1.c<C2.c;
}
void CircleThroughAPointAndTangentToALineWithRadius()
{
Point xp,x1,x2;
double r;
Circle C[4];
xp.read();x1.read();x2.read();
scanf("%lf",&r);
Line L,L1,L2;
L.v=Vector(x2-x1);
L.v=L.v/Length(L.v);
L.p=x1;
Vector vT=Normal(L.v);
L1.p=L.p+vT*r;
L2.p=L.p-vT*r;
L1.v=L2.v=L.v;
double dis1=Dis_Point_Line(xp,L1),dis2=Dis_Point_Line(xp,L2);
int cnt=0;
Point p1=GetLineProjection(xp,L1.p,L1.p+L1.v),p2=GetLineProjection(xp,L2.p,L2.p+L2.v);
if(sgn(dis1-r)==0)
C[cnt++].c=p1;
else if(sgn(dis1-r)<0)
{
double k=sqrt(r*r-dis1*dis1+eps);
C[cnt++].c=p1+L1.v*k;
C[cnt++].c=p1-L1.v*k;
}

if(sgn(dis2-r)==0)
C[cnt++].c=p2;
else if(sgn(dis2-r)<0)
{
double k=sqrt(r*r-dis2*dis2+eps);
C[cnt++].c=p2+L2.v*k;
C[cnt++].c=p2-L2.v*k;
}
sort(C,C+cnt,cmp);
printf("[");
for(int i=0;i<cnt;i++)
{
printf("(%.6lf,%.6lf)",C[i].c.x,C[i].c.y);
if(i!=cnt-1)
printf(",");
}
printf("]\n");
}

void CircleTangentToTwoLinesWithRadius()
{
Point A,B,C,D;
Point ans[4];
A.read();
B.read();
C.read();
D.read();
double r;
scanf("%lf",&r);
if(sgn(r)==0)
{
}
Point p=GetLineIntersection(A,C,A-B,C-D);
double t1=Angle(A-p,C-p);
double t2=pi-t1;
Vector v3=A-p,v4=C-p,v1,v2;
v4=v4/Length(v4),v3=v3/Length(v3);
v1=v3+v4,v2=v3-v4;
v1=v1/Length(v1),v2=v2/Length(v2);
ans[0]=p+v1*r/sin(t1/2),ans[1]=p-v1*r/sin(t1/2);
ans[2]=p+v2*r/sin(t2/2),ans[3]=p-v2*r/sin(t2/2);
sort(ans,ans+4);
printf("[");
for(int i=0;i<4;i++)
{
printf("(%.6lf,%.6lf)",ans[i].x,ans[i].y);
if(i!=3)
printf(",");
}
printf("]\n");
}

void CircleTangentToTwoDisjointCirclesWithRadius()
{
Circle C1,C2;
C1.read(),C2.read();
double r;
scanf("%lf",&r);
C1.r+=r,C2.r+=r;
vector<Point> p;
p.clear();
int cnt=getCircleCircleIntersection(C1,C2,p);
sort(p.begin(),p.end());
printf("[");
for(int i=0;i<cnt;i++)
{
printf("(%.6lf,%.6lf)",p[i].x,p[i].y);
if(i!=cnt-1)
printf(",");
}
printf("]\n");
}

int main()
{

string ss;
///freopen("in.txt","r",stdin);
while(cin>>ss)
{
if(ss=="CircumscribedCircle")
CircumscribedCircle();
if(ss=="InscribedCircle")
InscribedCircle();
if(ss=="TangentLineThroughPoint")
TangentLineThroughPoint();
if(ss=="CircleThroughAPointAndTangentToALineWithRadius")
CircleThroughAPointAndTangentToALineWithRadius();
if(ss=="CircleTangentToTwoLinesWithRadius")
CircleTangentToTwoLinesWithRadius();
if(ss=="CircleTangentToTwoDisjointCirclesWithRadius")
CircleTangentToTwoDisjointCirclesWithRadius();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: