用c++写一个获取短边中点的函数
时间: 2023-10-10 16:15:43 浏览: 37
可以使用以下C++代码来获取短边中点:
```cpp
#include <iostream>
struct Point {
int x;
int y;
};
Point getMidpoint(Point point1, Point point2) {
Point midpoint;
if (point1.x < point2.x) {
midpoint.x = point1.x + (point2.x - point1.x) / 2;
} else {
midpoint.x = point2.x + (point1.x - point2.x) / 2;
}
if (point1.y < point2.y) {
midpoint.y = point1.y + (point2.y - point1.y) / 2;
} else {
midpoint.y = point2.y + (point1.y - point2.y) / 2;
}
return midpoint;
}
int main() {
Point point1 = {0, 0};
Point point2 = {10, 5};
Point midpoint = getMidpoint(point1, point2);
std::cout << "Midpoint: (" << midpoint.x << ", " << midpoint.y << ")" << std::endl;
return 0;
}
```
在上面的代码中,我们通过 `getMidpoint` 函数来获取两个点之间的短边中点。首先,我们定义了一个 `Point` 结构体来表示一个点的坐标。然后,在 `getMidpoint` 函数中,我们计算了两个点的 x 和 y 坐标的平均值,以找到它们之间的中点。最后,我们在 `main` 函数中测试了这个函数,并打印出了中点的坐标。