判断三维空间中两个区域是否有重叠的C++代码
时间: 2023-05-24 10:01:17 浏览: 78
以下是一种判断三维空间中两个区域是否有重叠的 C 代码:
```c
#include <stdio.h>
#include <stdbool.h>
typedef struct {
float x;
float y;
float z;
} Point;
typedef struct {
Point min;
Point max;
} Box;
bool isOverlap(Box box1, Box box2) {
if (box1.max.x < box2.min.x || box2.max.x < box1.min.x)
return false;
if (box1.max.y < box2.min.y || box2.max.y < box1.min.y)
return false;
if (box1.max.z < box2.min.z || box2.max.z < box1.min.z)
return false;
return true;
}
int main() {
Box box1 = {{0, 0, 0}, {1, 1, 1}};
Box box2 = {{0.5, 0.5, 0.5}, {2, 2, 2}};
if (isOverlap(box1, box2))
printf("The two boxes overlap.\n");
else
printf("The two boxes do not overlap.\n");
return 0;
}
```
这段代码定义了一个 Point 结构体和一个 Box 结构体,用来表示三维空间中的点和方框。函数 isOverlap 判断两个 Box 是否有重叠部分,如果有则返回 true,否则返回 false。在 main 函数中测试了两个 Box 是否重叠。