Created
May 24, 2021 19:52
-
-
Save leftaroundabout/1731b244584d96e2071a05ac0f350203 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Diagrams.Prelude | |
import Diagrams.Backend.Cairo | |
import Data.Colour.RGBSpace.HSV | |
import Data.Colour.SRGB.Linear | |
import Data.Numbers.Primes | |
import Control.Monad | |
import Text.Printf | |
trajectory :: Int -> Double -> P2 Double | |
trajectory i t = cos (t*k*pi) ^& sin (t*k'*pi) | |
where (k:k':_) = fromIntegral<$>(drop i primes :: [Int]) | |
droplet :: Double -> P2 Double -> Diagram B | |
droplet r p@(P (V2 _ y)) | |
= circle r | |
& moveTo p | |
& fc ptcColour | |
& lc ptcColour | |
where ptcColour = rgb r g b | |
where RGB r g b = hsv ((y+1)*160) 1 1 | |
srate :: Num n => n | |
srate = 200 | |
rainbowsnap :: Double -> Diagram B | |
rainbowsnap t | |
= mconcat [ droplet 0.05 (trajectory (round $ 12 - 0.3*j) $ tanh (t'*(4+j*3))) | |
| j <- [0 .. 30] | |
, t' <- let oversampling = 40 | |
in [t + j/(oversampling*srate) | j<-[1..oversampling]] ] | |
<> (rect 5 3 & fc black) | |
main :: IO () | |
main = do | |
forM_ [0 .. 2*srate] $ \i -> do | |
let t = fromIntegral (i :: Int) / srate - 1 | |
renderCairo | |
(printf "rainbow_%03i.png" i) | |
(dims $ 300 ^& 180) | |
(rainbowsnap t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://physics.stackexchange.com/a/638745/3540