Skip to content

Instantly share code, notes, and snippets.

@crabmusket
Last active December 16, 2015 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crabmusket/5479731 to your computer and use it in GitHub Desktop.
Save crabmusket/5479731 to your computer and use it in GitHub Desktop.
Exponentially scaling distances for rendering objects that are far away using an imprecise floating Z buffer.
module Main where
import Graphics.Gnuplot.Simple
-- Calculates the scaling factor for an object at distance 'dist'. The object
-- will be at scale 1 until 'max'/2, and then will be exponentially scaled down
-- as it increases. As 'dist' approaches infinity, 'dist * factor max dist'
-- approaches 'max'. See http://goo.gl/xLzrv
factor max dist = if dist < max / 2
then 1
else (1 - 1 / 2**(f+1)) / f
where f = dist / max
-- Calculates the visual angular diameter of an object from its distance and
-- actual diameter. From http://en.wikipedia.org/wiki/Angular_diameter
angularSize diam dist = 2 * atan (diam / 2 / dist)
-- Convenience functions.
newDist max dist = dist * factor max dist
newDiam max diam dist = diam * factor max dist
newAngularSize max diam dist = angularSize (newDiam max diam dist) dist
-- Some constants
maxDist = 200 :: Float
initDiam = 1 :: Float
-- Main plotting function: demonstrates that the angular diameters of the object
-- with and without scaling are the same. The angular diameter with scaling is
-- plotted one unit above that without. See an output example at
-- http://i.imgur.com/OuFzphF.png
plotBoth = plot [
1 + newAngularSize maxDist initDiam,
angularSize initDiam
]
-- And other plots, in case you want them.
plotWith = plot [newAngularSize maxDist initDiam]
plotWithout = plot [angularSize initDiam]
-- Common Gnuplot stuff.
plot = plotFuncs [
Title "Apparent angular diameter",
XLabel "Distance",
YLabel "Angular diameter",
Key (Just ["off"])
] (linearScale 200 (1, 100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment