Generate random bodies

This commit is contained in:
Dimitri Lozeve 2017-07-25 23:38:11 +02:00
parent 35c0cc4446
commit 49116785ae
2 changed files with 40 additions and 8 deletions

View file

@ -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]

View file

@ -27,6 +27,7 @@ executable orbit-exe
build-depends: base
, orbit
, linear
, random
, gloss
default-language: Haskell2010