Last active
August 29, 2015 14:09
-
-
Save cfr/eb60df03439aa754ff2b to your computer and use it in GitHub Desktop.
Simple torus distribution
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
. |
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
function randr(min, max) { | |
return Math.random() * (max - min) + min; | |
}; | |
function torusDistribPoint(radius, size) { | |
var l = randr(size - radius, size + radius); | |
var a = randr(0, 2*Math.PI); | |
return [l * Math.cos(a), l * Math.sin(a)]; | |
} |
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
all: plot | |
torus: torusdistr.hs | |
ghc --make torusdistr.hs | |
plot: torus | |
./torusdistr > data | |
gnuplot plot.gpl |
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
set terminal png size 400,400 enhanced font "Helvetica,6" | |
set output 'torus.png' | |
set autoscale | |
plot "data" using 1:2 |
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
{-# LANGUAGE ScopedTypeVariables #-} | |
import System.Random (randomR, getStdGen, StdGen) | |
import Control.Monad (mapM_, replicateM) | |
import Control.Monad.State | |
torusDist gen n radius size = transform $ zip xs ys | |
where (xs, gen') = randoms n (0, 2*pi) gen | |
(ys, _) = randoms n (size - radius, size + radius) gen' | |
transform :: Floating a => [(a, a)] -> [(a, a)] | |
transform [] = [] | |
transform ((x, y):ts) = (a, b) : transform ts | |
where a = y * sin x | |
b = y * cos x | |
randoms n i = runState (replicateM n (state (randomR i))) | |
main = do gen <- getStdGen | |
let points :: [(Double, Double)] = torusDist gen 100 0.1 0.5 | |
mapM_ print points | |
where print (x, y) = put x >> put y >> putStr "\n" | |
put x = putStr (show x) >> putStr " " | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment