简体   繁体   English

此JavaScript代码是否遵循Midpoint Displacement算法?

[英]Does this JavaScript code follow the Midpoint Displacement algorithm?

I'm trying to use the Midpoint Displacement Algorithm using JavaScript and canvas as recommended on gamedev.stackexchange.com . 我正在尝试使用gamedev.stackexchange.com上推荐的使用JavaScript和canvas中点位移算法

The code below generates points where the array index is the x position and its value is the y position. 下面的代码生成点,其中数组索引是x位置,其值是y位置。

var createTerrain = function(chops, range) {
    chops = chops || 2;
    range = parseInt(range || 100); 

    if (chops > 8) return;

    var cycle = parseInt(width / chops);    

    for (var i = 0; i <= width; i += cycle) {

        var y = (height / 2) + getRandomNumber(-range, range);

        terrain[i] = y;

    }

    chops *= 2;
    range *= 0.5;
    createTerrain(chops, range);

}

getRandomNumber() 's arguments are min and max . getRandomNumber()的参数是minmax width and height are respective of the canvas . widthheight分别是canvas

This produces something like... 这会产生类似...... 帆布

It seems pretty choppy, but that may just be how it is. 它看起来很不稳定,但这可能就是这样。

Have I got this algorithm right? 我有这个算法吗? If not, what is wrong, and can you point me in the right direction? 如果没有,那有什么不对,你能指出我正确的方向吗?

I'd say it does not look like mid-point displacement, simply because every segment should be getting a regular X axis length (you have nearly vertical segments). 我会说它看起来不像中点位移,只是因为每个段都应该有一个规则的X轴长度(你有几乎垂直的段)。 Have you tried to generate a simple 4-segment array? 您是否尝试生成一个简单的4段数组? What does it look like? 它是什么样子的?

Here is a short tutorial I wrote, you can see it in action: Mid-point algorithm in Javascript 这是我写的一个简短的教程,你可以看到它在行动: Javascript中的中点算法

The source code: 源代码:

function Terrain(segmentCount) {
    this.segmentCount = segmentCount;
    this.points = [];
    for (i=0; i<=segmentCount; ++i) {
        this.points[i] = 0;
    }
};

/**
* Generate the terrain using the mid-point displacement algorithm. This is in fact
* a shortcut to the recursive function with the appropriate value to start
* generating the terrain.
*
* @param maxElevation the maximal vertical displacement to apply at this iteration
* @param sharpness how much to attenuate maxElevation at each iteration (lower
*        value means a smoother terrain). Keep between 0 and 1.
*/
Terrain.prototype.generateUsingMidPoint = function(maxElevation, sharpness) {
    this.midPoint(0, this.segmentCount, maxElevation, sharpness);
}

/**
* This is the recursive function to actually generate the terrain. It computes a new height for the point
* between "start" and "end" (the mid-point): averages start and end heights and adds a random
* displacement.
*
* @param maxElevation the maximal vertical displacement to apply at this iteration
* @param sharpness how much to attenuate maxElevation at each iteration (lower
*        value means a smoother terrain). Keep between 0 and 1.
*/
Terrain.prototype.midPoint = function(start, end, maxElevation, sharpness) {
    var middle = Math.round((start + end) * 0.5);
    if ((end-start<=1) || middle==start || middle==end) {
        return;
    }

    var newAltitude = 0.5 * (this.points[end] + this.points[start]) + maxElevation*(1 - 2*Math.random());
    this.points[middle] = newAltitude;

    this.midPoint(start, middle, maxElevation*sharpness, sharpness);
    this.midPoint(middle, end, maxElevation*sharpness, sharpness);
};

The algorithm you have implemented is clearly wrong, from looking at the result. 从查看结果来看,您实施的算法显然是错误的。 Where you went wrong is hard to say, since the code you have posted is not self contained. 你出错的地方很难说,因为你发布的代码不是自包含的。 You are operating on several variables defined in a parent scope and it is not immediately apparent what they contain or how they behave. 您正在对父作用域中定义的多个变量进行操作,并且不会立即显示它们包含的内容或它们的行为方式。

What is terrain for example? 例如什么是terrain

What you have to realize first of all is, that if the start and endpoints don't match on the y-axis you have done something wrong ;) 首先要知道的是,如果起点和终点在y轴上不匹配,那么你做错了;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM