Java,1017字节
输入是一个用空格分隔的列表,如下所示:l s1 s2 s3 s4 h a b
。
输出是一个包含数字的二维数组。
程序:
import java.util.*;import static java.lang.Math.*;class C{public static void main(String[]a){int b=a.length,d=0;float[]c=new float[b];for(;d<b;){c[d]=Float.parseFloat(a[d++]);}e=(int)(pow(2,c[0])+1);f=new float[e][e];f[0][0]=c[1];f[0][e-1]=c[2];f[e-1][0]=c[3];f[e-1][e-1]=c[4];g=c[5];float h=c[6],i=c[7];s(0,0,e-1,e-1,h,i);System.out.print(Arrays.deepToString(f));}static int e;static float[][]f;static float g;static void s(int q,int r,int s,int t,float h,float i){if(s-q<2|t-r<2|q<0|r<0|s>=e|t>=e)return;float o,p;int m=(q+s)/2,n=(r+t)/2;f[m][n]=(float)(a(q,r,s,r,q,t,s,t)+random()*(i-h)-h);d(m,r,m-q,o=h*g,p=i*g);d(q,n,m-q,o,p);d(m,t,m-q,o,p);d(s,n,m-q,o,p);}static void d(int x,int y,int e,float h,float i){float o,p;f[x][y]=(float)(a(x,y-e,x+e,y,x,y+e,x-e,y)+random()*(i-h)-h);s(x-e,y-e,x,y,o=h*g,p=i*g);s(x,y-e,x+e,y,o,p);s(x-e,y,x,y+e,o,p);s(x,y,x+e,y+e,o,p);}static float a(int...j){float k=0,l=0;for(int d=0;d<j.length;d+=2){if(j[d]<0|j[d+1]<0|j[d]>=e|j[d+1]>=e)continue;l++;k+=f[j[d]][j[d+1]];}return k/l;}}
缩进并显示地图的程序:
import java.util.*;
import java.awt.image.*;
import java.awt.*;
import javax.swing.*;
import static java.lang.Math.*;
class D{
public static void main(String[]a){
int b=a.length,d=0;
float[]c=new float[b];
for(;d<b;){
c[d]=Float.parseFloat(a[d++]);
}
e=(int)(pow(2,c[0])+1);
f=new float[e][e];
f[0][0]=c[1];
f[0][e-1]=c[2];
f[e-1][0]=c[3];
f[e-1][e-1]=c[4];
g=c[5];
float h=c[6],i=c[7];
s(0,0,e-1,e-1,h,i);
showImage(f);
}
static int e;
static float[][]f;
static float g;
static void s(int q,int r,int s,int t,float h,float i){
if(s-q<2|t-r<2|q<0|r<0|s>=e|t>=e)
return;
float o,p;
int m=(q+s)/2,n=(r+t)/2;
f[m][n]=(float)(a(q,r,s,r,q,t,s,t)+random()*(i+h)-h);
d(m,r,m-q,o=h*g,p=i*g);
d(q,n,m-q,o,p);
d(m,t,m-q,o,p);
d(s,n,m-q,o,p);
}
static void d(int x,int y,int e,float h,float i){
float o,p;
f[x][y]=(float)(a(x,y-e,x+e,y,x,y+e,x-e,y)+random()*(i-h)+h);
s(x-e,y-e,x,y,o=h*g,p=i*g);
s(x,y-e,x+e,y,o,p);
s(x-e,y,x,y+e,o,p);
s(x,y,x+e,y+e,o,p);
}
static float a(int...j){
float k=0,l=0;
for(int d=0;d<j.length;d+=2){
if(j[d]<0|j[d+1]<0|j[d]>=e|j[d+1]>=e)
continue;
l++;
k+=f[j[d]][j[d+1]];
}
return k/l;
}
public static void showImage(float[][] f){
float maxHeight = Float.MIN_VALUE;
float minHeight = Float.MAX_VALUE;
for (float[] row : f){
for (float height : row){
if (height > maxHeight){
maxHeight = height;
}
if (height < minHeight){
minHeight = height;
}
}
}
int e = f.length;
BufferedImage image = new BufferedImage(e, e, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < e; x++){
for (int y = 0; y < e; y++){
Color color = Color.getHSBColor((float)((f[x][y] - minHeight)/(maxHeight - minHeight)), 1, 1);
image.setRGB(x,y,color.getRGB());
}
}
JFrame frame = new JFrame("Picture");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JComponent(){
@Override
public void paint(Graphics g){
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}
});
frame.setVisible(true);
frame.setBounds(0,0,e,e);
}
}
这是Java中显示地图的函数:
public static void showImage(float[][] map){
float maxHeight = Float.MIN_VALUE;
float minHeight = Float.MAX_VALUE;
for (float[] row : map){
for (float height : row){
if (height > maxHeight){
maxHeight = height;
}
if (height < minHeight){
minHeight = height;
}
}
}
int size = map.length;
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < size; x++){
for (int y = 0; y < size; y++){
Color color = Color.getHSBColor((float)((map[x][y] - minHeight)/(maxHeight - minHeight)), 1, 1);
image.setRGB(x,y,color.getRGB());
}
}
JFrame frame = new JFrame("Picture");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JComponent(){
@Override
public void paint(Graphics g){
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}
});
frame.setVisible(true);
frame.setBounds(0,0,size,size);
}
所有这些图像的大小为7
。的4个种子是5
,10
,15
,和20
。
a
和b
是-10
和10
分别。
粗糙度开始于,.1
并增加.1
直至1
。
地形生成代码即将推出!!!
图片即将推出!!!