Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- poissonDisc :: (PrimMonad m, HasBoundingBox boundingBox) => Gen (PrimState m) -> boundingBox -> Double -> Int -> m [Vec2]
- uniformlyDistributedPoints :: (PrimMonad m, HasBoundingBox boundingBox) => Gen (PrimState m) -> boundingBox -> Int -> m (Vector Vec2)
- gaussianDistributedPoints :: (PrimMonad m, HasBoundingBox boundingBox) => Gen (PrimState m) -> boundingBox -> Mat2 -> Int -> m (Vector Vec2)
- rejection :: (PrimMonad m, HasBoundingBox boundingBox) => Gen (PrimState m) -> boundingBox -> (Vec2 -> Double) -> Int -> m (Vector Vec2)
Poisson-Disc sampling
:: (PrimMonad m, HasBoundingBox boundingBox) | |
=> Gen (PrimState m) | RNG from mwc-random. |
-> boundingBox | Region to generate points in |
-> Double | Radius around each point no other points are genereted. Smaller values yield more points. |
-> Int | \(k\) parameter: per point, how many attempts should be made to find an empty spot? Typical values: 3-10. Higher values are slower, but increase result quality. |
-> m [Vec2] |
Sample points using the Poisson Disc algorithm, which yields a visually uniform distribution. This is opposed to uniformly distributed points yield clumps and empty areas, which is often undesirable for generative art.
(image code)
>>>
:{
haddockRender "Geometry/Algorithms/Sampling/PoissonDisc/poisson_disc.svg" 300 300 $ \_ -> do let points = runST $ do gen <- MWC.create poissonDisc gen (shrinkBoundingBox 30 [zero, Vec2 300 300]) 10 4 for_ (zip [0..] points) $ \(i,p) -> do setColor (mma i) sketch (Circle p 2) fill :} Generated file: size 115KB, crc32: 0x43236d3e
Other distributions
uniformlyDistributedPoints Source #
:: (PrimMonad m, HasBoundingBox boundingBox) | |
=> Gen (PrimState m) | RNG from mwc-random. |
-> boundingBox | Region to generate points in |
-> Int | Number of points |
-> m (Vector Vec2) |
Generate uniformly distributed points.
(image code)
>>>
:{
haddockRender "Geometry/Algorithms/Sampling/uniform.svg" 300 300 $ \_ -> do let numPoints = 1000 bb = shrinkBoundingBox 30 [zero, Vec2 300 300] points = runST $ do gen <- MWC.create uniformlyDistributedPoints gen bb numPoints V.iforM_ points $ \i p -> do setColor (mma i) sketch (Circle p 2) fill :} Generated file: size 263KB, crc32: 0x29e19f0e
gaussianDistributedPoints Source #
:: (PrimMonad m, HasBoundingBox boundingBox) | |
=> Gen (PrimState m) | RNG from mwc-random. |
-> boundingBox | Determines width and height. The center of this is the mean \(\mathbf\mu\). |
-> Mat2 | Covariance matrix \(\mathbf\Sigma\). |
-> Int | Number of points. |
-> m (Vector Vec2) |
Generate Gaussian/normal distributed points.
Note: This is a rejection algorithm which discards samples outside of the
BoundingBox
. If you choose the covariance much larger than the height or
width, performance will deteriorate as more and more points are rejected.
(image code)
>>>
:{
haddockRender "Geometry/Algorithms/Sampling/gaussian.svg" 300 300 $ \_ -> do let numPoints = 1000 bb = shrinkBoundingBox 30 [zero, Vec2 300 300] points = runST $ do gen <- MWC.create gaussianDistributedPoints gen bb (30 *. mempty) numPoints V.iforM_ points $ \i p -> do setColor (mma i) sketch (Circle p 2) fill :} Generated file: size 267KB, crc32: 0xdffc4138
:: (PrimMonad m, HasBoundingBox boundingBox) | |
=> Gen (PrimState m) | RNG from mwc-random. |
-> boundingBox | Area to sample in |
-> (Vec2 -> Double) | Distribution \(w(\mathbf p) \in [0\ldots 1]\). |
-> Int | Number of points |
-> m (Vector Vec2) |
Sample a distribution via rejection sampling: pick a random coordinate, check whether the distribution’s value exceeds that value (accept the sample) or retry.
The distribution does not need to be normalized, but its values should be reasonably close to 1 for part of the domain, or the algorithm will take a long time to sample the points.
(image code)
>>>
:{
haddockRender "Geometry/Algorithms/Sampling/rejection.svg" 300 300 $ \_ -> do let numPoints = 1000 bb = shrinkBoundingBox 30 [zero, Vec2 300 300] distribution p = let r = norm (p -. Vec2 150 150) in gaussianFalloff 75 20 r points = runST $ do gen <- MWC.create rejection gen bb distribution numPoints V.iforM_ points $ \i p -> do setColor (mma i) sketch (Circle p 2) fill :} Generated file: size 263KB, crc32: 0x38511a1