所以我们正在尝试使用 构建一个毕达哥拉斯Tree
,gloss
它失败了 2 级和下一个(仅适用于 0 级和 1 级)。
这是代码:
data FTree a b = Unit b | Comp a (FTree a b) (FTree a b) deriving (Eq,Show)
type PTree = FTree Square Square
type Square = Float
generatePTree n = aux n 100 where
aux :: Int -> Float -> PTree
aux 0 x = Unit x
aux n x = Comp x (aux (n-1) (x * (sqrt(2)/2))) (aux (n-1) (x * (sqrt(2)/2)))
drawPTree :: PTree -> [Picture]
drawPTree p = aux p (0,0) 0 where
aux :: PTree -> (Float, Float) -> Float -> [Picture]
aux (Unit c) (x,y) ang = [Translate x y (Rotate ang (square c))]
aux (Comp c l r) (x,y) ang = [Translate x y (Rotate ang (square c))]++(aux l (x - somaX c,y + somaY c) (ang - 45)) ++ (aux r (x + somaX c,y + somaY c) (ang + 45))
where somaX c = c/2
somaY c = c + sqrt(((c * (sqrt 2))/4)^2 - ((sqrt (c^2 + c^2)) / 4)^2)
window = (InWindow "CP" (800,800) (0,0))
square s = rectangleSolid s s
main = animate window white draw
where
pics = drawPTree (generatePTree 2)
draw t = Pictures $ pics