text: Terrain Generation with Diamond Square - 酷辣虫
  综合编程	2016-06-27
  In the last two posts we looked at implementing the Midpoint Displacement algorithm for procedurally generating terrain. Today we're going to look at a similar algorithm called Diamond Square that fixes some problems with Midpoint Displacement.
  Midpoint Displacement is simple and fast, but it has some flaws. If you look at the end result you'll probably start to notice "seams" in the terrain that appear on perfectly square chunks. This happens because when you're calculating the midpoints of the edges of each square you're only averaging two sources of information.
  is an algorithm that works a lot like Midpoint Displacement, but it adds an extra step to ensure that (almost) every point uses four
  sources of data. This reduces the visual artifacts a lot without much extra effort.
  Let's draw the owl
  Set the midpoints of the edges of the heightmap to the average of the four points on the "diamond" around them (plus jitter).
  In the "square" step of Diamond Square, we take a square region of the heightmap and set the center of it to the average of the four corners (plus jitter).
  The Wikipedia article calls this the "diamond" step, but that seems backwards to me. We're operating on a square
  region of the heightmap, so this should be the square
  Since we're no longer working with heightmap "slices" like we were in the previous post, we'll need a way to specify the square chunk of the heightmap we're working on. I chose to use a center point ( x
  coordinates) and a radius:
  In the "diamond" step of Diamond Square, we take a diamond-shaped region and set the center of it to the average of the four corners:
  Once again we'll use a center point and radius to specify the region inside the heightmap:
  You might have noticed that we used heightmap-get-safe
  and safe-average
  in the ds-diamond
  code. This is to handle the diamonds on the edge of the heightmap. Those diamonds only have three points to average:
  Technically this means they have less information to work with than the rest of the points, and you might see some slight artifacts. But in practice it's not too bad, and it's a lot nicer than midpoint displacement where every
  line step uses only two
  Another way to handle this is to also wrap those edge coordinates around and pull the point from the other side of the heightmap. This is a bit more work but gives you an extra benefit: the heightmap will be tileable.
  Unfortunately we can't use recursion to iterate for Diamond Square like we did for Midpoint Displacement. We have to interleave the diamond and square steps, performing each round of squares on the entire
  map before moving on to a round of diamonds. If we don't, bad things will happen.
  Let's look at an example to see why. First we perform the initial square step:
  That's fine. Then we recurse into the first of the four diamonds:
  Everything's still good. Then we recurse into the square:
  But wait, there's a corner missing! We needed to complete that left-hand diamond step to get it, but that's still waiting on the stack for this line of recursion to bottom out.
  A "Strided" Iteration Construct
  So we need to use normal iteration. We can start by writing the code to do a single round of diamonds or squares on the heightmap. First we'll make a nice looping construct called do-stride
  This scary-looking macro builds the nested looping structure we'll need to perform the iteration for our diamonds and squares. It abstracts away the tedium of writing out nested for
  loops so we can focus on the rest of the algorithm.
  do-stride
  takes a list of variables to bind, a number to start at, a number to end before, and a body. For each variable it starts at start
  , runs body
  , increments by stride
  , and loops until the value is at or above end
  (do-stride [x] 0 5 1 (console.log x))
  will log 0 1 2 3 4
  (do-stride [x] 0 5 2 (console.log x))
  will log 0 2 4
  (do-stride [x y] 0 3 2 (console.log [x y]))
  will log [0, 0] [0, 2] [2, 0] [2, 2]
  Now that we've got this, we can iterate our square step over the heightmap for a specific radius:
  We use do-stride
  to start at the radius and loop until we fall off the heightmap, stepping by twice
  the radius each time.
  Because our heightmaps are always square we can use the same values for both dimensions. do-stride
  can take zero or more variables and will just do the right thing, so we don't need to worry about it.
  The stride is double the radius because as we step between squares we move over the right half of the first and
  the left half of the next:
  First of all, in the y
  dimension we need to start at 0
  instead of radius
  (these will be the top and bottom edge cases we looked at). Also, in the x
  direction all the even iterations need to start at radius
  , while the odd iterations should start at 0
  The code for the diamonds is the hairiest bit of the algorithm. Make sure you understand it before moving on. shift
  will be radius
  for even iterations and 0
  for odd ones:
  Finally we can put it all together. We initialize the corners and starting values, then loop over smaller and smaller radii and just call ds-squares
  and ds-diamonds
  to do the heavy lifting:
  The code for these blog posts is a bit of a mess because I've been copy/pasting to show the partially-completed demos after each step. I've created a littlesingle-page demo with completed versions of the various algorithms you can play with, and that code
  should be a lot more readable than the hacky code for these posts.
  责编内容by:Steve Losh (源链)。感谢您的支持!
  酷辣虫 » Terrain Generation with Diamond Square
  Triangle area in nD My typical go-to formula for the area of a triangle with vertices in nD is H...
  For kids, coding should be secondary to empathy, c... Nervous about our automated future, some parents are pushing kids as young as tw...
  Review: ZBrush 4R8 Pixologic's new update to its groundbreaking digital 3D sculpting, modelling and...
  How do I get my daughter interested in computers? I get this question a lot, especially from Silicon Valley tech VCs. Tech...