2023年6月20日火曜日

コッホ曲線



import HTurtle
import Data.Complex
main = do
    start
    koch ((0,0),(400,0)) 4
    end
koch ((a,b),(c,d)) n = do
    if n == 0   then doKoch >> return ()
                else do koch (p0,p1) (n-1)
                        koch (p1,p2) (n-1)
                        koch (p2,p3) (n-1)
                        koch (p3,p4) (n-1)
    where   al = a :+ b
            be = c :+ d
            p0 = (a,b)
            p1 = let (a :+ b) = c1 in (a,b)
            p2 = let (a :+ b) = c2 in (a,b)
            p3 = let (a :+ b) = c3 in (a,b)
            p4 = (c,d)
            c1 = cid (al,be) 1 2
            c2 = sumit (c1,c3) (pi / 3)
            c3 = cid (al,be) 2 1
            doKoch = do setPos p0
                        setPos p1
                        setPos p2
                        setPos p3
                        setPos p4
cid (a,b) m n = (n * a + m * b) / (m + n)
sumit (a,b) th = (b - a) * (cos th :+ sin th) + a


0 件のコメント:

コメントを投稿