UVA 12304(计算几何大综合题)

D - 2D Geometry 110 in 1!
Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit  Status  Practice  UVA 12304
Appoint description:  System Crawler  (2014-10-14)

Description

Download as PDF

Problem E

2D Geometry 110 in 1!

This is a collection of 110 (in binary) 2D geometry problems.

CircumscribedCircle x1 y1 x2 y2 x3 y3

Find 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 y3

Find 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 yp

Find 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 r

Find 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 r

Find 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 r

Find 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)]




写完就不会爱了。1万多b的代码,幸好过了。

挺开心的,计算几何模版正在成立中= =

题目来自大白书计算几何部分。

//Hello. I'm Peter.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"< b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define eps 1e-6
#define MOD 1000000007
#define MAXN
#define N
#define M 10100
const double pi=acos(-1.0);
string row;
double r,rad;
int dcmp(double x){//判断x正负或为0
    if(fabs(x)&poi){//求一条线和一个圆的交点。三种情况
    double a1=v.x,b1=p.x-c.c.x,c1=v.y,d1=p.y-c.c.y;
    double e1=sq(a1)+sq(c1),f1=2*(a1*b1+c1*d1),g1=sq(b1)+sq(d1)-sq(c.r);
    double delta=sq(f1)-4*e1*g1,t;
    if(dcmp(delta)<0) return 0;
    else if(dcmp(delta)==0){
        t=(-f1)/(2*e1);poi.pb(p+v*t);
        return 1;
    }
    else{
        t=(-f1+sqrt(delta))/(2*e1);poi.pb(p+v*t);
        t=(-f1-sqrt(delta))/(2*e1);poi.pb(p+v*t);
        return 2;
    }
}
int LineTangentCircle(Point p,Circle c,vector&vec){//求一个点与一个圆的切线。三种情况
    Vector u=c.c-p;
    double l=Length(u);
    if(dcmp(l-c.r)<0) return 0;//点在圆内
    else if(dcmp(l-c.r)==0){//点在圆上
        vec.pb(Rotate(u,pi/2));
        return 1;
    }
    else{//点在圆外
        double rad=asin(c.r/l);
        vec.pb(Rotate(u,rad));
        vec.pb(Rotate(u,-rad));
        return 2;
    }
}
Point Centre_CircleTangentTwoNonParallelLineWithRadius(Point p1,Vector v1,Point p2,Vector v2,double r){
    //给定两个向量,求两向量方向内夹着的圆的圆心。圆与两线均相切,圆的半径已给定
    Point p0=LineIntersect(p1,v1,p2,v2);
    Vector u=AngleBisector(p0,v1,v2);
    double rad=0.5*Angle(v1,v2);
    double l=r/sin(rad);
    double t=l/Length(u);
    return p0+u*t;
}
int FourCircleTangentTwoNonParallelLineWithRadius(Point p1,Vector v1,Point p2,Vector v2,double r,vector&cir){
    //求与两条不平行的直线都相切的4个圆,圆的半径已给定
    Point resp;
    resp=Centre_CircleTangentTwoNonParallelLineWithRadius(p1,v1,p2,v2,r);cir.pb(Circle(resp,r));
    resp=Centre_CircleTangentTwoNonParallelLineWithRadius(p1,-1*v1,p2,v2,r);cir.pb(Circle(resp,r));
    resp=Centre_CircleTangentTwoNonParallelLineWithRadius(p1,v1,p2,-1*v2,r);cir.pb(Circle(resp,r));
    resp=Centre_CircleTangentTwoNonParallelLineWithRadius(p1,-1*v1,p2,-1*v2,r);cir.pb(Circle(resp,r));
    return 4;
}
int CircleThroughAPointAndTangentALineWithRadius(Point p1,Point p2,Vector w,double r,vector&cir){
    //求经过p1并且与一条直线相切的一组圆
    Circle c1=Circle(p1,r);
    double t=r/Length(w);
    Vector u=Vector(-w.y,w.x);
    Point p4=p2+u*t;
    vectorpoi;poi.clear();
    int num=LineIntersectCircle(p4,w,c1,poi);
    u=Vector(w.y,-w.x);
    p4=p2+u*t;
    num+=LineIntersectCircle(p4,w,c1,poi);
    rep(i,0,num){
        cir.pb(Circle(poi[i],r));
    }
    return num;
}
int CircleExternallyTangentTwoDisjointCirclesWithRadius(Circle c1,Circle c2,double r,vector&cir){
    //求与两个相离的圆均外切的一组圆,三种情况
    double dis1=c1.r+r+r+c2.r,dis2=DistancePointToPoint(c1.c,c2.c);
    if(dcmp(dis1-dis2)<0) return 0;
    Vector u=c2.c-c1.c;
    double t=(r+c1.r)/Length(u);
    if(dcmp(dis1-dis2)==0){
        Point p0=c1.c+u*t;
        cir.pb(Circle(p0,r));
        return 1;
    }
    double aa=DistancePointToPoint(c1.c,c2.c),bb=r+c1.r,cc=r+c2.r;
    double rad=acos((sq(aa)+sq(bb)-sq(cc))/(2*aa*bb));
    Vector w=Rotate(u,rad);
    Point p0=c1.c+w*t;
    cir.pb(Circle(p0,r));
    w=Rotate(u,-rad);
    p0=c1.c+w*t;
    cir.pb(Circle(p0,r));
    return 2;
}
bool comp(const Point a,const Point b){
    if(a.x!=b.x) return a.x>row)
    {
        if(row=="CircumscribedCircle"){//给定三角形,求外切圆
            repin(i,1,3){
                readPoint(p[i]);
            }
            c1=CircumscribedCircle(p[1],p[2],p[3]);
            printf("(%.6f,%.6f,%.6f)\n",c1.c.x,c1.c.y,c1.r);
        }
        else if(row=="InscribedCircle"){//给定三角形,求内切圆
            repin(i,1,3){
                readPoint(p[i]);
            }
            c1=InscribedCircle(p[1],p[2],p[3]);
            printf("(%.6f,%.6f,%.6f)\n",c1.c.x,c1.c.y,c1.r);
        }
        else if(row=="TangentLineThroughPoint"){//给定点和圆,求切线。三种情况
            readCircle(c1);
            readPoint(p1);
            vectorvec;vec.clear();
            int num=LineTangentCircle(p1,c1,vec);
            vectorRealAngle;RealAngle.clear();
            rep(i,0,num){
                RealAngle.pb(RealAngleWithX(vec[i]));
            }
            sort(RealAngle.begin(),RealAngle.end());
            printf("[");
            rep(i,0,gsize(RealAngle)){
                if(i!=0) printf(",");
                printf("%.6f",RealAngle[i]);
            }
            printf("]\n");
        }
        else if(row=="CircleThroughAPointAndTangentToALineWithRadius"){
            readPoint(p1);
            readPoint(p2),readPoint(p3);
            scanf("%lf",&r);
            vectorcir;cir.clear();
            int num=CircleThroughAPointAndTangentALineWithRadius(p1,p2,p3-p2,r,cir);
            vectorpoi;poi.clear();
            rep(i,0,num){
                poi.pb(cir[i].c);
            }
            sort(poi.begin(),poi.end(),comp);
            printf("[");
            rep(i,0,num){
                if(i!=0) printf(",");
                printf("(%.6f,%.6f)",poi[i].x,poi[i].y);
            }
            printf("]\n");
        }
        else if(row=="CircleTangentToTwoLinesWithRadius"){
            readPoint(p1),readPoint(p2),readPoint(p3),readPoint(p4);
            scanf("%lf",&r);
            vectorcir;cir.clear();
            int num=FourCircleTangentTwoNonParallelLineWithRadius(p1,p2-p1,p3,p4-p3,r,cir);
            vectorpoi;poi.clear();
            rep(i,0,num){
                poi.pb(cir[i].c);
            }
            sort(poi.begin(),poi.end(),comp);
            printf("[");
            rep(i,0,gsize(poi)){
                if(i!=0) printf(",");
                printf("(%.6f,%.6f)",poi[i].x,poi[i].y);
            }
            printf("]\n");
        }
        else if(row=="CircleTangentToTwoDisjointCirclesWithRadius"){
            readCircle(c1),readCircle(c2);
            scanf("%lf",&r);
            vectorcir;cir.clear();
            int num=CircleExternallyTangentTwoDisjointCirclesWithRadius(c1,c2,r,cir);
            vectorpoi;poi.clear();
            rep(i,0,num){
                poi.pb(cir[i].c);
            }
            sort(poi.begin(),poi.end(),comp);
            printf("[");
            rep(i,0,num){
                if(i!=0) printf(",");
                printf("(%.6f,%.6f)",poi[i].x,poi[i].y);
            }
            printf("]\n");
        }
    }
}







你可能感兴趣的:(算法设计)