java方形_java – 菱形方形算法

这篇博客介绍了一种生成二维数据的有趣算法,该算法创建了“球形值”,即数据在边缘处包裹。通过迭代过程,算法计算每个位置的新值,基于其周围现有值的平均值加上随机偏移。此实现适用于2^n + 1大小的网格,并允许调整是否在边缘生成新值。
摘要由CSDN通过智能技术生成

这是一种用于生成值的有趣算法。这是我根据

this page in the references from the wikipedia article给出的解释创建的实现。它将创建“球形值”(包裹在所有边缘)。注释中有注释如何更改它以在边缘上生成新值而不是包装(尽管在这些情况下边缘的平均值并不正确)。

//size of grid to generate, note this must be a

//value 2^n+1

final int DATA_SIZE = 9;

//an initial seed value for the corners of the data

final double SEED = 1000.0;

double[][] data = new double[DATA_SIZE][DATA_SIZE];

//seed the data

data[0][0] = data[0][DATA_SIZE-1] = data[DATA_SIZE-1][0] =

data[DATA_SIZE-1][DATA_SIZE-1] = SEED;

double h = 500.0;//the range (-h -> +h) for the average offset

Random r = new Random();//for the new value in range of h

//side length is distance of a single square side

//or distance of diagonal in diamond

for(int sideLength = DATA_SIZE-1;

//side length must be >= 2 so we always have

//a new value (if its 1 we overwrite existing values

//on the last iteration)

sideLength >= 2;

//each iteration we are looking at smaller squares

//diamonds, and we decrease the variation of the offset

sideLength /=2, h/= 2.0){

//half the length of the side of a square

//or distance from diamond center to one corner

//(just to make calcs below a little clearer)

int halfSide = sideLength/2;

//generate the new square values

for(int x=0;x

for(int y=0;y

//x, y is upper left corner of square

//calculate average of existing corners

double avg = data[x][y] + //top left

data[x+sideLength][y] +//top right

data[x][y+sideLength] + //lower left

data[x+sideLength][y+sideLength];//lower right

avg /= 4.0;

//center is average plus random offset

data[x+halfSide][y+halfSide] =

//We calculate random value in range of 2h

//and then subtract h so the end value is

//in the range (-h, +h)

avg + (r.nextDouble()*2*h) - h;

}

}

//generate the diamond values

//since the diamonds are staggered we only move x

//by half side

//NOTE: if the data shouldn't wrap then x < DATA_SIZE

//to generate the far edge values

for(int x=0;x

//and y is x offset by half a side, but moved by

//the full side length

//NOTE: if the data shouldn't wrap then y < DATA_SIZE

//to generate the far edge values

for(int y=(x+halfSide)%sideLength;y

//x, y is center of diamond

//note we must use mod and add DATA_SIZE for subtraction

//so that we can wrap around the array to find the corners

double avg =

data[(x-halfSide+DATA_SIZE)%DATA_SIZE][y] + //left of center

data[(x+halfSide)%DATA_SIZE][y] + //right of center

data[x][(y+halfSide)%DATA_SIZE] + //below center

data[x][(y-halfSide+DATA_SIZE)%DATA_SIZE]; //above center

avg /= 4.0;

//new value = average plus random offset

//We calculate random value in range of 2h

//and then subtract h so the end value is

//in the range (-h, +h)

avg = avg + (r.nextDouble()*2*h) - h;

//update value for center of diamond

data[x][y] = avg;

//wrap values on the edges, remove

//this and adjust loop condition above

//for non-wrapping values.

if(x == 0) data[DATA_SIZE-1][y] = avg;

if(y == 0) data[x][DATA_SIZE-1] = avg;

}

}

}

//print out the data

for(double[] row : data){

for(double d : row){

System.out.printf("%8.3f ", d);

}

System.out.println();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值