From 49116785ae43a466600a8353ba8a4ba01888c77b Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Tue, 25 Jul 2017 23:38:11 +0200 Subject: [PATCH] Generate random bodies --- app/Main.hs | 47 +++++++++++++++++++++++++++++++++++++++-------- orbit.cabal | 1 + 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 05ac5c1..c8fcfe7 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -5,9 +5,38 @@ import Lib import Linear.Affine import Linear.V3 +import System.Random +import Control.Monad (replicateM) + import Graphics.Gloss hiding (Point) +-------------------------------------------------------------------------------- +-- Random body generation +-------------------------------------------------------------------------------- + +randomBody :: IO Body +randomBody = do + r <- randomIO :: IO Double + m <- randomIO :: IO Double + x <- randomIO :: IO Double + y <- randomIO :: IO Double + z <- randomIO :: IO Double + vx <- randomIO :: IO Double + vy <- randomIO :: IO Double + vz <- randomIO :: IO Double + -- Make radius proportional to mass for visualization + let radius = 20 * m + -- Scale mass + let mass = 1e3 * m + -- Scale position and speed + let posx = 1e3 * (2*x - 1) + let posy = 1e3 * (2*y - 1) + let speedx = 5e-5 * vx + let speedy = 5e-5 * vy + return $ Body "random" radius mass (P $ V3 posx posy 0) (V3 speedx speedy 0) + + -------------------------------------------------------------------------------- -- CSV export -------------------------------------------------------------------------------- @@ -44,7 +73,7 @@ window :: Display window = InWindow "Orbit" (width, height) (offset, offset) displayBody :: Body -> Picture -displayBody b = translate (realToFrac x/1e9) (realToFrac y/1e9) $ circle (realToFrac (bodyRadius b)/1e8) +displayBody b = translate (realToFrac x) (realToFrac y) $ circle (realToFrac (bodyRadius b)) where P (V3 x y _) = bodyPosition b displayBodies :: [Body] -> Picture @@ -54,13 +83,15 @@ drawing :: Picture drawing = color white $ circle 80 main :: IO () -main = simulate - window - black - 25 - [sun, earth, moon, mercury, venus, mars] - displayBodies - (\_ dt bs -> updateAll (realToFrac dt*1e6) bs) +main = do + bodies <- replicateM 300 randomBody + simulate + window + black + 25 + bodies + displayBodies + (\_ dt bs -> updateAll (realToFrac dt*1e6) bs) --main :: IO () --main = steps 1000000 10 [sun, earth, moon, mercury, venus, mars] diff --git a/orbit.cabal b/orbit.cabal index eed3753..07d3961 100644 --- a/orbit.cabal +++ b/orbit.cabal @@ -27,6 +27,7 @@ executable orbit-exe build-depends: base , orbit , linear + , random , gloss default-language: Haskell2010