Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用复合数据结构的最近点计算 我正在读Robert Sedwick Algorithms在C++中的一本书。以下是本书中给出的关于复合数据结构的示例_C++_Algorithm_Computational Geometry - Fatal编程技术网

使用复合数据结构的最近点计算 我正在读Robert Sedwick Algorithms在C++中的一本书。以下是本书中给出的关于复合数据结构的示例

使用复合数据结构的最近点计算 我正在读Robert Sedwick Algorithms在C++中的一本书。以下是本书中给出的关于复合数据结构的示例,c++,algorithm,computational-geometry,C++,Algorithm,Computational Geometry,问题陈述: 给定“d”,我们想知道单位正方形中N个点的集合中有多少对可以通过长度小于“d”的直线连接 下面的程序使用逻辑将单位正方形划分为一个网格,并维护一个二维链表数组,每个网格正方形对应一个列表。选择的网格足够精细,距离“d”内的所有点要么在同一个网格正方形中,要么在相邻的网格正方形中 我的问题是 为什么作者在malloc2d中分配G+2(G+2,G+2) 在gridinsert函数中,为什么作者执行以下语句int X=X*G+1;int Y=Y*G+1 在for循环中,为什么要将i初始化为

问题陈述: 给定“d”,我们想知道单位正方形中N个点的集合中有多少对可以通过长度小于“d”的直线连接

下面的程序使用逻辑将单位正方形划分为一个网格,并维护一个二维链表数组,每个网格正方形对应一个列表。选择的网格足够精细,距离“d”内的所有点要么在同一个网格正方形中,要么在相邻的网格正方形中

我的问题是

  • 为什么作者在malloc2d中分配G+2(G+2,G+2)
  • 在gridinsert函数中,为什么作者执行以下语句int X=X*G+1;int Y=Y*G+1
  • 在for循环中,为什么要将i初始化为X-1,将j初始化为Y-1
  • 在代码中,我们在同一方格或相邻方格中保持距离d内的点
  • 请用简单的例子帮助您理解以下程序

    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    using namespace std;
    
    
    float randFloat() {
        return 1.0*rand()/RAND_MAX;
    }
    
    
    struct myPoint {
        float x;
        float y;
    };
    
    float myDistance(myPoint a, myPoint b) {
        float dx = a.x - b.x, dy = a.y - b.y;
        return sqrt(dx*dx + dy*dy);
    }
    
    struct node {
        myPoint p; node *next; 
        node(myPoint pt, node* t) {
            p = pt; next = t;
        }
    };
    
    typedef node *link;
    static link **grid = NULL; 
    
    link **malloc2d(int r, int c) {
        link **t = new link*[r];
        for (int i = 0; i < r; i++) {
          t[i] = new link[c];
        }
        return t;
     }
    
    static int G, cnt = 0; 
    static float d;
    
    void gridinsert(float x, float y) {
        int X = x*G+1;
        int Y = y*G+1;
        myPoint p;
        p.x = x; p.y = y;
        link s, t = new node(p, grid[X][Y]);
        for (int i = X-1; i <= X+1; i++)
          for (int j = Y-1; j <= Y+1; j++)
            for (s = grid[i][j]; s != 0; s = s->next)
               if (myDistance(s->p, t->p) < d) cnt++; 
    
        grid[X][Y] = t;
     }
    
    int main(int argc, char *argv[]) { 
    
        int i; 
        int N = 10;
        d = 0.25;
        G = 1/d;
    
        grid = malloc2d(G+2, G+2);
        for (i = 0; i < G+2; i++)
            for (int j = 0; j < G+2; j++)
                grid[i][j] = 0;
    
        for (i = 0; i < N; i++)
            gridinsert(randFloat(), randFloat());
    
        cout << cnt << " pairs within " << d << endl;
    
       return 0;
     }
    
    #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    float和float(){
    返回1.0*rand()/rand_MAX;
    }
    结构myPoint{
    浮动x;
    浮动y;
    };
    浮动我的距离(我的点a、我的点b){
    浮动dx=a.x-b.x,dy=a.y-b.y;
    返回sqrt(dx*dx+dy*dy);
    }
    结构节点{
    myPoint p;节点*next;
    节点(myPoint pt,节点*t){
    p=pt;next=t;
    }
    };
    typedef节点*链接;
    静态链接**网格=空;
    链接**malloc2d(内部r,内部c){
    链路**t=新链路*[r];
    对于(int i=0;ip)其思想是检查网格的所有相邻单元。但边界单元没有邻接。因此,为了避免复杂的边界检查,我们将网格额外扩展2个单元-在第一个单元之前和最后一个单元之后。这些单元是“虚拟的”并且永远不会包含任何点-它们只是为了简化算法和为边界单元提供邻接

  • (X,Y)-包含该点的网格中单元的坐标(索引)。根据第1页,我们必须从单元(1,1)开始放置点,而不是(0,0)。(0,0)和任何其他边界点都是虚拟的

  • 因为我们检查了网格的所有相邻单元,(X,Y)的相邻单元是(X-1,Y-1),(X,Y-1),(X+1,Y-1)等到(X+1,Y+1)。这就是为什么我们有从X-1到X+1和从Y-1到Y+1的循环

  • 我们不维护它们,只需检查任何输入点与现有集合,并在每次匹配距离时递增计数器
    cnt
    。问题条件不要求保留这些对的列表。如果需要保留点的列表,则应修改
    gridinsert()
    ,例如,放置
    (s->p,t->p)
    到循环中的某个容器,而不是增量
    cnt++