Created
November 9, 2018 15:17
-
-
Save lotz84/492f260bf75588f0eab88b99ee9276d4 to your computer and use it in GitHub Desktop.
カオスゲーム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Graphics.Gloss.Interface.IO.Game | |
import System.Random.MWC | |
vertexes :: [Point] | |
vertexes = [(0, 120), (-160, -120), (160, -120)] | |
type Model = [Point] | |
step :: Model -> IO Model | |
step [] = pure [] -- まだ点が無ければ何もしない | |
step ps@((x, y):_) = do | |
gen <- createSystemRandom -- 乱数のジェネレータを生成 | |
i <- uniformR (0, 2) gen -- ランダムなインデックスを生成 | |
let (vx, vy) = vertexes !! i -- ランダムな頂点の座標 | |
pure $ ((x + vx) / 2, (y + vy) / 2) : ps -- 内分点を計算して追加する | |
handler :: Event -> Model -> IO Model | |
-- 最初に左クリックされた点を開始点とする | |
handler (EventKey (MouseButton LeftButton) Down _ (x, y)) [] = pure $ [(x, y)] | |
handler _ model = pure model -- それ以外の時は何もしない | |
draw :: Model -> IO Picture | |
draw ps = | |
let vCircles = map (\(x, y) -> translate x y $ circleSolid 5) $ vertexes | |
pCircles = map (\(x, y) -> translate x y $ circleSolid 2) $ ps | |
in pure $ mconcat vCircles <> mconcat pCircles | |
main :: IO () | |
main = playIO inWindow white 24 [] draw handler (\_ -> step) | |
where | |
inWindow = InWindow "Haskell Day 2018" (640, 480) (100, 100) |
Author
lotz84
commented
Nov 9, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment