Skip to content

Instantly share code, notes, and snippets.

@tcsavage
Created November 15, 2012 12:10
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 tcsavage/4078346 to your computer and use it in GitHub Desktop.
Save tcsavage/4078346 to your computer and use it in GitHub Desktop.
Sphere intersection snippet
{-
Minimum distance for ray intersection to be considered important.
This is due to limited numerical precision with floating point numbers. A ray could be reflected and then be immediately registered as intersecting with the object it just bounced off.
-}
epsilon :: Scalar
epsilon = 0.1
{-|
Defines a mathematical sphere.
-}
data Sphere = Sphere Vec3 Scalar deriving (Show, Read, Eq, Typeable)
instance Shape Sphere where
intersect ray@(Ray origin dir) ((Sphere center rad), material) = listToMaybe is
where
a = magSquared dir
b = 2 * ( dir `dot` (origin `sub` center))
c = (magSquared (origin `sub` center)) - rad^2
times = filter (> epsilon) (roots a b c)
normal_at_time t = normalize ((positionAtTime ray t) `sub` center)
intersection_at_time t = Intersection (normal_at_time t) (positionAtTime ray t) ray material
is = sortWith (\(t, _) -> t) $ map (\t -> (t,intersection_at_time t)) times
center (Sphere c _) = c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment