将坐标保存到文件

koaltpgm  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(334)

我正在使用柏林噪声生成一个Map,并试图将坐标保存到一个json文件中。但由于某种原因,我面临的问题是,只有最后50个左右的坐标被保存到json文件中。
我也不确定我是否完全理解setjsonobject为什么将i作为它的第一个参数(我只是从文档中这样复制它以使其工作)
非常感谢你的帮助!

int tileSize = 20;
float scl = 0.1;
int[] x = new int [2000];
int[] y = new int [1000];
int[] terrainType = new int [2000];
JSONArray values;

void setup() {
  size(1080, 720);
  noStroke();
  colorMode(HSB);
  drawTerrain();
  //ptintln(x, y);
}

void draw() {
}

void keyPressed() {
  if (key == ' ') {
    noiseSeed(millis());
    drawTerrain();
  }
}

void drawTerrain() {

  values = new JSONArray();

  for (int i = 0; i < width/tileSize; i++) {
    for (int j = 0; j < height/tileSize; j++) {
      x[i] = i;
      y[j] = j;

      JSONObject coordinates = new JSONObject();

      coordinates.setInt("x", i);
      coordinates.setInt("y", j);

      values.setJSONObject(i, coordinates);

      println(x[i], y[j]);
      fill(getColour(i, j));
      rect(i * tileSize, j * tileSize, tileSize, tileSize);
    }
  }
  saveJSONArray(values, "data/new.json");
}

int getColour(int x, int y) {
  float v = noise(x * scl, y * scl);
  if (v < 0.3) {
    //water
    return color (155, 255, 255);
  } else if (v < 0.4) {
    //sand
    return color (30, 255, 255);
  } else if (v < 0.7) {
    //grass
    return color(66, 255, 255);
  } else {
    //forest
    return color (80, 255, 200);
  }
}

这是println打印出来的:

40 6
40 7
40 8
40 9
40 10
40 11
40 12
40 13
40 14
40 15
40 16
40 17
40 18
40 19
40 20
40 21
40 22
40 23
40 24
40 25
40 26
40 27
40 28
40 29
40 30
40 31
40 32
40 33
40 34
40 35
41 0
41 1
41 2
41 3
41 4
41 5
41 6
41 7
41 8
41 9
41 10
41 11
41 12
41 13
41 14
41 15
41 16
41 17
41 18
41 19
41 20
41 21
41 22
41 23
41 24
41 25
41 26
41 27
41 28
41 29
41 30
41 31
41 32
41 33
41 34
41 35
42 0
42 1
42 2
42 3
42 4
42 5
42 6
42 7
42 8
42 9
42 10
42 11
42 12
42 13
42 14
42 15
42 16
42 17
42 18
42 19
42 20
42 21
42 22
42 23
42 24
42 25
42 26
42 27
42 28
42 29
42 30
42 31
42 32
42 33
42 34
42 35
43 0
43 1
43 2
43 3
43 4
43 5
43 6
43 7
43 8
43 9
43 10
43 11
43 12
43 13
43 14
43 15
43 16
43 17
43 18
43 19
43 20
43 21
43 22
43 23
43 24
43 25
43 26
43 27
43 28
43 29
43 30
43 31
43 32
43 33
43 34
43 35
44 0
44 1
44 2
44 3
44 4
44 5
44 6
44 7
44 8
44 9
44 10
44 11
44 12
44 13
44 14
44 15
44 16
44 17
44 18
44 19
44 20
44 21
44 22
44 23
44 24
44 25
44 26
44 27
44 28
44 29
44 30
44 31
44 32
44 33
44 34
44 35
45 0
45 1
45 2
45 3
45 4
45 5
45 6
45 7
45 8
45 9
45 10
45 11
45 12
45 13
45 14
45 15
45 16
45 17
45 18
45 19
45 20
45 21
45 22
45 23
45 24
45 25
45 26
45 27
45 28
45 29
45 30
45 31
45 32
45 33
45 34
45 35
46 0
46 1
46 2
46 3
46 4
46 5
46 6
46 7
46 8
46 9
46 10
46 11
46 12
46 13
46 14
46 15
46 16
46 17
46 18
46 19
46 20
46 21
46 22
46 23
46 24
46 25
46 26
46 27
46 28
46 29
46 30
46 31
46 32
46 33
46 34
46 35
47 0
47 1
47 2
47 3
47 4
47 5
47 6
47 7
47 8
47 9
47 10
47 11
47 12
47 13
47 14
47 15
47 16
47 17
47 18
47 19
47 20
47 21
47 22
47 23
47 24
47 25
47 26
47 27
47 28
47 29
47 30
47 31
47 32
47 33
47 34
47 35
48 0
48 1
48 2
48 3
48 4
48 5
48 6
48 7
48 8
48 9
48 10
48 11
48 12
48 13
48 14
48 15
48 16
48 17
48 18
48 19
48 20
48 21
48 22
48 23
48 24
48 25
48 26
48 27
48 28
48 29
48 30
48 31
48 32
48 33
48 34
48 35
49 0
49 1
49 2
49 3
49 4
49 5
49 6
49 7
49 8
49 9
49 10
49 11
49 12
49 13
49 14
49 15
49 16
49 17
49 18
49 19
49 20
49 21
49 22
49 23
49 24
49 25
49 26
49 27
49 28
49 29
49 30
49 31
49 32
49 33
49 34
49 35
50 0
50 1
50 2
50 3
50 4
50 5
50 6
50 7
50 8
50 9
50 10
50 11
50 12
50 13
50 14
50 15
50 16
50 17
50 18
50 19
50 20
50 21
50 22
50 23
50 24
50 25
50 26
50 27
50 28
50 29
50 30
50 31
50 32
50 33
50 34
50 35
51 0
51 1
51 2
51 3
51 4
51 5
51 6
51 7
51 8
51 9
51 10
51 11
51 12
51 13
51 14
51 15
51 16
51 17
51 18
51 19
51 20
51 21
51 22
51 23
51 24
51 25
51 26
51 27
51 28
51 29
51 30
51 31
51 32
51 33
51 34
51 35
52 0
52 1
52 2
52 3
52 4
52 5
52 6
52 7
52 8
52 9
52 10
52 11
52 12
52 13
52 14
52 15
52 16
52 17
52 18
52 19
52 20
52 21
52 22
52 23
52 24
52 25
52 26
52 27
52 28
52 29
52 30
52 31
52 32
52 33
52 34
52 35
53 0
53 1
53 2
53 3
53 4
53 5
53 6
53 7
53 8
53 9
53 10
53 11
53 12
53 13
53 14
53 15
53 16
53 17
53 18
53 19
53 20
53 21
53 22
53 23
53 24
53 25
53 26
53 27
53 28
53 29
53 30
53 31
53 32
53 33
53 34
53 35

这是保存的json文件:

[
  {
    "topography": 0.8394001722335815,
    "x": 0,
    "y": 35
  },
  {
    "topography": 0.7945913076400757,
    "x": 1,
    "y": 35
  },
  {
    "topography": 0.7647959589958191,
    "x": 2,
    "y": 35
  },
  {
    "topography": 0.7350562214851379,
    "x": 3,
    "y": 35
  },
  {
    "topography": 0.6939810514450073,
    "x": 4,
    "y": 35
  },
  {
    "topography": 0.6933805346488953,
    "x": 5,
    "y": 35
  },
  {
    "topography": 0.6550765633583069,
    "x": 6,
    "y": 35
  },
  {
    "topography": 0.6037718057632446,
    "x": 7,
    "y": 35
  },
  {
    "topography": 0.56168133020401,
    "x": 8,
    "y": 35
  },
  {
    "topography": 0.5543783903121948,
    "x": 9,
    "y": 35
  },
  {
    "topography": 0.5556368827819824,
    "x": 10,
    "y": 35
  },
  {
    "topography": 0.5415823459625244,
    "x": 11,
    "y": 35
  },
  {
    "topography": 0.4900124669075012,
    "x": 12,
    "y": 35
  },
  {
    "topography": 0.47219547629356384,
    "x": 13,
    "y": 35
  },
  {
    "topography": 0.5055332779884338,
    "x": 14,
    "y": 35
  },
  {
    "topography": 0.5263981819152832,
    "x": 15,
    "y": 35
  },
  {
    "topography": 0.49106156826019287,
    "x": 16,
    "y": 35
  },
  {
    "topography": 0.4588107764720917,
    "x": 17,
    "y": 35
  },
  {
    "topography": 0.4684934616088867,
    "x": 18,
    "y": 35
  },
  {
    "topography": 0.4896576404571533,
    "x": 19,
    "y": 35
  },
  {
    "topography": 0.5030308365821838,
    "x": 20,
    "y": 35
  },
  {
    "topography": 0.5619486570358276,
    "x": 21,
    "y": 35
  },
  {
    "topography": 0.5998398065567017,
    "x": 22,
    "y": 35
  },
  {
    "topography": 0.6354892253875732,
    "x": 23,
    "y": 35
  },
  {
    "topography": 0.6773940324783325,
    "x": 24,
    "y": 35
  },
  {
    "topography": 0.680229902267456,
    "x": 25,
    "y": 35
  },
  {
    "topography": 0.6836848258972168,
    "x": 26,
    "y": 35
  },
  {
    "topography": 0.6646286845207214,
    "x": 27,
    "y": 35
  },
  {
    "topography": 0.6194458603858948,
    "x": 28,
    "y": 35
  },
  {
    "topography": 0.5662513971328735,
    "x": 29,
    "y": 35
  },
  {
    "topography": 0.5241678953170776,
    "x": 30,
    "y": 35
  },
  {
    "topography": 0.5546122193336487,
    "x": 31,
    "y": 35
  },
  {
    "topography": 0.5811091065406799,
    "x": 32,
    "y": 35
  },
  {
    "topography": 0.594294011592865,
    "x": 33,
    "y": 35
  },
  {
    "topography": 0.5982682108879089,
    "x": 34,
    "y": 35
  },
  {
    "topography": 0.5531954765319824,
    "x": 35,
    "y": 35
  },
  {
    "topography": 0.5395386219024658,
    "x": 36,
    "y": 35
  },
  {
    "topography": 0.5353401303291321,
    "x": 37,
    "y": 35
  },
  {
    "topography": 0.5631544589996338,
    "x": 38,
    "y": 35
  },
  {
    "topography": 0.5797415375709534,
    "x": 39,
    "y": 35
  },
  {
    "topography": 0.5819047689437866,
    "x": 40,
    "y": 35
  },
  {
    "topography": 0.5945364832878113,
    "x": 41,
    "y": 35
  },
  {
    "topography": 0.5516071319580078,
    "x": 42,
    "y": 35
  },
  {
    "topography": 0.47375544905662537,
    "x": 43,
    "y": 35
  },
  {
    "topography": 0.38041284680366516,
    "x": 44,
    "y": 35
  },
  {
    "topography": 0.288770467042923,
    "x": 45,
    "y": 35
  },
  {
    "topography": 0.28685975074768066,
    "x": 46,
    "y": 35
  },
  {
    "topography": 0.36876726150512695,
    "x": 47,
    "y": 35
  },
  {
    "topography": 0.41278138756752014,
    "x": 48,
    "y": 35
  },
  {
    "topography": 0.41216781735420227,
    "x": 49,
    "y": 35
  },
  {
    "topography": 0.4043394923210144,
    "x": 50,
    "y": 35
  },
  {
    "topography": 0.39638426899909973,
    "x": 51,
    "y": 35
  },
  {
    "topography": 0.39632344245910645,
    "x": 52,
    "y": 35
  },
  {
    "topography": 0.3899587392807007,
    "x": 53,
    "y": 35
  }
]
lsmd5eda

lsmd5eda1#

我能为任何寻找答案的人找到答案:
基本上可以归结为以下几点:

values.setJSONObject(i, coordinates);

显然地, i 必须是一个唯一的标识,用于标识存储到文件中的对象(或类似对象),如果存在重复的对象,则它将用相同的编号覆盖上一个对象。这就是为什么它只存储第二个系统的最后53个对象 for 循环的迭代。
解决的办法是,几乎只是宣布一个新的 int count 然后在第二个for循环中增加一次。然后使用它来标识传递给json文件的每个对象。

values.setJSONObject(count, coordinates);

相关问题