Java 将坐标保存到文件
我正在使用柏林噪声生成一个地图,并试图将坐标保存到json文件中。但出于某种原因,我面临的问题是,只有最后50个左右的坐标被保存到json文件中Java 将坐标保存到文件,java,processing,Java,Processing,我正在使用柏林噪声生成一个地图,并试图将坐标保存到json文件中。但出于某种原因,我面临的问题是,只有最后50个左右的坐标被保存到json文件中 values.setJSONObject(count, coordinates); 我也不确定我是否完全理解setJSONObject为什么将I作为其第一个参数(我只是从文档中复制了它以使其工作) 非常感谢你的帮助 int tileSize = 20; float scl = 0.1; int[] x = new int [2000]; int[]
values.setJSONObject(count, coordinates);
我也不确定我是否完全理解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);
}
}
这是保存的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
}
]
我能够为任何寻找答案的人找到答案: 从本质上讲,这一切都归结为以下几点:
values.setJSONObject(i, coordinates);
显然,i
需要是一个唯一的id,用于标识存储到文件中的对象(或类似对象),如果存在重复的对象,则它将使用相同的编号覆盖上一个对象。这就是为什么它只为循环的迭代存储第二次的最后53个对象
解决方法是,几乎只需声明一个新的int count
,然后在第二个for循环中增加它一次。然后使用它标识传递到json文件的每个对象
values.setJSONObject(count, coordinates);
宽度/标题大小的值是多少,高度/标题大小的值是多少?tileSize是20。因为我用的是矩形,所以它是rect(I*20,j*20,20,20)@LajosArpad如果我没有正确回答你的问题,我道歉。我是编程新手。也许你能帮我知道你在找什么,因为我的全部代码都是你能看到的。不用担心。然后您需要学习调试。作为一名程序员,你需要遵循的一条主要规则是,不要依赖于你的理论,而要依赖于事实。一个理论足以为你提供一个假设,但是你需要实际的事实来找出问题所在。请调试您的代码,找出我想知道的值是什么。这可能是找到你问题答案的关键。好吧,我的坏@LajosArpad。所以宽度/tileSize=54,高度/tileSize=36。