对称的自相似平面图形
曼德博集合以数学家曼德博命名,它是复数平面组成分形的点的集合。 可以使用复二次多项式进行迭代计算。
f_c(z) = z^2 + c
它最大的特点就是自相似。 对于大多数分形软件,内部已经有比较成熟的代码。
如下,我们通过判断是否处理绘画区域决定是否绘制。
func inMandelbrot(x0 float64, y0 float64, n int) bool {
var (
x float64 = 0.0
y float64 = 0.0
)
for n > 0 {
xtemp := x*x - y*y + x0
y = 2.0*x*y + y0
x = xtemp
n = n - 1
if x*x+y*y > 4.0 {
return false
}
}
return true
}
并依据规则生成图形代码
for y >= ys {
x := xs
lineStr := []string{}
for x < xe {
n += 1
if inMandelbrot(x, y, int(threshhold)) {
lineStr = append(lineStr, "*")
} else {
lineStr = append(lineStr, ".")
}
x += dx
}
if i == 0 {
lines := fmt.Sprintf("%v", strings.Join(lineStr, ""))
endStr = append(endStr, lines)
} else {
stwo := ChangePlace(lineStr)
lines := fmt.Sprintf("%v", strings.Join(stwo, ""))
endStrTwo = append(endStrTwo, lines)
}
y -= dy
}
最后组合成我们期望的对称图形。
小结
其中的算法乐趣很多,有兴趣的可以找资料来看看。
代码路径
https://github.com/hahamx/examples/blob/main/alg_practice/0_mandebot/main.go
结果例子
https://github.com/hahamx/examples/blob/main/alg_practice/0_mandebot/md.txt