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? 如果有的话,输出字典序最小的路径 ...
随机推荐
- jsp 获取表单值, 提交类型为multipart/form-data处理
//tt.jsp<script type="text/javascript"> function doSubmit(){ alert("aaaaaa" ...
- 李洪强iOS开发本人集成环信的经验总结_09_处理好友请求
李洪强iOS开发本人集成环信的经验总结_09_处理好友请求 实现这种效果: 01 - 遵守处理好友请求的代理协议 02 - 设置代理 03 - 实现代理方法 04 - 实现代理中用到的方法
- Servlet课程0424(三) 通过继承HttpServlet来开发Servlet
//这是第三种开发servlet的方法,通过继承httpservlet package com.tsinghua; import javax.servlet.http.*; import java.i ...
- 使用头文件climits中的符号常量获知整型数据的表数范围---gyy整理
在头文件climits(limits.h)以宏定义的方式定义了各种符号常量来表示各种整型类型表示数的范围,如int的最大最小值,long的最大最小值等. 符号常量 表示 CHAR_BIT char 的 ...
- iOS 10 使用相机及相簿闪退的问题修正
http://www.cnblogs.com/onechen/p/5935579.html
- 哈希值识别工具hash-identifier
Hash Identifier可以用来识别各种类型的哈希值.在kali上使用方法很简单 (1)搜索hash-identifier (2)在HASH后面输入要识别的hash内容 (3)识别成功 wind ...
- mysql concat和group_concat
mysql concat(str1,str2...)连接两个字符串,(数字也是可以的,会转成字符串) MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL mys ...
- PHP集成支付宝快速实现充值功能
http://blog.lixiphp.com/php-alipay-fast-chongzhi/#axzz2tOypIl4r
- poj 1426 Find The Multiple( bfs )
题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...
- poj3225 线段树区间操作 (见鬼)
细节处理实在太重要了. #include<cstdio> #include<cstring> #define MT 65533*4 #define Maxn MT*4 int ...