Skip to content

Instantly share code, notes, and snippets.

@abuiles
Created October 14, 2009 19:02
Show Gist options
  • Save abuiles/210319 to your computer and use it in GitHub Desktop.
Save abuiles/210319 to your computer and use it in GitHub Desktop.
sealed case class Sphere (center:Vector3d , radius: Double) extends Geometry
{
def intersect (r:Ray, h:Hit, range:Range,material:Color3f): Option [(Hit,Range)]=
{
var v = new Vector3d ( r.origin.x - center.x , r.origin.y - center.y,r.origin.z - center.z);
var discriminant = pow (v.dot (r.direction),2.0) - (pow (v.dot(v),2) - pow (radius,2));
if (discriminant >= 0 )
{
//var t1 = -(v.dot(r.direction)) + sqrt (discriminant);
var t = -(v.dot(r.direction)) - sqrt (discriminat);
if (t >= range.minT && t < range.maxT )
{
var h = new Hit (t,material);
range.maxT = t;
return Some (h,range);
}
return None;
}
else
return None;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment