From 35c0cc4446105ee0a3fb71a3d51259f53692e16c Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Tue, 25 Jul 2017 23:19:18 +0200 Subject: [PATCH] Refactor and reformat --- app/Main.hs | 26 ++++++++++++++++++-------- src/Lib.hs | 15 +++++---------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index dd11faa..05ac5c1 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -7,24 +7,34 @@ import Linear.V3 import Graphics.Gloss hiding (Point) + +-------------------------------------------------------------------------------- +-- CSV export +-------------------------------------------------------------------------------- + csvFromPoint :: Point V3 Double -> String csvFromPoint (P (V3 x y z)) = show x ++ "," ++ show y ++ "," ++ show z csvFromBodies :: [Body] -> [String] -csvFromBodies [] = [] -csvFromBodies (x:xs) = - (bodyName x ++ "," - ++ (show $ bodyMass x) ++ "," - ++ (csvFromPoint $ bodyPosition x) ++ "\n") - :(csvFromBodies xs) +csvFromBodies = + map (\ x -> + bodyName x ++ + "," ++ + show (bodyMass x) ++ + "," ++ csvFromPoint (bodyPosition x) ++ "\n") steps :: Int -> Double -> [Body] -> IO () steps 0 _ _ = return () steps n dt bodies = do - putStr . concat $ map ((++) (show n ++ ",")) $ csvFromBodies bodies + putStr . concat $ map ((show n ++ ",") ++) $ csvFromBodies bodies steps (n-1) dt (updateAll dt bodies) + +-------------------------------------------------------------------------------- +-- Gloss +-------------------------------------------------------------------------------- + width, height, offset :: Int width = 1000 height = 750 @@ -38,7 +48,7 @@ displayBody b = translate (realToFrac x/1e9) (realToFrac y/1e9) $ circle (realTo where P (V3 x y _) = bodyPosition b displayBodies :: [Body] -> Picture -displayBodies = (color white) . Pictures . (map displayBody) +displayBodies = color white . Pictures . map displayBody drawing :: Picture drawing = color white $ circle 80 diff --git a/src/Lib.hs b/src/Lib.hs index d851fb6..aacd2ec 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -1,11 +1,6 @@ module Lib ( gravity, - Body, - bodyName, - bodyRadius, - bodyMass, - bodyPosition, - bodySpeed, + Body(..), bodyDistance, field, acceleration, @@ -55,14 +50,14 @@ bodyDistance body1 body2 = -- Field created by a body on a certain position field :: Body -> Point V3 Double -> V3 Double field body pos = - unP $ (gravity * m / r**2) *^ (normalize vec) + unP $ (gravity * m / r**2) *^ normalize vec where m = bodyMass body - vec = (bodyPosition body) - pos + vec = bodyPosition body - pos r = norm vec -- Acceleration given to a body by its neighbours acceleration :: Body -> [Body] -> V3 Double -acceleration body = foldr f (fromInteger 0) +acceleration body = foldr f 0 where f neighbour acc = acc + field neighbour (bodyPosition body) @@ -81,7 +76,7 @@ update dt (Body name r m pos speed) neighbours = -- Update all bodies with a timestep dt updateAll :: Double -> [Body] -> [Body] -updateAll dt bodies = aux [] [] bodies +updateAll dt = aux [] [] where -- Cycles through all bodies, updates each one and stores it in -- res. Each body already updated is moved to prev.