有4个圆塔,圆心分别为(2,2)、(-2,2)、(-2,-2)、(2,一2),圆半径为 1,见图 4.17。这 4 个塔的高度为10m,塔以外无建筑物。今输人任一点的坐标,求该点的建筑高度(塔外的高度为零)。c语言
时间: 2024-06-13 21:05:51 浏览: 111
以下是C语言的代码实现,用于计算任意点的建筑高度:
```c
#include <stdio.h>
#include <math.h>
#define TOWER_HEIGHT 10
#define TOWER_RADIUS 1
struct Point {
double x;
double y;
};
double distance(struct Point p1, struct Point p2) {
return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}
double towerHeight(struct Point p) {
struct Point towers[4] = {{2, 2}, {-2, 2}, {-2, -2}, {2, -2}};
double max_height = 0;
for (int i = 0; i < 4; i++) {
double d = distance(p, towers[i]);
if (d <= TOWER_RADIUS && TOWER_HEIGHT > max_height) {
max_height = TOWER_HEIGHT;
}
}
return max_height;
}
int main() {
struct Point p = {1, 1};
double height = towerHeight(p);
printf("The height of the building at point (%.2f, %.2f) is %.2f meters.\n", p.x, p.y, height);
return 0;
}
```
阅读全文