This preview shows pages 1–2. Sign up to view the full content.

// This file contains the definition of the class sphere / #include "Sphere.h" #include "math.h" # const double Sphere::kEpsilon = 0.001; c // ---------------------------------------------------------------- default constructor c Sphere::Sphere(void) S : GeometricObject(), center(0.0), radius(1.0) {} { // ---------------------------------------------------------------- constructor / Sphere::Sphere(Point3D c, double r) : GeometricObject(), center(c), radius(r) {} { // ---------------------------------------------------------------- clone / GeometricObject* Sphere::clone(void) const { return (new Sphere(*this)); } // ---------------------------------------------------------------- copy constructor c : GeometricObject(sphere), center(sphere.center), radius(sphere.radius) {} { // ---------------------------------------------------------------- assignment operator o Sphere& S { return (*this); r GeometricObject::operator= (rhs);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: G center = rhs.center; radius = rhs.radius; = return (*this); } // ---------------------------------------------------------------- destructor / Sphere::~Sphere(void) {} S //---------------------------------------------------------------- hit / bool Sphere::hit(const Ray& ray, double& tmin, ShadeRec& sr) const { double t; Vector3D temp = ray.o - center; double a = ray.d * ray.d; double b = 2.0 * temp * ray.d; double c = temp * temp - radius * radius; double disc = b * b - 4.0 * a * c; = if (disc < 0.0) return(false); else { e double e = sqrt(disc); double denom = 2.0 * a; t = (-b - e) / denom; // smaller root t if (t > kEpsilon) { tmin = t; sr.normal = (temp + t * ray.d) / radius; sr.local_hit_point = ray.o + t * ray.d; return (true); } } t = (-b + e) / denom; // larger root t if (t > kEpsilon) { tmin = t; sr.normal = (temp + t * ray.d) / radius; sr.local_hit_point = ray.o + t * ray.d; return (true); } } return (false); }...
View Full Document