Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

algorithm - how to create a branching vein/river like structure on a square grid

I am trying to procedurally generate some rivers.

I have a flat (no concept of elevation) square grid as base and want to draw a branching structure on it like shown in the image.

Can you share the steps that one may use to get that done?

I am not looking for the fastest implementation as there is no real time generation, but the simpler implementation will be prefered. Lua is my language but anything will do.

Few more things:

  1. The shape should be generated algorithmic ally.
  2. The shape should be controllable using a seed value.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your river delta looks much like a tree. Here is some Python code using turtle for Graphics to draw a tree.

# You can edit this code and run it right here in the browser! # Try changing colors or adding your own shapes.

import turtle
from random import randint

def tree(length,n, ps):
    """ paints a branch of a tree with 2 smaller branches, like an Y"""
    if length < (length/n):
           return       # escape the function
    turtle.pensize(max(ps,1))     
    turtle.forward(length)        # paint the thik branch of the tree
    lb = 45+randint(-20,20)
    turtle.left(lb)          # rotate left for smaller "fork" branch
    tree(length * 0.5*(1+randint(-20,20)/100),length/n,ps-1) # create a smaller branch with 1/2 the lenght of the parent branch
    rb = 45+randint(-20,20)
    turtle.right(lb+rb)         # rotoate right for smaller "fork" branch
    tree(length * 0.6,length/n,ps-1)      # create second smaller branch
    turtle.left(rb)          # rotate back to original heading
    rt = randint(-20,20)
    turtle.right(rt)
    tree(length * 0.45,length/n,ps-1)
    turtle.left(rt)
    turtle.backward(length)       # move back to original position
    return              # leave the function, continue with calling program
turtle.left(90)
turtle.penup()
turtle.backward(250)
turtle.pendown()
tree(150,5,5)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...