创建网格并分配值

时间:2014-02-04 13:56:49

标签: c++

我想创建一个具有明确间距的世界的二维平方网格(比方说d)

-180 =< j =< 180
-90 =< k =< 90

Grid[j][k]

然后为每个单元分配我存储在两个数组中的lon lat点

0 =< i < N

Lon[i]
Lat[i]

按细胞分组观察结果。

我正在考虑使用嵌套循环来创建网格,但我不确定如何使用它。

非常感谢

2 个答案:

答案 0 :(得分:1)

在没有大量研究迹象的情况下,您可能会被SO社区大肆宣传为新手问题。但这是我的强制性答案。

struct Position
{
    double Lon, Lat;
}

const unsigned short lonCount = 180;
const unsigned short latCount = 360;

Position positions[lonCount][latCount];

for( unsigned short lon = -90; lon <= 90; lon++ )
{
    for( unsigned short lat = -180; lat <= 180; lat++ )
    {
        Position* p = &positions[lon + lonCount / 2][lat + latCount / 2];
        p->Lon = lon;
        p->Lat = lat;
    }
}

这应该让你开始。

答案 1 :(得分:-1)

struct Position {
  double Lon, Lat;
    int index;
};

int main(){
printf ("\n");


    static const double arrlon[] = {100,180,180};   
    static const double arrlat[] = {0,2,3};
    static const int arrind[] = {0,0,0};
    vector<double> londat (arrlon, arrlon + sizeof(arrlon) / sizeof(arrlon[0]));
    vector<double> latdat (arrlat, arrlat + sizeof(arrlat) / sizeof(arrlat[0]));
    vector<int> index (arrind, arrind + sizeof(arrind) / sizeof(arrind[0]));
    int N = sizeof(arrlat)/ sizeof(arrlat[0]) ;

    const int lonCount = 360;
    const int latCount = 180;
    const int step = 5;

    Position positions[lonCount+1][latCount+1];
    Position* p;

    int count = 0;
    for(int lon = 0; lon <= 360; lon=lon+step){
    for(int lat = 0; lat <= 180; lat=lat+step){
      p = &positions[lon][lat];
      p->Lon = lon;
      p->Lat = lat;
            p->index = count;
            count++;
            for (int i = 0; i<N ; i++){
                if(londat[i] >= p->Lon && londat[i] < p->Lon+step && latdat[i] >= p->Lat && latdat[i] < p->Lat+step) index[i] = p->index;
            }
       }
    }

    for (int i = 0; i<N ; i++) cerr << i << "   " << index[i] << endl;

    return 0;
}