From fac591fb5134aada58a1ec06ebcb1913c817b233 Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Tue, 14 Nov 2017 00:03:16 +0000 Subject: [PATCH] Use Gloss for real-time visualization --- app/Main.hs | 36 +++++++++++++++++++++++++++++++++--- orbit.cabal | 1 + 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index db98868..5577a57 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -8,6 +8,8 @@ import Linear.V3 import System.Random import Control.Monad (replicateM) +import Graphics.Gloss hiding (Point) + -------------------------------------------------------------------------------- -- Random body generation @@ -76,8 +78,36 @@ csvFromInit :: Int -- ^ The number of time steps to keep -> String -- ^ CSV data csvFromInit n dt theta b = concat $ map (uncurry csvFromBodies) (take n $ steps dt theta b) + +-------------------------------------------------------------------------------- +-- Gloss +-------------------------------------------------------------------------------- + +width, height, offset :: Int +width = 1000 +height = 750 +offset = 100 + +window :: Display +window = InWindow "Orbit" (width, height) (offset, offset) + +displayBody :: Body -> Picture +displayBody b = translate (realToFrac x) (realToFrac y) $ circle (realToFrac (_bodyRadius b)) + where P (V3 x y _) = _bodyPosition b + +displayBodies :: [Body] -> Picture +displayBodies = color white . Pictures . map displayBody + +drawing :: Picture +drawing = color white $ circle 80 + main :: IO () main = do - bodies <- replicateM 100 randomBody - putStrLn $ csvFromInit 10000 (60*20) 0.5 bodies - + bodies <- replicateM 300 randomBody + simulate + window + black + 25 + bodies + displayBodies + (\_ dt bs -> updateAll (realToFrac dt*1e6) 0.5 bs) diff --git a/orbit.cabal b/orbit.cabal index cbde683..f45e4f3 100644 --- a/orbit.cabal +++ b/orbit.cabal @@ -31,6 +31,7 @@ executable orbit-exe , orbit , linear , random + , gloss default-language: Haskell2010 test-suite orbit-test