gloss で Turtle Graphics

Turtle Graphics on Gloss

Gloss を使った Turtle Graphics を作ってみました。

実際のコードは こちらの github にあります。

Gloss については こちら の記事が参考になる と思います。

含まれるファイル

app / Main.hs               -- 実験用

sample
├─ circle / Main.hs         -- 複数の円を同時に描く
├─ dragonCurve / Main.hs    -- ドラゴン曲線を描く
├─ koch / Main.hs           -- Koch 曲線を描く
├─ spiral / Main.hs         -- らせんを描く
└─ star / Main.hs           -- 星を描く

src / Graphics / Turtle.hs  -- Turtle Graphics 本体

Sample の実行法

  • stack build を実行することで sample ディレクトリ以下にあるサンプルがコン パイルされ、 Circle, Spiral, Koch, DragonCurve, Star といったファイルが 作られます。
  • stack exec Circlestack exec Spiral を実行すると図形が描画されます。

circle

Dragon_Curve

Koch_Curve

Turtle Graphics の使い方

使用例を示します。

import           Graphics.Gloss
import           Graphics.Turtle


main :: IO ()
main = runTurtle window white 20 [(st, cmdLst)]
  where
    window = InWindow "test" (800, 600) (10, 10)
    st = initST
    cmdLst = [forward 200, left 120, forward 200, left 120, forward 200]

runTurtle の型は次のようになってます。

runTurtle :: Display                -- gloss の描画モード
          -> Color                  -- gloss の背景色
          -> Int                    -- 1 秒あたりのステップ数
          -> [(TurtleST, [Command]) -- 亀のデータ
          -> IO ()

Display および Colorglosssimulate 関数に渡すものです。
詳しくは こちら を参照してください。

TurtleST は亀の初期値です。

data TurtleST = TurtleST { angle    :: Float -- ^ 亀の向き
                         , point    :: Point -- ^ 亀の位置
                         , penColor :: Color -- ^ ペンの色
                         , pen      :: Bool  -- ^ up or down
                         , mark     :: Bool  -- ^ 亀のマーク
                         } deriving Show

TurtleSTinitST で自動的に設定されますが、自分で設定することも可能です。

initST = TurtleST { angle    = 0
                  , point    = (0, 0)
                  , penColor = black
                  , pen      = True
                  , mark     = True }

Command は亀に実行させる命令です。

Turtle Graphics のコマンド

各コマンドの型は次のようになっています。

type PrimitiveCommand = TurtleST -> (Picture, TurtleST)

type Command = [PrimitiveCommand]

PrimitiveCommandTurtleST を受け取り、図形 (Picture) とコマンド実行後の TurtleST を返します。

CommandPrimitiveCommand のリストになっており、通常はこちらを使用します。

forward n, fd n

亀が n だけ前進する。 ペンが下りていれば線を描く。

quickForward n, qf n

forward より高速に前進する。

backward n, bk n

亀が n だけ後退する。 ペンが下りていれば線が描く。

left th, lt th

亀が th 度だけ左旋回する。

quickLeft th, ql th

left より高速に左旋回する。

right th, rt th

亀が th 度だけ右旋回する。

quickRight th, qr th

right より高速に右旋回する。

goto p

亀が p の位置へ移動する。亀の向きは変らない。ペンが下りていれば線を描く。

penDown, pd

亀のペンを下げる。亀が移動すると線が描かれる。

penUp, pu

亀のペンを上げる。亀が移動しても線は描かれない。

setAngle th

亀の向きを th 度に設定する。

setPoint p

亀の位置を p に設定する。

setColor c

亀のペンの色を c に設定する。

drawPolygonL n m

一辺の長さが m の正 n 角形を左回りに描く

drawPolygonR n m

一辺の長さが m の正 n 角形を右回りに描く

drawCircle r

亀の位置を中心に、半径 r の円を描く。

drawArcL th r

中心角 th , 半径 r の円弧を左回りに描く。

drawArcR th r

中心角 th , 半径 r の円弧を右回りに描く。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account