Pattern substitution fractals, which can usually be expressed using the L system, think that those snowflake curves and the like are pattern substitution fractals. L system refers to a sequence of characters in which certain sub-sequences can be replaced by rules with specific sequences (usually longer), and different characters in the sequence have different meanings. such as changing the direction of drawing and drawing a line segment and so on.
The L system allows you to draw very close to the real plant and, of course, to draw other curves.
Package Mainimport ("Github.com/hydra13142/paint" "Image" "Image/color" "Image/png" "OS") const (D = 6) var (X, Y, N = 6, 1536, 0 img = paint. Image{image. Newrgba (image. Rect (0, 0, 1543, 1543)), color. RGBA{50, 255}, color. RGBA{50, +, 255}}) type Lsystem struct {rp map[byte][]byte do map[byte]func () Ch Chan byte}func Newlsystem ( A map[byte]string, B map[byte]func ()) *lsystem {c: = Make (Map[byte][]byte, Len (a)) for k, V: = Range a {c[k ] = []byte (v)} return &lsystem{c, B, Nil}}func (L *lsystem) Init (s string, n int) {step: = Func (in <-cha n byte) Chan byte {ex: = Make (chan bytes) go func () {for {c, OK: = <-in If!ok {break} s, OK: = L.rp[c] If!ok { Ex <-C} else {for _, c = Range S {ex <-C }}}} close (ex)} () return ex} var p, Q Chan byte p = Make (chan byte) for q = p; n > 0; n--{q = step (q)} l.ch = Q go func () {for _, c: = range []byte (s) {P <-c } Close (P)} ()}func (L *lsystem) Run () {for {c, OK: = <-l.ch if!ok {break } F, OK: = L.do[c] If OK {f ()}}}func main () {//Draw background-color img. Bar (0, 0, 1542, 1542) Draw: = Func () {var x, y int switch N% 4 {Case 1, -3:x, y = x , Y+d case 2, -2:x, y = x-d, y case 3, -1:x, y = x, y-d case 0:x, y = x+d, y} img. Line (x, y, x, y) x, y = x, y} lsys: = Newlsystem (map[byte]string{' L ': "+rf-lfl-fr+", ' R ': "-lf+rfr+fl-",}, Map[byte]func () {' + ': func () { n++}, '-': Func () {n--}, ' F ': func () {Draw ()},},/* If replaced by the following two parameters, the dragon will be drawn Shape curve map[byte]string{' A ': "-a+b+a-b", ' B ': "a+b-a-b+",}, M Ap[byte]func () {' + ': func () {n++}, '-': Func () {n--}, ' A ': func () {Draw ( }, ' B ': func () {Draw ()},}, */) Lsys. Init ("R", 8) Lsys. Run ()//write file Save F, E: = os. Create ("Frac.png") if E! = nil {println ("error")} defer F.close () PNG. Encode (F, IMG)}
The above is a pattern matching fractal example, the picture is still very beautiful, you can make wallpaper/desktop, as follows:
In this small program, I used a series of pipeline to do multiple mode substitution, if it is C + + and other languages, you need to use a similar task queue way to handle (or also use multithreading). Go can be piped and can save a lot of memory.
Another fractal is an iterative fractal. This fractal is not strictly self-similar, but often more beautiful.
Package Mainimport ( "image" "Image/color" "image/png" //"math" "OS") Func repeat (i, j int) color . RGBA { x: = Float64 (i-3500)/ y: = float64 (j-2500)/+ A: = 0.0 B: = 0.0 for t: = 0; t < 25 6; t++ { M: = A * a N: = b * b o: = A * b a = m-n + x b = o + O + y if m+n > 4 { retu RN Color. Rgba{uint8 (t), uint8 (t), uint8 (t), 255}}} return color. rgba{255, 255, 255, 255}}func main () { file, _: = OS. Create ("Mdb.png") defer file. Close () img: = image. Newrgba (image. Rect (0, 0, defer)) is png. Encode (File, img) for i: = 0, I <, i++ { for J: = 0, J < Z, J + + { c: = Repeat (i, j) IMG. Set (I, J, c)}}}
The most common form of iterative fractals.
The following is part of the drawing result (the complete graph is much larger than this)
A little memory of fractal algorithm