- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试将代码从 JS
重写为 Java
。代码为钻石方 block 算法。
来源是:http://www.playfuljs.com/realistic-terrain-in-130-lines/
我重写了代码,但我的代码不工作......
输出不好。
我在 Java
中的代码是:
public class MapGenerator {
public static void main(String[] args) {
MapGenerator mg = new MapGenerator(9);
mg.generate();
mg.printMap();
}
private int size, max;
double[] map;
int[][] matrix;
public MapGenerator(int detail) {
this.size = (int) Math.pow(2, detail) + 1;
this.max = this.size - 1;
this.map = new double[this.size * this.size];
}
private double get(int x, int y) {
if (x < 0 || x > this.max || y < 0 || y > this.max) {
return -1;
}
return this.map[x + this.size * y];
}
private void set(int x, int y, double val) {
this.map[x + this.size * y] = val;
}
public void generate() {
set(0, 0, max);
set(this.max, 0, max / 2);
set(this.max, this.max, 0);
set(0, this.max, max / 2);
divide(this.max);
buildMatrix();
saveTerrain(0, 0, 0, 0, matrix, "vystup.ter");
}
private void buildMatrix() {
matrix = new int[size][size];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = (int) map[i + j];
}
}
}
private void divide(int size) {
//?
double roughness = 0.7;
int x, y, half = size / 2;
double scale = roughness * size;
if (half < 1) {
return;
}
for (y = half; y < max; y += size) {
for (x = half; x < max; x += size) {
square(x, y, half, Library.randInt(0, 100) * scale * 2 - scale);
}
}
for (y = 0; y <= max; y += half) {
for (x = (y + half) % size; x <= max; x += size) {
diamond(x, y, half, Library.randInt(0, 100) * scale * 2 - scale);
}
}
divide(size / 2);
}
private void square(int x, int y, int size, double offset) {
double tmp_1 = get(x, y - size); // top
double tmp_2 = get(x + size, y); // right
double tmp_3 = get(x, y + size); // bottom
double tmp_4 = get(x - size, y); // left
set(x, y, ((tmp_1 + tmp_2 + tmp_3 + tmp_4) / 4.0) + offset);
}
private void diamond(int x, int y, int size, double offset) {
double tmp_1 = get(x, y - size); // top
double tmp_2 = get(x + size, y); // right
double tmp_3 = get(x, y + size); // bottom
double tmp_4 = get(x - size, y); // left
set(x, y, ((tmp_1 + tmp_2 + tmp_3 + tmp_4) / 4.0) + offset);
}
public void printMap() {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println("");
}
}
public void saveTerrain(int canonX, int canonY, int targetX, int targetY,
int[][] terrain, String fName) {
int height = terrain.length;
int width = terrain[0].length;
DataOutputStream fout = null;
try {
// Samotný zápis dat
fout = new DataOutputStream(new FileOutputStream(fName));
fout.writeInt(width);
fout.writeInt(height);
fout.writeInt(canonX);
fout.writeInt(canonY);
fout.writeInt(targetX);
fout.writeInt(targetY);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
fout.writeInt(terrain[y][x]);
}
}
} /*
* Následuje pouze zavření souboru a ošetrení výjimek
*/ catch (FileNotFoundException e) {
System.err.println("Nepovedlo se otevrit vystupni soubor.");
} catch (IOException e) {
System.err.println("Nepovedlo se zapsat vystupni soubor.");
} finally {
try {
if (fout != null) {
fout.close();
}
} catch (IOException e) {
System.err.println("Nepovedlo se uzavrit vystupni soubor.");
}
}
}
}
谁能帮帮我?
算法的输出是 int[][]
,我使用另一个程序来想象它。
输出可视化:
它应该是这样的
我没有收到错误,但输出是错误的。感谢您的帮助。
最佳答案
我昨天(来自同一来源)编写了同样的代码,由于分割区域的方形步骤重叠以及同一步骤中的不规则性,递归方法真的很棘手。因此,我决定从头开始以迭代方式执行此操作(由于在我的编程环境中没有堆/堆栈垃圾,因此速度要快得多),如您所见,我从 130 行代码减少到大约 50 行代码。
这就是我在 C++ 中的处理方式(非递归):
void diamond_square(int size)
{
picture pic;
int x,y,xx,yy,xs,ys,d,d2,r;
for (xs=1;xs<size;xs<<=1); xs>>=1; ys=xs; // align to power of 2
pic.resize(xs+1,ys+1); pic.pf=_pf_u; // resize image to power of 2 +1
d=xs; d2=d>>1; r=128; // init step,half step and randomness
Randomize();
pic.p[ 0][ 0].dd=r; // set corners values (should be random but I want this)
pic.p[ 0][xs].dd=r;
pic.p[ys][ 0].dd=r;
pic.p[ys][xs].dd=r;
for (;d2;d=d2,d2>>=1) // subdivide step until full image is filled
{
// diamond
for (y=d2,yy=ys-d2;y<=yy;y+=d)
for (x=d2,xx=xs-d2;x<=xx;x+=d)
pic.p[y][x].dd=((pic.p[y-d2][x-d2].dd+pic.p[y-d2][x+d2].dd+pic.p[y+d2][x-d2].dd+pic.p[y+d2][x+d2].dd)>>2)+Random(r);
// square
for (y=d2,yy=ys-d2;y<=yy;y+=d)
for (x=d ,xx=xs-d ;x<=xx;x+=d)
pic.p[y][x].dd=((pic.p[y][x-d2].dd+pic.p[y][x+d2].dd+pic.p[y-d2][x].dd+pic.p[y+d2][x].dd)>>2)+Random(r);
for (y=d ,yy=ys-d ;y<=yy;y+=d)
for (x=d2,xx=xs-d2;x<=xx;x+=d)
pic.p[y][x].dd=((pic.p[y][x-d2].dd+pic.p[y][x+d2].dd+pic.p[y-d2][x].dd+pic.p[y+d2][x].dd)>>2)+Random(r);
for (x=d2,xx=xs-d2;x<=xx;x+=d)
{
y= 0; pic.p[y][x].dd=((pic.p[y][x-d2].dd+pic.p[y][x+d2].dd+pic.p[y+d2][x].dd)/3)+Random(r);
y=ys; pic.p[y][x].dd=((pic.p[y][x-d2].dd+pic.p[y][x+d2].dd+pic.p[y-d2][x].dd)/3)+Random(r);
}
for (y=d2,yy=ys-d2;y<=yy;y+=d)
{
x= 0; pic.p[y][x].dd=((pic.p[y][x+d2].dd+pic.p[y-d2][x].dd+pic.p[y+d2][x].dd)/3)+Random(r);
x=xs; pic.p[y][x].dd=((pic.p[y][x-d2].dd+pic.p[y-d2][x].dd+pic.p[y+d2][x].dd)/3)+Random(r);
}
// adjust randomness
r=(r*220)>>8; if (r<2) r=2;
}
// here pic holds the terrain map
}
我使用自己的 picture
类,所以这里有一些成员:
resize(xs,ys)
将图像调整为新的分辨率p[ys][xs].dd
是 32 位无符号整数 DWORD
pf
为像素格式(可以忽略)这是 diamond_square(513);
您可以使用 r
randomnes 初始值和调整值来改变行为。您也可以更改初始 Angular 值。
当心我程序中的局部 int xs,ys
变量拥有 2 个值的幂而不是 2 +1 的幂!!!
关于javascript - 菱形方 block 算法不起作用(将代码从 JS 重写为 JAVA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36246535/
我尝试创建菱形 View 。我想在弹出窗口中使用它。有谁知道该怎么做? 感谢任何建议。 最佳答案 看看 PathShape : 看起来您只需将具有正确线条/填充的路径传递给它,然后将其绘制在弹出对话框
我正在努力弄清楚如何添加像此处图片那样的边框。菱形/方形旋转边框。我试过边框图片,但似乎无法加载它们。 这两个 border-image: url('path-to-your-png') 33
当我知道每个 Angular 的度数时,如何使用 css 创建四边形。 我已经尝试通过变换和倾斜重新创建一个四边形。 但是,这并不是很好。 这是我尝试存档的内容。 具体要求是: 以此为背景的一个 di
本人是一名python初学者,刚刚看到一道有趣的python问题,“用python如何在编译器中打印出菱形图案?” 因此决定尝试一下,代码不多,仅供参考。 代码
我是jointJS的新手,我需要使用JointJS创建自定义形状,我尝试使用矩形创建菱形,使其高度和宽度相同,然后旋转45度,如下所示, var diamond = new joint.shapes
我的程序打印出这样的钻石: ...............* ..........* * * .....* * * * * * * * *
我正在使用 SMFL/C++ 制作一个 2D 等距游戏引擎,我在进行等距计算时得到了这个: 这是我在 2D 引擎中计算等距坐标的公式:对于 I-J 坐标,我有: x = (I - J) * (tile
我正在尝试创建一个横跨页面的菱形样式链接菜单。我还会有一些 java 脚本下拉菜单。像这样的东西:\\\\ 我在 div 上使用倾斜 css3 属性,但它们也会倾斜文本,有没有办法取消倾斜 div 中
我想制作一个带有图像背景的菱形。我能做到,唯一的问题是图像似乎同时旋转,这是我不想要的。这也需要在ie8中工作 fiddle :http://jsfiddle.net/zangief007/2bft2
我正在抓取一些韩语文本,大部分字符显示了 99.9%,但其余字符如下所示。 �Z 例如,它应该抓取“고소를해줫어”,但在我的输出中它给了我“고소를해�Z어”。 我知道这是一个编码问题,我只是不知道如何
我正在尝试用 Java 编写一个程序,从用户那里捕获一个整数(假设数据有效),然后根据整数的大小输出一个菱形,即用户输入 5 ,输出将是: --*-- -*-*- *---* -*-*- --*--
我最近将数据库的内容从一台主机移到了另一台主机。在那之后我得到了奇怪的字符,比如两个菱形或菱形,里面有问号而不是瑞典字符 ä。 我之前在第一台主机上遇到过这个问题,然后我添加了这段代码,它工作正常 $
是否可以使用react-native创建菱形 View ? 这是我想要的图片: 我尝试使用边框样式,但无法实现。 最佳答案 我在这里得到了解决方案:https://rnplay.org/apps/58
我正在尝试使用嵌套的 for 循环来显示星号菱形。 这是迄今为止我的代码: public class Diamond { public static void main(String[] ar
我正在做一个元素,我有一个需要响应的 div 钻石图片。 下图显示了我创建的 div 中的钻石,但它不适用于所有尺寸。我希望钻石能够对浏览器大小做出响应,因此它始终适合。 我有a jsFiddle ,
三角形 等腰直角三角形1 2.7 ? 1
我现在正在 ArgoUML 中设计 UML 事件图。 我知道如果我想设计如下条件: if(condition) { doTrueAction(); } else { doFalseAc
我有 2 个 SKSpriteNode: 一个简单的正方形 (A) 旋转(-45°)(B)的同一个正方形 我需要随时检查另一个 SKSpriteNode(一个球)的中心是否在这些方 block 之一内
1.输出上三角形 第一行1个星,第二行3个星,第三行5个星,第四行7个星,第五行9个星。 分析:三角形的形状由输出的空白和星组成,通过分析每一行输出几个空格,几个星,就可完成输出三角形的工作。
我想在 Swift 中创建一个带有 titleLabel 的菱形 UIButton。我的问题是,titleLabel 文本缩小并且仅显示三个点。如何扩展 titleLabel 的框架以获得足够的标题空
我是一名优秀的程序员,十分优秀!