C++ 使用坐标 xy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30479541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:47:06  来源:igfitidea点击:

Working with coordinates x y

c++functionstructurecoordinatesgeometry

提问by Asm

I need to read from a file coordinates of points. The file looks like this:

我需要从点的文件坐标中读取。该文件如下所示:

x0 y0

x0 y0

x1 y1

x1 y1

....

....

Then find center and diameter of the smallest enclosing circle. But I stucked in the beginning. I don't know how to hold coordinates and decided to choose array of structures. I've read coordinates into structure. I'm going to make 4 conditions:

然后找到最小封闭圆的中心和直径。但我一开始就卡住了。我不知道如何保持坐标并决定选择结构数组。我已将坐标读入结构中。我要提出4个条件:

1 - There is one point and you can't find the smallest enclosing circle.

1 - 有一点,你找不到最小的封闭圆。

2 - There are 2 points. Now the task is to find distance between them and its center.

2 - 有 2 分。现在的任务是找到它们与其中心之间的距离。

3 - There are 3 points.

3 - 有 3 分。

4 - More than 3 points. Use of special algorithm

4 - 超过 3 分。特殊算法的使用

I tried to use vector. I don't know how to use my points (elements of vector) later in functions etc.

我尝试使用矢量。我不知道以后如何在函数等中使用我的点(向量元素)。

#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;
}

回答by David K

I would name the struct something like Pointrather than Points, since a single instance of the struct holds only one pair of x,y coordinates.

我会将结构命名为类似Point而不是Points,因为该结构的单个实例仅包含一对 x,y 坐标。

Then a suitable distance function might be something like

那么一个合适的距离函数可能是这样的

float distance(const Point& point1, const Point& point2)
{
  return  sqrt((point1.x * point2.x) + (point1.y * point2.y));
}

You can get the distance between any two points in your input set like this:

您可以像这样获得输入集中任意两点之间的距离:

distance(point[i], point[j])

You might also want to measure the distances from your input points to a point that is not in the set, such as a point where you think the center of the circle might be. For example,

您可能还想测量从输入点到不在集合中的点的距离,例如您认为圆心可能所在的点。例如,

distance(point[i], candidate_center_of_circle)

If it were my code, I would likely make Pointa class and give it a member function for distance so that I could write something like

如果是我的代码,我可能会创建Point一个类并为其提供一个距离的成员函数,以便我可以编写类似

candidate_center_of_circle.distanceTo(point[i])

By the way, I mightname the variable pointsrather than pointbecause it is a vector that holds multiple instances of Point. If you intend to write things like point[i]a lot you might not like points[i], but if you are mostly going to make STL iterators over the vector then you would have something like this:

顺便说一句,我可能会命名变量,points而不是point因为它是一个包含Point. 如果你打算写point[i]很多你可能不喜欢的东西 points[i],但如果你主要是要在向量上制作 STL 迭代器,那么你会有这样的东西:

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