Working with coordinates x y

Working with coordinates x y

本文关键字:coordinates with Working      更新时间:2023-10-16

我需要从文件中读取点的坐标。文件看起来像这样:

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)