我只是想知道我在这里做错了什么?错误主要来自我的第一个函数,我称错了吗?
typedef struct{ //typedef and function prototype
int x, y, radius;
}circle;
int intersect(circle c1, circle c2);
我的功能所需的主要功能的一部分
circle c1 = {5, 6, 3.2};
circle c2 = {6, 8, 1.2};
如果两个圆参数相交,则返回1。如何正确使用struct调用数组?我一直在收到错误
int intersect(circle c1, circle c2){
float cx, cy, r, distance;
cx = (circle c1[0].x - circle c2[0].x) * (circle c1[0].x - circle c2[0].x);
cy = (circle c1[1].x - circle c2[1].x) * (circle c1[1].x - circle c2[1].x);
r = (circle c1[2].x - circle c2[2].x);
distance = sqrt(cx + cy);
if (r <= distance){
return 1;
}else{
return 0;
}
}
我正在为决赛做准备,所以将不胜感激帮助
答案 0 :(得分:2)
您的代码中没有数组,因此请勿尝试使用数组表示法。另外,不要声明与函数参数具有相同名称的局部变量。
int intersect(circle c1, circle c2)
{
float dx, dy, r, distance;
dx = (c1.x - c2.x) * (c1.x - c2.x);
dy = (c1.y - c2.y) * (c1.y - c2.y); // x changed to y throughout
r = (c1.r + c2.r); // rewritten too
distance = sqrt(cx + cy);
if (r <= distance)
return 1;
else
return 0;
}