[UVA][何、交] 453 - Intersecting Circles@Morris' Blog|PChome Online 人新台
2013-03-28 16:58:58| 人859| 回0 | 上一篇 | 下一篇

[UVA][何、交] 453 - Intersecting Circles

0 收藏 0 0 站台


 Intersecting Circles 

The equation of a circle with radius r and center tex2html_wrap_inline29 is

displaymath31

Write a program that compares two circles to see if they intersect and, if they do, computes the points of intersection. (There can be 1, 2, or and infinite number of such points).

Input

The input to this program will consist of a pair number of lines. Each two lines represent a intersection problem. Each line will contain 3 real numbers constituting the tex2html_wrap_inline33 , tex2html_wrap_inline35 and r parameters for one circle.

Output

For each problem, the output should be the words "NO INTERSECTION" if the circles do not intersect.

When they have an infinite number of intersection points, the output should be the words "THE CIRCLES ARE THE SAME"

If they do intersect at 1 or 2 points, the output should be a line with one or two pairs, respectively, of real numbers giving the x and y coordinates of any point of intersection. Pairs must be sorted first by their x coordinate and when these are equal by the y coordinate.

Each pair is to be printed in parenthesis with numbers accurately rounded to three digits to the right of the decimal point, as the sample below.

Sample Input

0.0 0.0 1.0 3.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0 0.0 1.0

Sample Output

NO INTERSECTION THE CIRCLES ARE THE SAME (0.500,-0.866)(0.500,0.866)

明一下做法,

拉心,心距,然後分成可能。

如果 disAB, A.r, B.r 不成三角形相交//外或

交一的候// 切或外切
交的候,使用旋向量求交。

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct Cir {
double x, y, r;
int scan() {
return scanf("%lf %lf %lf", &x, &y, &r) == 3;
}
};
#define eps 1e-6
struct Pt {
double x, y;
bool operator<(const Pt other) const {
if(fabs(x-other.x) < eps)
return y < other.y;
return x < other.x;
}
};
int IntersectCir(Cir A, Cir B, Pt v[]) { // Pt v[] result buffer
double disAB = sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
if(fabs(disAB) < eps && fabs(A.r-B.r) < eps) {
if(A.r < eps) {
v[0].x = A.x;
v[0].y = A.y;
return 1;
}
return 0;
}
if(A.r+B.r+ps < disAB || A.r+disAB+eps < B.r || B.r+disAB+eps < A.r) {
return -1;
}
double x, y, vx, vy;
vx = B.x - A.x;
vy = B.y - A.y;
if(fabs(disAB-A.r-B.r) < eps || fabs(A.r-disAB-B.r) < eps ||
fabs(B.r-A.r-disAB) < eps) {
if(fabs(disAB-A.r-B.r) < eps) { // (A)(B)
v[0].x = A.x+vx*A.r/(A.r+B.r);
v[0].y = A.y+vy*A.r/(A.r+B.r);
} else {
if(A.r < B.r) { //((A)B)
v[0].x = A.x-vx*A.r/(B.r-A.r);
v[0].y = A.y-vy*A.r/(B.r-A.r);
} else { //((B)A)
v[0].x = B.x+vx*B.r/(A.r-B.r);
v[0].y = B.y+vy*B.r/(A.r-B.r);
}
}
return 1;
}
double theta = acos((A.r*A.r+disAB*disAB-B.r*B.r)/2/A.r/disAB);
double rvx, rvy; //rotate vector
rvx = vx*cos(theta)-vy*sin(theta);
rvy = vx*sin(theta)+vy*cos(theta);
v[0].x = A.x+rvx*A.r/disAB;
v[0].y = A.y+rvy*A.r/disAB;
rvx = vx*cos(-theta)-vy*sin(-theta);
rvy = vx*sin(-theta)+vy*cos(-theta);
v[1].x = A.x+rvx*A.r/disAB;
v[1].y = A.y+rvy*A.r/disAB;
return 2;
}
int main() {
Cir A, B;
#define eps2 5.14e-5
while(A.scan()) {
B.scan();
Pt p[2];
int r = IntersectCir(A, B, p);
if(r == 0)
puts("THE CIRCLES ARE THE SAME");
else if(r < 0)
puts("NO INTERSECTION");
else if(r == 1)
printf("(%.3lf,%.3lf)\n", p[0].x+eps2, p[0].y+eps2);
else {
sort(p, p+2);
printf("(%.3lf,%.3lf)(%.3lf,%.3lf)\n", p[0].x+eps2, p[0].y+eps2, p[1].x+eps2, p[1].y+eps2);
}
}
return 0;
}
 

台: Morris
人(859) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][并、交、周] 10969 - Sweet Dream
此分上一篇:[UVA][四分] 806 - Spatial Structures

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86