So I'm trying to implement this http://www.gameprogrammer.com/fractal.html#midpoint algorithm, to generate myself a terrain. In the tutorial is said:
OK, before we go into 2D fractal surfaces, you need to know about the roughness constant. This is the value which will determine how much the random number range is reduced each time through the loop and, therefore, will determine the roughness of the resulting fractal. The sample code uses a floating-point number in the range 0.0 to 1.0 and calls it H. 2(-H) is therefore a number in the range 1.0 (for small H) to 0.5 (for large H). The random number range can be multiplied by this amount each time through the loop. With H set to 1.0, the random number range will be halved each time through the loop, resulting in a very smooth fractal. With H set to 0.0, the range will not be reduced at all, resulting in something quite jagged.
Let's say rand_start
and rand_end
are the starting and ending values that a random number can be generated by using for example random.uniform(rand_start, rand_end)
or random.uniform(-1.0, 1.0)
Then I must reduce the number range so I do:
rand_start *= H * (pow(2, -H))
rand_end *= H * (pow(2, -H))
With H
set to 1.0 for example.Both rand_start
and rand_end
will be lowered by half and in the next iteration the random value will be between -0.5 and 0.5.
All clear, just like the tutorial said it.
Here is the problem:
My x
coordinate starts at 0 in the left side of the window, and y
coordinate in the top of the window, in Pygame
I draw my line at this coordinates:
line_start = (0, 300)
line_end
= (WINDOWWIDTH, 300)
How to reduce my random range if start and end values are initialized like that:
rand_start = 400
rand_end = 200
Again, my line starts at y
coordinate 300 and the highest the midpoint can be displaced is 200, the lowest - 400.
Any code required will be provided asap.