#include<stdio.h>
#include<math.h>
struct point_t {
double x, y;
};
struct circle_t {
struct point_t center;
double r;
};
int double_equals(double const a, double const b)
{
static const double ZERO = 1e-15;
return fabs(a - b) < ZERO;
}
double distance_sqr(struct point_t const* a, struct point_t const* b)
{
return (a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y);
}
double distance(struct point_t const* a, struct point_t const* b)
{
return sqrt(distance_sqr(a, b));
}
int insect(struct circle_t circles[], struct point_t points[])
{
double d, a, b, c, p, q, r;
double cos_value[2], sin_value[2];
if (double_equals(circles[0].center.x, circles[1].center.x)
&& double_equals(circles[0].center.y, circles[1].center.y)
&& double_equals(circles[0].r, circles[1].r)) {
return -1;
}
d = distance(&circles[0].center, &circles[1].center);
if (d > circles[0].r + circles[1].r
|| d < fabs(circles[0].r - circles[1].r)) {
return 0;
}
a = 2.0 * circles[0].r * (circles[0].center.x - circles[1].center.x);
b = 2.0 * circles[0].r * (circles[0].center.y - circles[1].center.y);
c = circles[1].r * circles[1].r - circles[0].r * circles[0].r
- distance_sqr(&circles[0].center, &circles[1].center);
p = a * a + b * b;
q = -2.0 * a * c;
if (double_equals(d, circles[0].r + circles[1].r)
|| double_equals(d, fabs(circles[0].r - circles[1].r))) {
cos_value[0] = -q / p / 2.0;
sin_value[0] = sqrt(1 - cos_value[0] * cos_value[0]);
points[0].x = circles[0].r * cos_value[0] + circles[0].center.x;
points[0].y = circles[0].r * sin_value[0] + circles[0].center.y;
if (!double_equals(distance_sqr(&points[0], &circles[1].center),
circles[1].r * circles[1].r)) {
points[0].y = circles[0].center.y - circles[0].r * sin_value[0];
}
return 1;
}
r = c * c - b * b;
cos_value[0] = (sqrt(q * q - 4.0 * p * r) - q) / p / 2.0;
cos_value[1] = (-sqrt(q * q - 4.0 * p * r) - q) / p / 2.0;
sin_value[0] = sqrt(1 - cos_value[0] * cos_value[0]);
sin_value[1] = sqrt(1 - cos_value[1] * cos_value[1]);
points[0].x = circles[0].r * cos_value[0] + circles[0].center.x;
points[1].x = circles[0].r * cos_value[1] + circles[0].center.x;
points[0].y = circles[0].r * sin_value[0] + circles[0].center.y;
points[1].y = circles[0].r * sin_value[1] + circles[0].center.y;
if (!double_equals(distance_sqr(&points[0], &circles[1].center),
circles[1].r * circles[1].r)) {
points[0].y = circles[0].center.y - circles[0].r * sin_value[0];
}
if (!double_equals(distance_sqr(&points[1], &circles[1].center),
circles[1].r * circles[1].r)) {
points[1].y = circles[0].center.y - circles[0].r * sin_value[1];
}
if (double_equals(points[0].y, points[1].y)
&& double_equals(points[0].x, points[1].x)) {
if (points[0].y > 0) {
points[1].y = -points[1].y;
} else {
points[0].y = -points[0].y;
}
}
return 2;
}
int main()
{
struct circle_t circles[2];
struct point_t points[2];
printf("請輸入兩圓x,y,半徑(以逗號分開):");
circles[0].center.x=0;
circles[0].center.y=0;
circles[0].r=5;
circles[1].center.x=6; circles[1].center.y=0; circles[1].r=1;
switch (insect(circles, points))
{
case -1:
printf("THE CIRCLES ARE THE SAME/n");
break;
case 0:
printf("NO INTERSECTION/n");
break;
case 1:
printf("(%f %f)\n", points[0].x, points[0].y);
break;
case 2:
printf("(%f %f) (%f %f)\n",
points[0].x, points[0].y,
points[1].x, points[1].y);
}
return 0;
}
uj5u.com熱心網友回復:
指標訪問湊字數補丁。。。。。
uj5u.com熱心網友回復:
結果沒有開根號嗎?uj5u.com熱心網友回復:
看樣子是三圓求交點的公式。這是要無線定位么?
uj5u.com熱心網友回復:
就是數學公式而已。uj5u.com熱心網友回復:

指標而已
uj5u.com熱心網友回復:
a->b等價于
(*a).b
uj5u.com熱心網友回復:
a,b的距離的平方轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/30288.html
標籤:基礎類
上一篇:pIUnknown->QueryInterface(const IID& iid, void** ppv)中第一引數如何理解
下一篇:mfc 氣球上飄消失影片
