UVa 10674 (求两圆公切线) Tangents
题意:
给出两个圆的圆心坐标和半径,求这两个圆的公切线切点的坐标及对应线段长度。若两圆重合,有无数条公切线则输出-1.
输出是按照一定顺序输出的。
分析:
首先情况比较多,要一一判断,不要漏掉。
如果高中的那点老底还在的话,代码还是很好理解的。
//#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std; const double PI = acos(-1.0);
const double EPS = 1e-;
struct Point
{
double x, y;
Point(double x=, double y=):x(x), y(y) {} };
typedef Point Vector;
Vector operator + (Vector A, Vector B)
{
return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (Vector A, Vector B)
{
return Vector(A.x-B.x, A.y-B.y);
}
Vector operator * (Vector A, double p)
{
return Vector(A.x*p, A.y*p);
}
Vector operator / (Vector A, double p)
{
return Vector(A.x/p, A.y/p);
}
double dcmp(double x)
{
if(fabs(x) < EPS) return ;
else return x < ? - : ;
}
bool operator < (const Vector& a, const Vector& b)
{
return dcmp(a.x-b.x) < || dcmp(a.x-b.x) == && dcmp(a.y-b.y) < ;
}
bool operator == (const Vector& a, const Vector& b)
{
return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ;
}
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 - a.y*b.x;
}
double Length(Vector a)
{
return sqrt(Dot(a, a));
}
struct Circle
{
double x, y, r;
Circle(double x, double y, double r):x(x), y(y), r(r) {}
Point point(double a)
{
return Point(x + r*cos(a), y + r*sin(a));
}
};
int getTangents(Circle A, Circle B, Point* a, Point* b)
{
int cnt = ;
if(A.r < B.r) { swap(A, B); swap(a, b); }
double d2 = (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y);
double rdiff = A.r - B.r;
double rsum = A.r + B.r;
if(d2 < rdiff*rdiff) return ; //内含 double base = atan2(B.y-A.y, B.x-A.x);
if(dcmp(d2) == && dcmp(A.r - B.r) == ) return -; //重合
if(dcmp(d2 - rdiff*rdiff) == ) //内切
{
a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
return ;
} //有外公切线
double ang = acos((A.r - B.r) / sqrt(d2));
a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++;
a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++;
if(dcmp(rsum*rsum - d2) == )
{//外切
a[cnt] = b[cnt] = A.point(base); cnt++;
}
else if(dcmp(d2 - rsum*rsum) > )
{
ang = acos((A.r + B.r) / sqrt(d2));
a[cnt] = A.point(base + ang); b[cnt] = B.point(PI + base + ang); cnt++;
a[cnt] = A.point(base - ang); b[cnt] = B.point(PI + base - ang); cnt++;
}
return cnt;
} int main(void)
{
#ifdef LOCAL
freopen("10674in.txt", "r", stdin);
#endif int x1, y1, r1, x2, y2, r2;
while(scanf("%d%d%d%d%d%d", &x1, &y1, &r1, &x2, &y2, &r2) == && r1 && r2)
{
Point a[], b[];
Circle C1(x1, y1, r1), C2(x2, y2, r2);
int n = getTangents(C1, C2, a, b);
printf("%d\n", n);
int p[] = {, , , };
for(int i = ; i < n; ++i)
for(int j = i+; j < n; ++j)
if(a[p[j]] < a[p[i]] || (a[p[j]] == a[p[i]] && b[p[j]] < b[p[i]])) swap(p[i], p[j]);
for(int i = ; i < n; ++i)
printf("%.5lf %.5lf %.5lf %.5lf %.5lf\n", a[p[i]].x, a[p[i]].y, b[p[i]].x, b[p[i]].y, Length(a[p[i]] - b[p[i]]));
} return ;
}
代码君
写的第一份,样例过,自己随机生成的1000组数据和lrj的代码对比,输出也是一模一样的,可就是无线WA。
这里还是贴一下这份“神奇”的代码吧,也许日后能发现这个隐藏的错误。
//#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std; struct Point
{
double x, y;
Point(double x=, double y=) :x(x),y(y) {}
};
typedef Point Vector;
const double EPS = 1e-;
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b)
{ return a.x < b.x || (a.x == b.x && a.y < b.y); }
int dcmp(double x)
{ if(fabs(x) < EPS) return ; else return x < ? - : ; }
bool operator == (const Point& a, const Point& b)
{ return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
double Dot(Vector A, Vector B)
{ return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); } struct Circle
{
Point c; //Ô²ÐÄ
double r; //°ë¾¶
//Circle(Point c, double r):c(c), r(r) {}
Point point(double a)
{//Çó¶ÔÓ¦Ô²ÐĽǵĵã
return Point(c.x + r*cos(a), c.y + r*sin(a));
}
};
double angle(Vector v) { return atan2(v.y, v.x); }
const double PI = acos(-1.0); int getTangents(Circle A, Circle B, Point* a, Point* b)
{
int cnt = ;
if(A.r < B.r) { swap(A, B); swap(a, b); }
double d2 = (A.c.x-B.c.x)*(A.c.x-B.c.x) + (A.c.y-B.c.y)*(A.c.y-B.c.y);
double rdiff = A.r - B.r;
double rsum = A.r + B.r;
if(d2 < rdiff*rdiff) return ; //ÄÚº¬ double base = atan2(B.c.y-A.c.y, B.c.x-A.c.x);
if(dcmp(d2) == && dcmp(A.r-B.r) == ) return -; //Á½Ô²Öغϣ¬ÎÞÇî¶àÌõÇÐÏß
if(dcmp(d2 - rdiff*rdiff) == ) //ÄÚÇÐ
{
a[cnt] = A.point(base);
b[cnt] = B.point(base);
cnt++;
return ;
}
//ÓÐÍ⹫ÇÐÏß
double ang = acos((A.r-B.r) / sqrt(d2));
a[cnt] = A.point(base+ang); b[cnt] = B.point(base+ang); cnt++;
a[cnt] = A.point(base-ang); b[cnt] = B.point(base-ang); cnt++; if(dcmp(d2 - rsum*rsum) == ) //Ò»ÌõÄÚ¹«ÇÐÏß
{
a[cnt] = b[cnt] = A.point(base); cnt++;
}
else if(dcmp(d2 - rsum*rsum) > )
{
double ang = acos((A.r+B.r) / sqrt(d2));
a[cnt] = A.point(base+ang); b[cnt] = B.point(base+ang+PI); cnt++;
a[cnt] = A.point(base-ang); b[cnt] = B.point(base-ang+PI); cnt++;
}
//for(int i = 0; i < cnt; ++i)
// printf("%.5lf %.5lf %.5lf %.5lf\n", a[i].x, a[i].y, b[i].x, b[i].y);
return cnt;
} int main(void)
{
#ifdef LOCAL
freopen("10674in.txt", "r", stdin);
//freopen("10674out.txt", "w", stdout);
#endif int x1, y1, r1, x2, y2, r2;
while(scanf("%d%d%d%d%d%d", &x1, &y1, &r1, &x2, &y2, &r2) == && dcmp(r1) > )
{
Point a[], b[];
/*Point p1(x1, y1), p2(x2, y2);
Circle C1(p1, r1), C2(p2, r2);*/
Circle C1, C2;
C1.c.x=x1, C1.c.y=y1, C1.r=r1;
C2.c.x=x2, C2.c.y=y2, C2.r=r2;
int n = getTangents(C1, C2, a, b);
printf("%d\n", n);
if(n > )
{
int p[] = {, , , };
for(int i = ; i < n; ++i)
for(int j = i+; j < n; ++j)
if(a[p[j]] < a[p[i]] || (a[p[j]] == a[p[i]] && b[p[j]] < b[p[i]])) swap(p[i], p[j]);
for(int i = ; i < n; ++i)
printf("%.5lf %.5lf %.5lf %.5lf %.5lf\n", a[p[i]].x, a[p[i]].y, b[p[i]].x, b[p[i]].y, Length(a[p[i]] - b[p[i]]));
}
} return ;
}
WA掉的代码君
UVa 10674 (求两圆公切线) Tangents的更多相关文章
- POJ 2546 & ZOJ 1597 Circular Area(求两圆相交的面积 模板)
题目链接: POJ:http://poj.org/problem? id=2546 ZOJ:problemId=597" target="_blank">http: ...
- 求两圆相交部分面积(C++)
已知两圆圆心坐标和半径,求相交部分面积: #include <iostream> using namespace std; #include<cmath> #include&l ...
- hdu5858 Hard problem(求两圆相交面积)
题目传送门 Hard problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu 5120 (求两圆相交的面积
题意:告诉你两个圆环,求圆环相交的面积. /* gyt Live up to every day */ #include<cstdio> #include<cmath> #in ...
- Gym-101915B Ali and Wi-Fi 计算几何 求两圆交点
题面 题意:给你n个圆,每个圆有一个权值,你可以选择一个点,可以获得覆盖这个点的圆中,权值最大的m个的权值,问最多权值是多少 题解:好像是叙利亚的题....我们画画图就知道,我们要找的就是圆与圆交的那 ...
- 2D空间中求两圆的交点
出处:https://stackoverflow.com/questions/19916880/sphere-sphere-intersection-c-3d-coordinates-of-colli ...
- java求两个圆相交坐标
最近由于项目需要,根据两个圆函数求出相交的坐标.实现代码如下,另感谢两圆求交点算法实现Java代码,虽然他所贡献的代码中存在问题,但仍有借鉴意义. 1.两个圆相交的数学求法 在中学数学中我们知道,一个 ...
- ●UVA 10674 Tangents
题链: https://vjudge.net/problem/UVA-10674 题解: 计算几何,求两个圆的公切线. <算法竞赛入门经典——训练指南>P266,讲得很清楚的. 大致是分为 ...
- GPS(2)关于位置的3个示例,实时获取GPS定位数据,求两个经纬点距离,邻近某个区域圆时警告
实时获取GPS定位数据 import android.app.Activity; import android.content.Context; import android.location.Loc ...
随机推荐
- 【BZOJ】【2594】【WC2006】水管局长数据加强版
LCT 动态维护MST嘛……但是有删边= =好像没法搞的样子 离线记录所有修改&询问,倒序处理,就可以变删边为加边了- 论如何用LCT维护最小生成树:先搞出一棵最小生成树,然后每次加边(u,v ...
- nenu contest2
http://vjudge.net/vjudge/contest/view.action?cid=54562#overview H B. Polygons http://codeforces.com ...
- Guava文档翻译之ListenableFuture
ListenableFutureExplained 并发是一个困难的问题,但是使用强大而简单的抽象可以极大地简化并发问题.为了简化事情,Guava使用ListenableFuture继承了JDK的Fu ...
- React vs Angular 2: 冰与火之歌
黄玄 · 3 个月前 本文译自 Angular 2 versus React: There Will Be Blood ,其实之前有人翻译过,但是翻得水平有一点不忍直视,我们不希望浪费这篇好文章. 本 ...
- Robot framework+python安装使用图解版
一.安装包 1.Python2.7(一切的基础,切记安装目录不能有中文不能有空格) 1)python2.7:(python环境):python-2.7.msi 2)setuptools(python包 ...
- Perl 三种时间time,localtime,gmttime
#!/usr/bin/perl use warnings; use diagnostics; use strict; use POSIX; print "time: ", time ...
- lintcode:三数之和
题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...
- swift学习笔记-UI篇之UIImageView
1.基本使用 将要使用的图片拖入到项目里,我这里使用的是名为“1.jpg”的图片,然后创建UIImageView,并设置要显示的图片为"1.jpg"//1. 基本使用 let im ...
- 删除元素 不存在 NO 存在 输出余下元素
#include<stdio.h> #include<stdlib.h> #define N 5 #define NULL 0 #define OK 1 #define ERR ...
- Android:让EditText不自动获取焦点
解决方法: 在EditText的父级控件中加入属性: android:focusable="true" android:focusableInTouchMode="tru ...