円と円の交点を求めるには?
Posted: 2006年12月11日(月) 07:57
中心座標が異なる2 つの円の中心座標と半径を入力し,その交点の座標を求めるプログラムができません。
中心座標と半径は,struct CIRCLE {int x, y, r;};と、 2 つの円(struct CIRCLE)を引数とし,交点の数(0~2)を返す関数 int Kouten(struct CIRCLE c1, struct CIRCLE c2)を使って作りたいのですが。
私はまず片方の円を原点に移動させ、次にもう片方の円を原点を軸に回転させて、両方の円の中心をx軸上にもっていき、交点を出してそれを同様に回転、移動させて交点を得ようとしたのですが・・・・。
どうかお願いします。一応作りかけを載せときます。
#include<stdio.h>
#include<math.h>
struct CIRCLE {
int x, y, r;
};
int main(void)
{
int a,b;
double x,y,y1,a2,p;
struct CIRCLE c1,c2;
printf("Input Circle 1:\nX = ");
scanf("%d",&c1.x);
printf("Y = ");
scanf("%d",&c1.y);
printf("R = ");
scanf("%d",&c1.r);
printf("Input Circle 2:\nX = ");
scanf("%d",&c2.x);
printf("Y = ");
scanf("%d",&c2.y);
printf("R = ");
scanf("%d",&c2.r);
a = c2.x -c1.x;
b = c2.y -c1.y;
printf("a = %d,b = %d\n",a,b);
p = atan2(b,a);
a = a*cos(-p) - b*sin(-p);
b = a*sin(-p) + b*cos(-p);
printf("a = %d b = %d\n",a,b);
a2 = sqrt((c2.x-c1.x)*(c2.x-c1.x) + (c2.y-c1.y)*(c2.y-c1.y));
printf("a2 = %lf\n",a2);
x = (c1.r*c1.r - c2.r*c2.r + a*a)/(2*a);
y = sqrt(c1.r*c1.r - x*x);
printf("(x,y) = (%3.1lf,%3.1lf)\n",x,y);
y1 = -1*y;
x = x*cos(p) - y*sin(p);
y = x*sin(p) + y*cos(p);
printf("x = %lf\ny = %lf\np = %lf\n",x,y,p);
if(a2 > c1.r + c2.r){
printf("Kouten:%d\n",0);
}
else if(a2 < c1.r + c2.r){
printf("Kouten:%d\n",2);
printf("(%lf,%lf),(%lf,%lf)\n",x+c1.x,y+c1.y,x+c1.x,y1+c1.y);
}else {
printf("Kouten:%d\n",1);
printf("(%lf,%lf)\n",x+c1.x,y+c1.y);
}
return 0;
}
中心座標と半径は,struct CIRCLE {int x, y, r;};と、 2 つの円(struct CIRCLE)を引数とし,交点の数(0~2)を返す関数 int Kouten(struct CIRCLE c1, struct CIRCLE c2)を使って作りたいのですが。
私はまず片方の円を原点に移動させ、次にもう片方の円を原点を軸に回転させて、両方の円の中心をx軸上にもっていき、交点を出してそれを同様に回転、移動させて交点を得ようとしたのですが・・・・。
どうかお願いします。一応作りかけを載せときます。
#include<stdio.h>
#include<math.h>
struct CIRCLE {
int x, y, r;
};
int main(void)
{
int a,b;
double x,y,y1,a2,p;
struct CIRCLE c1,c2;
printf("Input Circle 1:\nX = ");
scanf("%d",&c1.x);
printf("Y = ");
scanf("%d",&c1.y);
printf("R = ");
scanf("%d",&c1.r);
printf("Input Circle 2:\nX = ");
scanf("%d",&c2.x);
printf("Y = ");
scanf("%d",&c2.y);
printf("R = ");
scanf("%d",&c2.r);
a = c2.x -c1.x;
b = c2.y -c1.y;
printf("a = %d,b = %d\n",a,b);
p = atan2(b,a);
a = a*cos(-p) - b*sin(-p);
b = a*sin(-p) + b*cos(-p);
printf("a = %d b = %d\n",a,b);
a2 = sqrt((c2.x-c1.x)*(c2.x-c1.x) + (c2.y-c1.y)*(c2.y-c1.y));
printf("a2 = %lf\n",a2);
x = (c1.r*c1.r - c2.r*c2.r + a*a)/(2*a);
y = sqrt(c1.r*c1.r - x*x);
printf("(x,y) = (%3.1lf,%3.1lf)\n",x,y);
y1 = -1*y;
x = x*cos(p) - y*sin(p);
y = x*sin(p) + y*cos(p);
printf("x = %lf\ny = %lf\np = %lf\n",x,y,p);
if(a2 > c1.r + c2.r){
printf("Kouten:%d\n",0);
}
else if(a2 < c1.r + c2.r){
printf("Kouten:%d\n",2);
printf("(%lf,%lf),(%lf,%lf)\n",x+c1.x,y+c1.y,x+c1.x,y1+c1.y);
}else {
printf("Kouten:%d\n",1);
printf("(%lf,%lf)\n",x+c1.x,y+c1.y);
}
return 0;
}