C++实现获取曲线的两个端点
时间: 2023-06-15 22:03:06 浏览: 77
可以通过以下步骤获取曲线的两个端点:
1. 首先,遍历曲线上的所有点,找到最左边的点和最右边的点。
2. 然后,比较所有点的纵坐标,找到最上面的点和最下面的点。
3. 最后,最左边的点和最右边的点就是曲线的两个端点。
以下是一个示例代码,假设曲线上的点存储在一个名为points的数组中:
```c++
#include <iostream>
#include <vector>
struct Point {
double x;
double y;
};
int main() {
std::vector<Point> points = {{1, 2}, {3, 4}, {5, 6}, {0, 3}, {7, 8}};
// 初始化最左边和最右边的点为第一个点
Point leftmost = points[0];
Point rightmost = points[0];
// 遍历所有点,找到最左边和最右边的点
for (const auto& p : points) {
if (p.x < leftmost.x) {
leftmost = p;
}
if (p.x > rightmost.x) {
rightmost = p;
}
}
// 初始化最上面和最下面的点为第一个点
Point topmost = points[0];
Point bottommost = points[0];
// 比较所有点的纵坐标,找到最上面和最下面的点
for (const auto& p : points) {
if (p.y < topmost.y) {
topmost = p;
}
if (p.y > bottommost.y) {
bottommost = p;
}
}
std::cout << "Leftmost point: (" << leftmost.x << ", " << leftmost.y << ")" << std::endl;
std::cout << "Rightmost point: (" << rightmost.x << ", " << rightmost.y << ")" << std::endl;
std::cout << "Topmost point: (" << topmost.x << ", " << topmost.y << ")" << std::endl;
std::cout << "Bottommost point: (" << bottommost.x << ", " << bottommost.y << ")" << std::endl;
return 0;
}
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)