您的位置:首页 > 其它

UVA 12304 2D Geometry 110 in 1!(计算几何)

2015-04-28 10:33 357 查看
This is a collection of 110 (in binary) 2D geometry problems.CircumscribedCircle x1 y1 x2 y2 x3 y3Find out the circumscribed circle of triangle (x1,y1)-(x2,y2)-(x3,y3). These three points are guaranteed to be non-collinear. The circle is formatted as (x,y,r) where (x,y) is the center of circle, r is the radius.InscribedCircle x1 y1 x2 y2 x3 y3Find out the inscribed circle of triangle (x1,y1)-(x2,y2)-(x3,y3). These three points are guaranteed to be non-collinear. The circle is formatted as (x,y,r) where (x,y) is the center of circle, r is the radius.TangentLineThroughPoint xc yc r xp ypFind out the list of tangent lines of circle centered (xc,yc) with radius r that pass through point (xp,yp). Each tangent line is formatted as a single real number "angle" (in degrees), the angle of the line (0<=angle<180). Note that the answer should be formatted as a list (see below for details).CircleThroughAPointAndTangentToALineWithRadius xp yp x1 y1 x2 y2 rFind out the list of circles passing through point (xp, yp) that is tangent to a line (x1,y1)-(x2,y2) with radius r. Each circle is formatted as (x,y), since the radius is already given. Note that the answer should be formatted as a list. If there is no answer, you should print an empty list.CircleTangentToTwoLinesWithRadius x1 y1 x2 y2 x3 y3 x4 y4 rFind out the list of circles tangent to two non-parallel lines (x1,y1)-(x2,y2) and (x3,y3)-(x4,y4), having radius r. Each circle is formatted as (x,y), since the radius is already given. Note that the answer should be formatted as a list. If there is no answer, you should print an empty list.CircleTangentToTwoDisjointCirclesWithRadius x1 y1 r1 x2 y2 r2 rFind out the list of circles externally tangent to two disjoint circles (x1,y1,r1) and (x2,y2,r2), having radius r. By "externally" we mean it should not enclose the two given circles. Each circle is formatted as (x,y), since the radius is already given. Note that the answer should be formatted as a list. If there is no answer, you should print an empty list.For each line described above, the two endpoints will not be equal. When formatting a list of real numbers, the numbers should be sorted in increasing order; when formatting a list of (x,y) pairs, the pairs should be sorted in increasing order of x. In case of tie, smaller y comes first.

Input

There will be at most 1000 sub-problems, one in each line, formatted as above. The coordinates will be integers with absolute value not greater than 1000. The input is terminated by end of file (EOF).

Output

For each input line, print out your answer formatted as stated in the problem description. Each number in the output should be rounded to six digits after the decimal point. Note that the list should be enclosed by square brackets, and tuples should be enclosed by brackets. There should be no space characters in each line of your output.

Sample Input

CircumscribedCircle 0 0 20 1 8 17
InscribedCircle 0 0 20 1 8 17
TangentLineThroughPoint 200 200 100 40 150
TangentLineThroughPoint 200 200 100 200 100
TangentLineThroughPoint 200 200 100 270 210
CircleThroughAPointAndTangentToALineWithRadius 100 200 75 190 185 65 100
CircleThroughAPointAndTangentToALineWithRadius 75 190 75 190 185 65 100
CircleThroughAPointAndTangentToALineWithRadius 100 300 100 100 200 100 100
CircleThroughAPointAndTangentToALineWithRadius 100 300 100 100 200 100 99
CircleTangentToTwoLinesWithRadius 50 80 320 190 85 190 125 40 30
CircleTangentToTwoDisjointCirclesWithRadius 120 200 50 210 150 30 25
CircleTangentToTwoDisjointCirclesWithRadius 100 100 80 300 250 70 50

Output for the Sample Input

(9.734940,5.801205,11.332389)
(9.113006,6.107686,5.644984)
[53.977231,160.730818]
[0.000000]
[]
[(112.047575,299.271627),(199.997744,199.328253)]
[(-0.071352,123.937211),(150.071352,256.062789)]
[(100.000000,200.000000)]
[]
[(72.231286,121.451368),(87.815122,63.011983),(128.242785,144.270867),(143.826621,85.831483)]
[(157.131525,134.836744),(194.943947,202.899105)]
[(204.000000,178.000000)]
#include <iostream>#include <vector>#include <math.h>#include <iomanip>#include <string>#include <algorithm>using namespace std;#define PI acos(-1)struct Point{    double x,y;    Point(){}    Point(double x,double y):x(x),y(y){}};struct Line{  Point a,v;  Line() {}  Line(Point a,Point v):a(a), v(v) {}};struct Circle{    Point c;    double r;    Circle(){}    Circle(Point c,double r):c(c),r(r){}    Point point(double a)    {        return Point(c.x+cos(a)*r,c.y+sin(a)*r);    }};Point operator+(Point A,Point B){    return Point(A.x+B.x,A.y+B.y);}Point operator-(Point A,Point B){    return Point(A.x-B.x,A.y-B.y);}Point operator*(Point A,double p){    return Point(A.x*p,A.y*p);}Point operator/(Point A,double p){    return Point(A.x/p,A.y/p);}bool operator<(const Point&a,const Point&b){    return a.x<b.x||(a.x==b.x&&a.y<b.y);}double Dot(Point A,Point B){    return A.x*B.x+A.y*B.y;}double Length(Point A)//向量的长度{    return sqrt(Dot(A,A));}double fix(double x){  if(x <-1e-10) x=x+PI;  return x*180.0/PI;}double angle(Point v){  return atan2(v.y, v.x);}int dcmp(double x){    if(fabs(x)<1e-10)return 0;    else return x<0?-1:1;}Point Normal(Point A)//单位法向量{    double L=Length(A);    return Point(-A.y/L,A.x/L);}Point Rotate(Point A,double rad){    return Point(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}double Cross(Point A,Point B)//利用叉积求面积{    return A.x*B.y-A.y*B.x;}double DistanceToLine(Point P,Point A,Point B)//点到直线的距离{    Point v1=B-A,v2=P-A;    return fabs(Cross(v1,v2)/Length(v1));}Circle CircumscribedCircle(Point p1,Point p2,Point p3)//求三角形的外接圆,保证3点不共线{    double Bx=p2.x-p1.x,By=p2.y-p1.y;//向量p1p2    double Cx=p3.x-p1.x,Cy=p3.y-p1.y;//向量p1p3    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(p1-p));//圆心,点到点的距离}Circle InscribedCircle(Point p1,Point p2,Point p3)//求三角形的内切圆,保证3点不共线{    double a=Length(p2-p3);//求三条边的长度    double b=Length(p3-p1);    double c=Length(p1-p2);    Point p=(p1*a+p2*b+p3*c)/(a+b+c);//圆心    return Circle(p,DistanceToLine(p,p1,p2));//半径为点到直线的距离}int TangentLineThroughPoint(Circle C,Point P,double *V){    Point u=C.c-P;//向量pc    double dist=Length(u);//pc的距离    if(dist<C.r)return 0;//点在圆内无切线    else if(dcmp(dist-C.r)==0)//点在圆上    {        V[0]=fix(angle(Rotate(u,-PI/2)));//逆时针旋转PI/2        return 1;    }    else    {        double ang=asin(C.r/dist);        V[0]=fix(angle(Rotate(u,-ang)));//顺时针转        V[1]=fix(angle(Rotate(u,+ang)));//逆时针转        sort(V,V+2);        return 2;    }}void cirLineIntersect(Circle C,Line L,vector<Point> &sol){    double e=Dot(L.v,L.v);    double f=Dot(L.a-C.c,L.v)*2;    double g=Dot(L.a-C.c,L.a-C.c)-C.r*C.r;    double dlt=f*f-4*e*g;    if(dlt<-1e-6)return;    if(dlt<1e-6)    {        double t=-f/(2*e);        sol.push_back(L.v*t+L.a);        return;    }    dlt=sqrt(dlt);    double t1=(-f+dlt)/(2*e),t2=(-f-dlt)/(2*e);    sol.push_back(L.v*t1+L.a);    sol.push_back(L.v*t2+L.a);}void CircleThroughAPointAndTangentToALineWithRadius(Point P,Line lin,double r){    vector <Point> sol;    Point v=Normal(lin.v);    cirLineIntersect(Circle(P,r),Line(lin.a+v*r,lin.v),sol);    cirLineIntersect(Circle(P,r),Line(lin.a-v*r,lin.v),sol);    sort(sol.begin(),sol.end());    cout<<"[";    for(int i = 0; i < sol.size(); i++)    {        cout<<"("<<sol[i].x<<","<<sol[i].y<<")";        if(i != sol.size()-1)          cout<<",";    }    cout<<"]"<<endl;}void lineLineIntersect(Line L1, Line L2, vector <Point> &sol){    double t = Cross((L2.a-L1.a), L2.v) / Cross(L1.v, L2.v);    sol.push_back(L1.a+L1.v*t);}void CircleTangentToTwoLinesWithRadius(Line L1, Line L2, double r){    vector<Point> sol;    Point v1=Normal(L1.v);    Point v2=Normal(L2.v);    lineLineIntersect(Line(L1.a+v1*r, L1.v), Line(L2.a+v2*r, L2.v), sol);    lineLineIntersect(Line(L1.a+v1*r, L1.v), Line(L2.a-v2*r, L2.v), sol);    lineLineIntersect(Line(L1.a-v1*r, L1.v), Line(L2.a+v2*r, L2.v), sol);    lineLineIntersect(Line(L1.a-v1*r, L1.v), Line(L2.a-v2*r, L2.v), sol);    sort(sol.begin(), sol.end());    cout<<"[";    for(int i = 0; i < sol.size(); i++)    {        cout<<"("<<sol[i].x<<","<<sol[i].y<<")";        if(i != sol.size()-1)          cout<<",";    }    cout<<"]"<<endl;}void cirCirIntersect(Circle C1, Circle C2, vector <Point> &sol){    Point v = C2.c - C1.c;    double d =Length(v);    if(dcmp(d-C1.r-C2.r) > 0) return;    double ct =angle(v), alf = acos((C1.r*C1.r+d*d-C2.r*C2.r)/(2*C1.r*d));    sol.push_back(C1.point(ct+alf));    if(dcmp(alf) > 1e-10)sol.push_back(C1.point(ct-alf));}void CircleTangentToTwoDisjointCirclesWithRadius(Circle C1, Circle C2, double r){    vector <Point> sol;    cirCirIntersect(Circle(C1.c, C1.r+r), Circle(C2.c, C2.r+r), sol);    sort(sol.begin(), sol.end());    cout<<"[";    for(int i = 0; i < sol.size(); i++)    {        cout<<"("<<sol[i].x<<","<<sol[i].y<<")";        if(i != sol.size()-1)            cout<<",";    }    cout<<"]"<<endl;}int main(){    string op;    while(cin>>op)    {        cout<<setiosflags(ios::fixed)<<setprecision(6);        if(op=="CircumscribedCircle")        {            Point p1,p2,p3;            cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y;            Circle result=CircumscribedCircle(p1,p2,p3);            cout<<"("<<result.c.x<<","<<result.c.y<<","<<result.r<<")"<<endl;        }        if(op=="InscribedCircle")        {            Point p1,p2,p3;            cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y;            Circle result=InscribedCircle(p1,p2,p3);            cout<<"("<<result.c.x<<","<<result.c.y<<","<<result.r<<")"<<endl;        }        if(op=="TangentLineThroughPoint")        {            Circle C;Point P;            cin>>C.c.x>>C.c.y>>C.r>>P.x>>P.y;            double V[2];            int count=TangentLineThroughPoint(C,P,V);            if(count==0)                cout<<"[]"<<endl;            else if(count==1)                cout<<"["<<V[0]<<"]"<<endl;            else                cout<<"["<<V[0]<<","<<V[1]<<"]"<<endl;        }        if(op=="CircleThroughAPointAndTangentToALineWithRadius")        {            Point P;            double r,x1,y1,x2,y2;            cin>>P.x>>P.y>>x1>>y1>>x2>>y2>>r;            Line lin=Line(Point(x1,y1),Point(x2-x1,y2-y1));            CircleThroughAPointAndTangentToALineWithRadius(P,lin,r);        }        if(op=="CircleTangentToTwoLinesWithRadius")        {            double x1,y1,x2,y2,x3,y3,x4,y4,r;            cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4>>r;            Line L1=Line(Point(x1,y1),Point(x2-x1,y2-y1));            Line L2=Line(Point(x3,y3),Point(x4-x3,y4-y3));            CircleTangentToTwoLinesWithRadius(L1,L2,r);        }        if(op=="CircleTangentToTwoDisjointCirclesWithRadius")        {            Circle C1,C2;            double r;            cin>>C1.c.x>>C1.c.y>>C1.r>>C2.c.x>>C2.c.y>>C2.r>>r;            CircleTangentToTwoDisjointCirclesWithRadius(C1, C2, r);        }    }    return 0;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: