繁体   English   中英

使用坐标xy

[英]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])

顺便说一句,我可能将变量命名为points而不是point因为它是一个包含Point多个实例的向量。 如果您打算编写很多类似point[i]的东西,那么您可能不喜欢points[i] ,但是如果您主要是要在向量上进行STL迭代器,那么您将得到以下内容:

for (std::vector<Point>::const_iterator it = points.begin(); it != points.end(); ++it)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM