【问题标题】:c++ Reading 3D coordinates from a large array and calculating the distance between themc ++从大数组中读取3D坐标并计算它们之间的距离
【发布时间】:2018-03-12 13:33:24
【问题描述】:

我有一个任务,因为我真的很挣扎:

编写一个程序,将input-week6-ad-q3.txt(200个点的3D坐标)读入数组,报告如下 1.point1与其他点的距离(考虑第1个坐标是point1,第2个point2,,,.) 2.离点1最近的点和点1到最近点的距离。

我尝试过启动它,但目前无法获取坐标,我什至不知道从哪里开始寻找最近的点。任何帮助将非常感激。

这是我目前所得到的:

#include <iostream>
#include <fstream>

using namespace std;

const int MAX = 200;
typedef double x_coord[MAX];
typedef double y_coord[MAX];
typedef double z_coord[MAX];

int main()
{
    x_coord x;
    y_coord y;
    z_coord z;
    int count;
    ifstream in_stream;

    in_stream.open("input-week6-ad-q4-2.txt");
    in_stream.get(x, y, z);
    for (count = 0; count < MAX; count++)
    {
        x[count] = x;
        in_stream.get(x);

        y[count] = y;
        in_stream.get(y);

        z[count] = z;
        in_stream.get(z);
    }
    in_stream.close();

    system("pause");
    return 0;
}

输入文件的点布局如下:

Coordinates of many points  
   x        y         z
-0.06325 0.0359793 0.0420873 
-0.06275 0.0360343 0.0425949 
-0.0645 0.0365101 0.0404362 

等等。

【问题讨论】:

  • 不要将“寻找最近点”视为编程问题。把它想象成一道数学题。一旦你在纸上得到了解决方案,你就可以开始尝试编码了
  • 提示——最快的方法不是比较距离;但距离平方!
  • get() 返回单个字符,这不是您想要的。 in_stream &gt;&gt; x[count]; 会有所改进。
  • 我建议使用结构的集合(向量或数组)。你可以调用结构Point3d。通常结构数组比并行数组更好(考虑同步问题)。此外,处理器可以将结构的元素加载到缓存行中,这比 3 个数组更容易。

标签: c++ arrays stream


【解决方案1】:

回答你问题的第二部分,通常二维空间中点之间的距离称为Euclidean distance

【讨论】:

    【解决方案2】:

    我们将在这里用一块石头杀死两只鸟

    首先为该点创建一个结构:

    struct Point3d
    {
      double x, y, z;
    };
    

    接下来,重载operator&gt;&gt; 以读取点值:

    struct Point3d
    {
      double x, y, z;
      friend std::istream& operator>>(std::istream& input, Point3d& p);
    };
    std::istream& operator>>(std::istream& input, Point3d& p)
    {
      input >> p.x >> p.y >> p.z;
      return input;
    }
    

    接下来,使用std::vector创建一个数据库:
    typedef std::vector Database_Type; Database_Type 数据库;

    输入循环来了:

    std::string text_line;
    // Ignore the header lines
    std::getline(my_data, text_line);
    std::getline(my_data, text_line);
    Point3d p;
    while (my_data >> p)
    {
      database.push_back(p);
    }
    

    您可以在结构中插入距离函数:

    struct Point3d
    {
      //...
      double distance(const Point3d& p) const;
    };
    double
    Point3d::
    distance(const Point3d& p) const
    {
      const double x_diff_squared = (x - p.x) * (x - p.x);
      const double y_diff_squared = (y - p.y) * (y - p.y);
      const double z_diff_squared = (z - p.z) * (z - p.z);
      return sqrt(x_diff_squared + y_diff_squared + z_diff_squared);
    }
    

    上述方法将允许您执行以下操作:

    const double distance_p1_p2 = database[0].distance(database[1]);
    

    编辑 1:独立距离函数
    或者,您可以制作一个独立的距离函数:

    double Distance(const Point3d& p1, const Point3d& p2)
    {
          const double x_diff_squared = (p1.x - p2.x) * (p1.x - p2.x);
          const double y_diff_squared = (p1.y - p2.y) * (p1.y - p2.y);
          const double z_diff_squared = (p1.z - p2.z) * (p1.z - p2.z);
          return sqrt(x_diff_squared + y_diff_squared + z_diff_squared);
    }
    

    应用为:

    const distance_p1_p2 = Distance(database[0], database[1]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多