uva 12304

题意:要求解答6个关于圆的问题。
1.给出三角形坐标求外接圆
2.给出三角形坐标求内切圆
3.给出一个圆心和半径已知的圆,求过点(x,y)的所有和这个圆相切的直线
4.求所有和已知直线相切的过定点(x,y)的已知半径的圆的圆心
5.给出两个不平行的直线,求所有半径为r的同时和这两个直线相切的圆
6.给定两个相离的圆,求出所有和这两个圆外切的半径为r的圆。

比较恶心的计算几何模板题,直接模板吧。。。。

 #include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<memory.h>
#include<cstdlib>
#include<vector>
#define clc(a,b) memset(a,b,sizeof(a))
#define LL long long int
#define up(i,x,y) for(i=x;i<=y;i++)
#define w(a) while(a)
using namespace std;
const int inf=0x3f3f3f3f;
const int N = ;
const int maxn = ;
const double eps = 1e-; //调到1e-6以上第4问就可以用delta判断切线,但《训练指南》建议,尽量不要调eps
const double pi = acos(-); char type[maxn]; int dcmp(double x)
{
return fabs(x) < eps ? : (x > ? : -);
} struct Point
{
double x;
double y; Point(double x = , double y = ):x(x), y(y) {} bool operator < (const Point& e) const
{
return dcmp(x - e.x) < || (dcmp(x - e.x) == && dcmp(y - e.y) < );
} bool operator == (const Point& e) const
{
return dcmp(x - e.x) == && dcmp(y - e.y) == ;
} int read()
{
return scanf("%lf%lf", &x, &y);
}
} p[]; typedef Point Vector; Vector operator + (Point A, Point 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 * (Point A, double p)
{
return Vector(A.x * p, A.y * p);
} Vector operator / (Point A, double p)
{
return Vector(A.x / p, A.y / p);
} struct Line
{
Point p;
Point v; Line() {}
Line(Point p, Point v):p(p), v(v) {} int read()
{
return scanf("%lf%lf%lf%lf", &p.x, &p.y, &v.x, &v.y);
} Point point(double t)
{
return p + v * t;
}
}; struct Circle
{
Point c;
double r; Circle() {}
Circle(Point c, double r):c(c), r(r) {} int read()
{
return scanf("%lf%lf%lf", &c.x, &c.y, &r);
} Point point(double a)
{
return Point(c.x + r * cos(a), c.y + r * sin(a));
}
}; double Dot(Vector A, Vector B)
{
return A.x * B.x + A.y * B.y;
} double Cross(Vector A, Vector B)
{
return A.x * B.y - B.x * A.y;
} double Length(Vector A)
{
return sqrt(Dot(A, 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);
} double DistanceToLine(Point P, Point A, Point B) //点到直线的距离
{
Vector v1 = B - A;
Vector v2 = P - A;
return fabs(Cross(v1, v2) / Length(v1));
} double angle(Vector v) //求向量的极角
{
return atan2(v.y, v.x);
} Point GetLineIntersection(Line l1, Line l2) //求两直线的交点(前提:相交)
{
Vector u = l1.p - l2.p;
double t = Cross(l2.v, u) / Cross(l1.v, l2.v);
return l1.point(t);
} int getLineCircleIntersection(Line l, Circle C, double& t1, double& t2, vector<Point>& sol) //求直线与圆的交点
{
double a = l.v.x;
double b = l.p.x - C.c.x;
double c = l.v.y;
double d = l.p.y - C.c.y;
double e = a * a + c * c;
double f = * (a * b + c * d);
double g = b * b + d * d - C.r * C.r;
double delta = f * f - * e * g;
double dist = DistanceToLine(C.c, l.p, l.p+l.v);
if(dcmp(dist - C.r) == ) //相切,此处需特殊判断,不能用delta
{
t1 = t2 = -f / ( * e);
sol.push_back(l.point(t1));
return ;
}
if(dcmp(delta) < ) return ; //相离
else //相交
{
t1 = (-f - sqrt(delta)) / ( * e);
sol.push_back(l.point(t1));
t2 = (-f + sqrt(delta)) / ( * e);
sol.push_back(l.point(t2));
return ;
}
} int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol) //求圆与圆的交点
{
double d = Length(C1.c - C2.c);
if(dcmp(d) == )
{
if(dcmp(C1.r - C2.r) == ) return -; //两圆重合
return ; //同心圆但不重合
}
if(dcmp(C1.r + C2.r - d) < ) return ; //外离
if(dcmp(fabs(C1.r - C2.r) - d) > ) return ; //内含
double a = angle(C2.c - C1.c);
double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / ( * C1.r * d));
Point p1 = C1.point(a + da);
Point p2 = C1.point(a - da);
sol.push_back(p1);
if(p1 == p2) return ; //外切
sol.push_back(p2);
return ;
} 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 = * (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(cx, cy);
return Circle(p, Length(p1-p));
} Circle InscribedCircle(Point p1, Point p2, Point p3) //求三角形的内切圆
{
double a = Length(p3 - p2);
double b = Length(p3 - p1);
double c = Length(p2 - p1);
Point p = (p1 * a + p2 * b + p3 * c) / (a + b + c);
return Circle(p, DistanceToLine(p, p2, p3));
} int TangentLineThroughPoint(Point p, Circle C, Vector *v) //求点到圆的直线
{
Vector u = C.c - p;
double dist = Length(u);
if(dcmp(dist - C.r) < ) return ;
else if(dcmp(dist - C.r) < eps)
{
v[] = Rotate(u, pi / );
return ;
}
else
{
double ang = asin(C.r / dist);
v[] = Rotate(u, ang);
v[] = Rotate(u, -ang);
return ;
}
} void CircleThroughAPointAndTangentToALineWithRadius(Point p, Point p1, Point p2, double r)
{
Vector AB = p2 - p1;
Vector change1 = Rotate(AB, pi / ) / Length(AB) * r;
Vector change2 = Rotate(AB, -pi / ) / Length(AB) * r;
Line l1(p1 + change1, AB);
Line l2(p1 + change2, AB);
vector<Point> sol;
sol.clear();
double t1, t2;
int cnt1 = getLineCircleIntersection(l1, Circle(p, r), t1, t2, sol);
int cnt2 = getLineCircleIntersection(l2, Circle(p, r), t1, t2, sol);
int cnt = cnt1 + cnt2;
if(cnt) sort(sol.begin(), sol.end());
printf("[");
for(int i = ; i < cnt; i++)
{
printf("(%.6f,%.6f)", sol[i].x, sol[i].y);
if(cnt == && !i) printf(",");
}
puts("]");
} void CircleTangentToTwoLinesWithRadius(Point A, Point B, Point C, Point D, double r)
{
Vector AB = B - A;
Vector change = Normal(AB) * r;
Point newA1 = A + change;
Point newA2 = A - change;
Vector CD = D - C;
Vector update = Normal(CD) * r;
Point newC1 = C + update;
Point newC2 = C - update;
Point p[];
p[] = GetLineIntersection(Line(newA1, AB), Line(newC1, CD));
p[] = GetLineIntersection(Line(newA1, AB), Line(newC2, CD));
p[] = GetLineIntersection(Line(newA2, AB), Line(newC1, CD));
p[] = GetLineIntersection(Line(newA2, AB), Line(newC2, CD));
sort(p, p + );
printf("[");
printf("(%.6f,%.6f)", p[].x, p[].y);
for(int i = ; i < ; i++)
{
printf(",(%.6f,%.6f)", p[i].x, p[i].y);
}
puts("]");
} void CircleTangentToTwoDisjointCirclesWithRadius(Circle C1, Circle C2, double r)
{
Vector CC = C2.c - C1.c;
double rdist = Length(CC);
if(dcmp( * r - rdist + C1.r + C2.r) < ) puts("[]");
else if(dcmp( * r - rdist + C1.r + C2.r) == )
{
double ang = angle(CC);
Point A = C1.point(ang);
Point B = C2.point(ang + pi);
Point ret = (A + B) / ;
printf("[(%.6f,%.6f)]\n", ret.x, ret.y);
}
else
{
Circle A = Circle(C1.c, C1.r + r);
Circle B = Circle(C2.c, C2.r + r);
vector<Point> sol;
sol.clear();
GetCircleCircleIntersection(A, B, sol);
sort(sol.begin(), sol.end());
printf("[(%.6f,%.6f),(%.6f,%.6f)]\n", sol[].x, sol[].y, sol[].x, sol[].y);
}
} int main()
{
while(scanf("%s", type) == )
{
if(strcmp(type, "CircumscribedCircle") == )
{
Point p1, p2, p3;
p1.read();
p2.read();
p3.read();
Circle ret = CircumscribedCircle(p1, p2, p3);
printf("(%f,%f,%f)\n", ret.c.x, ret.c.y, ret.r);
}
else if(strcmp(type, "InscribedCircle") == )
{
Point p1, p2, p3;
p1.read();
p2.read();
p3.read();
Circle ret = InscribedCircle(p1, p2, p3);
printf("(%f,%f,%f)\n", ret.c.x, ret.c.y, ret.r);
}
else if(strcmp(type, "TangentLineThroughPoint") == )
{
Circle C;
Point p;
C.read();
p.read();
Vector v[];
int cnt = TangentLineThroughPoint(p, C, v);
double ret[];
for(int i = ; i < cnt; i++)
{
ret[i] = angle(v[i]);
if(dcmp(ret[i] - pi) == ) ret[i] = ;
if(dcmp(ret[i]) < ) ret[i] += pi;
}
sort(ret, ret + cnt);
printf("[");
for(int i = ; i < cnt; i++)
{
printf("%.6f", ret[i] / pi * );
if(cnt == && !i) printf(",");
}
puts("]");
}
else if(strcmp(type, "CircleThroughAPointAndTangentToALineWithRadius") == )
{
Point p, p1, p2;
double r;
p.read();
p1.read();
p2.read();
scanf("%lf", &r);
CircleThroughAPointAndTangentToALineWithRadius(p, p1, p2, r);
}
else if(strcmp(type, "CircleTangentToTwoLinesWithRadius") == )
{
Point A, B, C, D;
double r;
A.read();
B.read();
C.read();
D.read();
scanf("%lf", &r);
CircleTangentToTwoLinesWithRadius(A, B, C, D, r);
}
else
{
Circle C1, C2;
double r;
C1.read();
C2.read();
scanf("%lf", &r);
CircleTangentToTwoDisjointCirclesWithRadius(C1, C2, r);
}
}
return ;
}
上一篇:W5500EVB UDP模式的测试与理解-新华龙电子


下一篇:[2017BUAA软工]结对项目-数独程序扩展