Working with coordinates x y
Working with coordinates x y
我需要从文件中读取点的坐标。文件看起来像这样:
x0 y0
(x1, y1
…
然后求最小围圆的圆心和直径。但我一开始就卡住了。我不知道如何保持坐标和决定选择数组的结构。我把坐标读进了结构。我将设置4个条件:
1 -有一个点,你不可能找到最小的圆
2 -有2个点。现在的任务是找出它们与中心之间的距离。
3 -有3个点。
4 -大于3分。特殊算法
的使用我尝试使用向量。我不知道如何使用我的点(向量的元素)稍后在函数等
#include "stdafx.h"
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
// Distance
float distance(){
return sqrt((point[0].x * point[1].x) + (point[0].y * point[1].y));
}
struct Points
{
float x, y;
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<Points> point;
Points tmp;
ifstream fin("Points.txt");
if (!fin.is_open())
cout << "Cannot open the file n";
else{
while (fin >> tmp.x >> tmp.y){
point.push_back(tmp);
cout << tmp.x << tmp.y << endl;
}
fin.close();
}
return 0;
}
我会将结构体命名为Point
而不是Points
,由于结构体的单个实例只包含一对x,y坐标,
那么合适的距离函数应该是
float distance(const Point& point1, const Point& point2)
{
return sqrt((point1.x * point2.x) + (point1.y * point2.y));
}
你可以像这样得到输入集中任意两点之间的距离:
distance(point[i], point[j])
您可能还想测量从输入点到的距离不在集合中的点,如你认为是中心的点这个圆可能是。例如,
distance(point[i], candidate_center_of_circle)
如果它是我的代码,我可能会使Point
一个类,并给它一个距离的成员函数这样我就可以写成
candidate_center_of_circle.distanceTo(point[i])
顺便说一下,I 可能将变量命名为points
而不是point
因为它是一个包含多个Point
实例的向量。如果你打算写很多像point[i]
这样的东西,你可能不喜欢points[i]
,但如果您主要是在vector上使用STL迭代器然后你会得到这样的内容:
for (std::vector<Point>::const_iterator it = points.begin(); it != points.end(); ++it)
相关文章:
- Problems with std::cin.fail()
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- Boost.TEST with CLion: "Test framework quit unexpectedly"
- 避免碎片化的ClientHellos with OpenSSL (DTLS)
- Issues with Win32 ReadProcessMemory API
- Qt with WinAPI MouseProc
- [[maybe_unused]] with structured_binding?
- Issue with WriteProcessMemory
- OpenCV RTP-Stream with FFMPEG
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- std::adjacent_difference with std::chrono time_point
- DLL Made with CMake 使程序崩溃
- QtCreator with C 库中的链接器问题
- SHBrowseForFolder with BIF_BROWSEFORCOMPUTER and SHGetPathFr
- specialized std::default_delete with QQmlComponent
- VS2019 - Sudo Remote Debugging on Linux with Cmake project
- Inference pytorch C++ with alexnet and cv::imread image
- 将"Cartesian coordinates"转换为"polar coordinates with respect to user specified origin"
- Working with coordinates x y