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

D - 2D Geometry 110 in 1!
Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Appoint description: 

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<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > 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)<eps) return 0;
    else if(x<0) return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point(){};
    Point(double xx,double yy):x(xx),y(yy){};
}p1,p2,p3,p4,v1,v2,v3,v4,p[M],v[M];
typedef Point Vector;
void readPoint(Point &a){//输入一个点
    scanf("%lf %lf",&a.x,&a.y);
}
double sq(double x){//x^2
    return x*x;
}
Vector operator +(const Vector a,const Vector b){//两向量相加
    return Vector(a.x+b.x,a.y+b.y);
}
Vector operator -(const Vector a,const Vector b){//两向量相减
    return Vector(a.x-b.x,a.y-b.y);
}
double operator *(const Vector a,const Vector b){//两向量点乘
    return a.x*b.x+a.y*b.y;
}
double operator &(const Vector a,const Vector b){//两向量叉积
    return a.x*b.y-a.y*b.x;
}
Vector operator *(const Vector b,const double a){//一个数*一个向量
    return Vector(a*b.x,a*b.y);
}
Vector operator *(const double a,const Vector b){//一个向量*一个数
    return Vector(a*b.x,a*b.y);
}
Point MiddlePointToPoint(Point a,Point b){//求两点的中点
    return Point((a.x+b.x)/2,(a.y+b.y)/2);
}
double Length(Vector b){//求一个向量的长度
    return sqrt(b*b);
}
double DistancePointToPoint(Point a,Point b){//求点到点的无向距离
    Vector v=a-b;
    return Length(v);
}
double DistancePointToLine(Point p,Point a,Vector v){//求点到线的无向距离
    Vector w=p-a;
    return fabs((v&w)/Length(v));
}
Point LineIntersect(Point p1,Vector v,Point p2,Vector w){//求两线的交点,前提是两向量不共线
    Vector u=p1-p2;
    double t=(w&u)/(v&w);
    return p1+v*t;
}
double Angle(Vector a,Vector b){//求两个向量的夹角(0<=x<=pi)
    double t=(a*b)/Length(a)/Length(b);
    return acos(t);
}
double RealAngleWithX(Vector a){//求线与x轴的真实角(0<=X<180)
    Vector b(1,0);
    if(dcmp(a&b)==0) return 0.0;
    else if(dcmp(a*b)==0) return 90.0;
    double rad=Angle(a,b);
    rad=(rad/pi)*180.0;
    if(dcmp(a.y)<0) rad=180.0-rad;
    return rad;
}
Vector Rotate(Vector a,double rad){//求将向量a逆时针旋转rad角度后得到的向量
    return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
Vector AngleBisector(Point p,Vector v1,Vector v2){//给定两个向量,求角平分线
    double rad=Angle(v1,v2);
    return Rotate(v1,dcmp(v1&v2)*0.5*rad);
}
//Circle
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);
    }
}c1,c2,c[M];
void readCircle(Circle &c){//输入一个圆
    readPoint(c.c);
    scanf("%lf",&c.r);
}
Circle CircumscribedCircle(Point p1,Point p2,Point p3){//给定三角形,求外切圆
    Vector vt=p2-p1;
    Vector v1=Vector(-vt.y,vt.x);
    Point m1=MiddlePointToPoint(p1,p2);
    vt=p3-p2;
    Vector v2=Vector(-vt.y,vt.x);
    Point m2=MiddlePointToPoint(p2,p3);
    Point p4=LineIntersect(m1,v1,m2,v2);
    double r=DistancePointToPoint(p1,p4);
    Circle res;res.c=p4,res.r=r;
    return res;
}
Circle InscribedCircle(Point p1,Point p2,Point p3){//给定三角形,求内切圆
    Vector u=p2-p1,w=p3-p1;
    Vector v1=AngleBisector(p1,u,w);
    u=p1-p2,w=p3-p2;
    Vector v2=AngleBisector(p2,u,w);
    Point p4=LineIntersect(p1,v1,p2,v2);
    double r=DistancePointToLine(p4,p1,u);
    Circle res;res.c=p4,res.r=r;
    return res;
}
int LineIntersectCircle(Point p,Vector v,Circle c,vector<Point>&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<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<Circle>&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<Circle>&cir){
    //求经过p1并且与一条直线相切的一组圆
    Circle c1=Circle(p1,r);
    double t=r/Length(w);
    Vector u=Vector(-w.y,w.x);
    Point p4=p2+u*t;
    vector<Point>poi;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<Circle>&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<b.x;
    else return a.y<b.y;
}
int main()
{
    while(cin>>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);
            vector<Point>vec;vec.clear();
            int num=LineTangentCircle(p1,c1,vec);
            vector<double>RealAngle;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);
            vector<Circle>cir;cir.clear();
            int num=CircleThroughAPointAndTangentALineWithRadius(p1,p2,p3-p2,r,cir);
            vector<Point>poi;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);
            vector<Circle>cir;cir.clear();
            int num=FourCircleTangentTwoNonParallelLineWithRadius(p1,p2-p1,p3,p4-p3,r,cir);
            vector<Point>poi;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);
            vector<Circle>cir;cir.clear();
            int num=CircleExternallyTangentTwoDisjointCirclesWithRadius(c1,c2,r,cir);
            vector<Point>poi;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");
        }
    }
}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值