简体   繁体   English

使用坐标xy

[英]Working with coordinates x y

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. 2-有2分。 Now the task is to find distance between them and its center. 现在的任务是找到它们与其中心之间的距离。

3 - There are 3 points. 3-有3分。

4 - More than 3 points. 4-超过3分。 Use of special algorithm 使用特殊算法

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

I would name the struct something like Point rather 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 Point a 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 might name the variable points rather than point because it is a vector that holds multiple instances of Point . 顺便说一句,我可能将变量命名为points而不是point因为它是一个包含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: 如果您打算编写很多类似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