diff --git a/wtracer.cpp b/wtracer.cpp index 6aa422f..5f7e7c5 100644 --- a/wtracer.cpp +++ b/wtracer.cpp @@ -4,7 +4,18 @@ #include "vec3.h" #include "ray.h" +bool hit_sphere(const Point3& center, double radius, const Ray& r) { + Vec3 oc = r.origin() - center; + auto a = dot(r.direction(), r.direction()); + auto b = 2.0 * dot(oc, r.direction()); + auto c = dot(oc, oc) - radius * radius; + auto disciminant = b*b - 4*a*c; + return disciminant > 0; +} + Color ray_color(const Ray& r) { + if (hit_sphere(Point3(0, 0, -1), 0.5, r)) + return Color(1, 0, 0); Vec3 unit_direction = unit_vector(r.direction()); auto t = 0.5 * (unit_direction.y() + 1.0); return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0);