我即将开始一个大学项目,为现有项目建立一个程序化城市.
我想知道你们之前是否有任何编码L-Systems的经验,并且知道我开始的好地方.在使用程序方法和Perlin Noise和fBm之前我做了一些工作,所以我得到了L-System在分形意义上的前提.我正在寻找一个可能会让我朝着编码L系统的方向发展的地方.您可以指向我的任何帮助或技术文档都会很棒.
c++ algorithm procedural-programming l-systems procedural-generation
我将创建一个可以从L系统语法生成字符串的程序.
Astrid Lindenmayer用于模拟藻类生长的原始L系统是:
variables : A B constants : none axiom : A rules : (A ? AB), (B ? A)
产生:
iteration | resulting model 0 | A 1 | AB 2 | ABA 3 | ABAAB 4 | ABAABABA 5 | ABAABABAABAAB
我自己在J中天真地实现了这样:
algae =: 1&algae : (([: ; (('AB'"0)`('A'"0) @. ('AB' i. ]))&.>"0)^:[) "1 0 1
(i.6) ([;algae)"1 0 1 'A'
?????????????????
?0?A ?
?????????????????
?1?AB ?
?????????????????
?2?ABA ?
?????????????????
?3?ABAAB ?
?????????????????
?4?ABAABABA …
Run Code Online (Sandbox Code Playgroud) 这是我在 stackover 流中的第一篇文章。最近我开始阅读名为“植物的算法之美”的书,其中在第 1 章中,他解释了 L 系统。(您可以在此处阅读该章节)。
所以据我所知,有两种类型的 L 系统。边缘重写和节点重写。
边缘重写相对非常简单。有一个初始的起始多边形和一个生成器。初始多边形的每条边(边)都将被生成器替换。
但是这个节点重写非常混乱。从我收集到的信息来看,有两个或更多规则,并且每次迭代都将规则中的变量替换为其常量对应项。
对于海龟解释,这些是标准规则
F : Move turtle forward in current direction (initial direction is up)
+ : rotate turtle clock wise
- : rotate turtle anti clock wise
[ : Push the current state of the turtle onto a pushdown operations stack.
The information saved on the stack contains the turtle’s position and orientation,
and possibly other attributes such as the color and width of lines being drawn. …
Run Code Online (Sandbox Code Playgroud) algorithm procedural-programming l-systems procedural-generation fractals
我试图在Haskell中表达一个L系统https://en.m.wikipedia.org/wiki/L-system,特别是Lindenmayer的原始L系统,用于模拟藻类的生长.
变量:AB
常数:无
公理:
规则:(A→AB),(B→A)
对我来说,解决这个问题的自然方法是将规则应用于列表中的每个元素,这对我来说意味着我可以使用某种类型的字符串替换来对解决方案进行建模.
对于"字符"列表[A,B,A我们应用规则并获得[A→AB,B→A,A→AB] = [A,B,A,A,B](对于此模型为了与Haskell很好地配合,你必须将AB视为列表[A,B],我们将与上述规则产生的任何其他结果相结合.
我已经生成了下面的代码,其中包含数据构造函数,不必处理除A或B之外的其他字符,
data Letter = A | B deriving (Show, Eq)
type Alphabet = [Letter]
algae :: Alphabet -> Alphabet
algae = concat . map (\c -> if
| c == A -> A:[B]
| c == B -> [A])
Run Code Online (Sandbox Code Playgroud)
上面的代码是这样的,用自己作为参数调用它会产生预期的结果,即.那
algae $ algae $algae [A] = [A, B, A, A, B]
Run Code Online (Sandbox Code Playgroud)
重复的应用程序按预期工作.
我接下来要完成的是将函数递归地应用到自身,但未能表达这一点.通过这个我的意思是我希望能够调用该函数,无论是algae [A]
或者只是algae
(这需要更改类型签名algae :: Alphabet
),这会产生一个无限的列表,通过将藻类无限次地应用到自身上来获得这个列表.
由于我已经承认失败,我已经查看了http://hackage.haskell.org/package/lindenmayer-0.1.0.0/docs/Lindenmayer-D0L.html但是我无法理解代码,因为它(还)还找到了其他代码同样令人困惑的实施.
我已尽力尝试使用使用folds
和 …