Generate random bodies
This commit is contained in:
parent
35c0cc4446
commit
49116785ae
2 changed files with 40 additions and 8 deletions
47
app/Main.hs
47
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]
|
||||
|
|
|
@ -27,6 +27,7 @@ executable orbit-exe
|
|||
build-depends: base
|
||||
, orbit
|
||||
, linear
|
||||
, random
|
||||
, gloss
|
||||
default-language: Haskell2010
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue