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 ;
}
uva 12304的更多相关文章
- Uva 12304 - 2D Geometry 110 in 1!
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 12304 (6个二维几何问题合集) 2D Geometry 110 in 1!
这个题能1A纯属运气,要是WA掉,可真不知道该怎么去调了. 题意: 这是完全独立的6个子问题.代码中是根据字符串的长度来区分问题编号的. 给出三角形三点坐标,求外接圆圆心和半径. 给出三角形三点坐标, ...
- UVA 12304 - 2D Geometry 110 in 1! - [平面几何基础题大集合][计算几何模板]
题目链接:https://cn.vjudge.net/problem/UVA-12304 题意: 作为题目大合集,有以下一些要求: ①给出三角形三个点,求三角形外接圆,求外接圆的圆心和半径. ②给出三 ...
- uva 12304点与直线与圆之间的关系
Problem E 2D Geometry 110 in 1! This is a collection of 110 (in binary) 2D geometry problems. Circum ...
- UVA 12304 /// 圆的综合题 圆的模板
题目大意: ①给出三角形三个点,求三角形外接圆,求外接圆的圆心和半径. ②给出三角形三个点,求三角形内接圆,求内接圆的圆心和半径. ③给出一个圆,和一个点,求过该点的圆的切线与x轴的夹角(0<= ...
- .Uva&LA部分题目代码
1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...
- UVA-12304 2D Geometry 110 in 1! (有关圆的基本操作)
UVA-12304 2D Geometry 110 in 1! 该问题包含以下几个子问题 CircumscribedCircle x1 y1 x2 y2 x3 y3 : 三角形外接圆 Inscribe ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
随机推荐
- BZOJ 3160 万径人踪灭 解题报告
这个题感觉很神呀.将 FFT 和 Manacher 有机结合在了一起. 首先我们不管那个 “不能连续” 的条件,那么我们就可以求出有多少对字母关于某一条直线对称,然后记 $T_i$ 为关于直线 $i$ ...
- The 5th Zhejiang Provincial Collegiate Programming Contest---ProblemE:Easy Task
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2969 全场第一水题.我不知道怎么解释,看代码就好了... #include ...
- oracle中int类型和number类型区别
INT类型是NUMBER类型的子类型.下面简要说明:(1)NUMBER(P,S)该数据类型用于定义数字类型的数据,其中P表示数字的总位数(最大字节个数),而S则表示小数点后面的位数.假设定义SAL列为 ...
- Samza文档翻译 : Concepts
此页介绍啊Samza的一些高层级概念. Streams Samza处理Streams(流).流由同一类型的不可变的消息组成.例如,一个流可以是对一个网站的所有点击,或者对一个数据库表的所有更新,或者一 ...
- Connect to the mysql dataase from remote server
Make sure that the firewall is closed!!!!!!!!! shell command should be like is: mysql -u username -p ...
- 【mysql的设计与优化专题(4)】表的垂直拆分和水平拆分
垂直拆分 垂直拆分是指数据表列的拆分,把一张列比较多的表拆分为多张表 通常我们按以下原则进行垂直拆分: 把不常用的字段单独放在一张表; 把text,blob等大字段拆分出来放在附表中; 经常组合查询的 ...
- Android:再按一次退出程序
感觉这种效果比较友好 //两秒内按返回键两次退出程序 private long exitTime = 0; @Override public boolean onKeyDown(int keyCode ...
- Tomcat 部署Undeployment Failure
Tomcat 部署Undeployment Failure - yongjava的日志 - 网易博客 http://blog.163.com/qiangyongbin2000@126/blog/sta ...
- (转)Decision Tree
Decision Tree:Analysis 大家有没有玩过猜猜看(Twenty Questions)的游戏?我在心里想一件物体,你可以用一些问题来确定我心里想的这个物体:如是不是植物?是否会飞?能游 ...
- java开发之IO流
一直对IO流记不清楚,从别的地方转过来. 看下图: 流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两 ...