gpt4 book ai didi

java - 钻石广场执行不当

转载 作者:行者123 更新时间:2023-11-30 02:35:40 25 4
gpt4 key购买 nike

堆栈社区您好,

我想了很久才发布这个帖子,因为我不想吸引另一个“重复”的帖子。然而,我已经没有想法了,也不知道有任何论坛或其他堆栈可以发布此文章以获得帮助。

我将此应用程序编写为一个有趣的项目,试图生成一些高度图。然而,每当我尝试一次生成多个高度图时,所有重复项都会显示为黑色空白,或者如果 MAP_SIZE 变量足够低,则显示为白色空白。 (例如,16 && 33 创建白色空白,1025 创建黑色)

我的输出文件夹如下所示:low value虚拟现实higher value

low value

high value

这是为什么呢?我在凌晨 3:15 错过的事情只是数学上的侥幸吗?我专门编写了 printMap 来实现检查 map 数据值的功能,并且当它们在指定它们为黑/白的范围内时。我认为没有理由在第一次迭代后继续存在。

只是为了好玩,我又打印了 44 张 map ,在第一个 map 之后,它们都是黑色的,MAP_SIZE 设置为 1025。请随意检查一下。

我根据这里的读数创建了菱形方形算法:http://www.gameprogrammer.com/fractal.html#heightmaps

还有我的 greyWriteImage,来自一个关于单纯形噪声图的旧堆栈溢出线程。

编辑感谢我能够解决我的问题,事实证明这只是一个简单的事实,对于您尝试使用 populateMap 函数创建的每个新 map ,我忘记将 avgOffset 重置为 1。本质上问题是你将 avgOffset 连续除以 2,得到的结果越来越小,并且总是以某种方式进行转换。

下面我为任何想要使用我的算法和输出的人提供了我完整的源代码。玩得开心。

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import java.util.concurrent.ThreadLocalRandom;

public class generateHeightMap {

// https://stackoverflow.com/questions/43179809/diamond-square-improper-implementation

private static final Random RAND = new Random();
// Size of map to generate, must be a value of (2^n+1), ie. 33, 65, 129
// 257,1025 are fun values
private static final int MAP_SIZE = 1025;
// initial seed for corners of map
private static final double SEED = ThreadLocalRandom.current().nextInt(0, 1 + 1);
// average offset of data between points
private static double avgOffSetInit = 1;

private static final String PATH = "C:\\Users\\bcm27\\Desktop\\grayScale_export";
private static String fileName = "\\grayscale_map00.PNG";

public generateHeightMap(int howManyMaps) {
System.out.printf("Seed: %s\nMap Size: %s\nAverage Offset: %s\n",
SEED, MAP_SIZE, avgOffSetInit);
System.out.println("-------------------------------------------");

for(int i = 1; i <= howManyMaps; i++){ // how many maps to generate

double[][] map = populateMap(new double[MAP_SIZE][MAP_SIZE]);
//printMap(map);
generateHeightMap.greyWriteImage(map);
fileName = "\\grayscale_map0" + i + ".PNG";

System.out.println("Output: " + PATH + fileName);
}
}
/*************************************************************************************
* @param requires a 2d map array of 0-1 values, and a valid file path
* @post creates a image file saved to path + file_name
************************************************************************************/
private static void greyWriteImage(double[][] data) {
BufferedImage image =
new BufferedImage(data.length, data[0].length, BufferedImage.TYPE_INT_RGB);

for (int y = 0; y < data[0].length; y++)
{
for (int x = 0; x < data.length; x++)
{// for each element in the data

if (data[x][y]>1){
// tells the image whether its white
data[x][y]=1;
}
if (data[x][y]<0){
// tells the image whether its black
data[x][y]=0;
}
Color col = // RBG colors
new Color((float)data[x][y],
(float)data[x][y],
(float)data[x][y]);
// sets the image pixel color equal to the RGB value
image.setRGB(x, y, col.getRGB());
}
}

try {
// retrieve image
File outputfile = new File( PATH + fileName);
outputfile.createNewFile();
ImageIO.write(image, "png", outputfile);

} catch (IOException e) {
throw new RuntimeException("I didn't handle this very well. ERROR:\n" + e);
}
}

/****************************************************************************
* @param requires map double[MAPSIZE][MAPSIZE]
* @return returns populated map
*
* [1] Taking a square of four points, generate a random value at the square
* midpoint, where the two diagonals meet. The midpoint value is calcul-
* ated by averaging the four corner values, plus a random amount. This
* gives you diamonds when you have multiple squares arranged in a grid.
*
* [2] Taking each diamond of four points, generate a random value at the
* center of the diamond. Calculate the midpoint value by averaging the
* corner values, plus a random amount generated in the same range as
* used for the diamond step. This gives you squares again.
*
* '*' equals a new value
* '=' equals a old value
*
* * - - - * = - - - = = - * - = = - = - = = * = * =
* - - - - - - - - - - - - - - - - * - * - * = * = *
* - - - - - - - * - - * - = - * = - = - = = * = * =
* - - - - - - - - - - - - - - - - * - * - * = * = *
* * - - - * = - - - = = - * - = = - = - = = * = * =
* A B C D E
*
* A: Seed corners
* B: Randomized center value
* C: Diamond step
* D: Repeated square step
* E: Inner diamond step
*
* Rinse and repeat C->D->E until data map is filled
*
***************************************************************************/
private static double[][] populateMap(double[][] map) {

// assures us we have a fresh map each time
double avgOffSet = avgOffSetInit;

// assigns the corners of the map values to SEED
map[0][0] =
map[0][MAP_SIZE-1] =
map[MAP_SIZE-1][0] =
map[MAP_SIZE-1][MAP_SIZE-1] = SEED;

// square and diamond loop start
for(int sideLength = MAP_SIZE-1; sideLength >= 2; sideLength /=2,avgOffSet/= 2.0) {

int halfSide = sideLength / 2;
double avgOfPoints;

/********************************************************************
* [1] SQUARE FRACTAL [1]
*********************************************************************/
// loops through x & y values of the height map
for(int x = 0; x < MAP_SIZE-1; x += sideLength) {
for(int y = 0; y <MAP_SIZE-1; y += sideLength) {

avgOfPoints = map[x][y] + //top left point
map[x + sideLength][y] + //top right point
map[x][y + sideLength] + //lower left point
map[x + sideLength][y + sideLength];//lower right point

// average of surrounding points
avgOfPoints /= 4.0;

// random value of 2*offset subtracted
// by offset for range of +/- the average
map[x+halfSide][y+halfSide] = avgOfPoints +
(RAND.nextDouble()*2*avgOffSet) - avgOffSet;
}
}

/********************************************************************
* [2] DIAMOND FRACTAL [2]
*********************************************************************/
for(int x=0; x < MAP_SIZE-1; x += halfSide) {
for(int y = (x + halfSide) % sideLength; y < MAP_SIZE-1;

y += sideLength) {
avgOfPoints =
map[(x - halfSide + MAP_SIZE) % MAP_SIZE][y] +//left of center
map[(x + halfSide) % MAP_SIZE][y] + //right of center
map[x][(y + halfSide) % MAP_SIZE] + //below center
map[x][(y - halfSide + MAP_SIZE) % MAP_SIZE]; //above center

// average of surrounding values
avgOfPoints /= 4.0;

// in range of +/- offset
avgOfPoints += (RAND.nextDouble()*2*avgOffSet) - avgOffSet;

//update value for center of diamond
map[x][y] = avgOfPoints;

// comment out for non wrapping values
if(x == 0) map[MAP_SIZE-1][y] = avgOfPoints;
if(y == 0) map[x][MAP_SIZE-1] = avgOfPoints;

} // end y
} // end x
} // end of diamond
return map;
} // end of populateMap

/*************************************************************************************
* @param requires a 2d map array to print the values of at +/-0.00
************************************************************************************/
@SuppressWarnings("unused")
private static void printMap(double[][] map) {
System.out.println("---------------------------------------------");

for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[x].length; y++) {
System.out.printf("%+.2f ", map[x][y] );
}
System.out.println();
}
}

} // end of class

最佳答案

是否有可能在创建每个 map 之前必须初始化 avgOffSet(populateMap 的开头)?

它被除以 2,但从未重置为 1。我认为每个映射都是独立的,即不依赖于前一个映射,因此没有理由不重置变量。但我不知道那个算法,也没有时间学习它......[:-|

private static double[][] populateMap(double[][] map) {

avgOffSet = 1; // missing this one

map[0][0] = ...

如果这是正确的,我建议 avgOffset 应该是一个变量;最终创建一个带有初始值的字段avgOffsetInitial(而不是当前字段)。

关于java - 钻石广场执行不当,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43179809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com