请你用c语言实现编程,有4个圆塔。圆心分别为(2,2),(-2,2),(-2,-2),(2,-2),圆半径为1,这4个塔的高度为10, 塔以外无建筑物,今输入任意一点的坐标,求该点的建筑高度(塔外高度为0)
时间: 2023-05-28 20:02:51 浏览: 219
#include <stdio.h>
#include <math.h>
#define TOWER_NUM 4 // 圆塔个数
#define TOWER_R 1 // 圆塔半径
#define TOWER_H 10 // 圆塔高度
// 定义圆塔的结构体
typedef struct {
double x; // 圆心横坐标
double y; // 圆心纵坐标
} Tower;
// 计算点和圆心的距离
double distance(double x1, double y1, double x2, double y2) {
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
int main() {
// 定义4个圆塔
Tower towers[TOWER_NUM] = {{2, 2}, {-2, 2}, {-2, -2}, {2, -2}};
// 输入点的坐标
double x, y;
printf("请输入点的横坐标和纵坐标,以空格分隔:");
scanf("%lf %lf", &x, &y);
// 计算每个圆塔与点的距离
double d[TOWER_NUM];
for (int i = 0; i < TOWER_NUM; i++) {
d[i] = distance(x, y, towers[i].x, towers[i].y);
}
// 判断点在哪座圆塔上
int tower_index = -1;
for (int i = 0; i < TOWER_NUM; i++) {
if (d[i] <= TOWER_R) {
tower_index = i;
break;
}
}
// 根据不同情况计算建筑高度
double height;
if (tower_index == -1) { // 点在塔外
height = 0;
} else { // 点在某座圆塔上
height = TOWER_H * (1 - d[tower_index] / TOWER_R);
}
printf("该点的建筑高度为:%lf\n", height);
return 0;
}
阅读全文