diff options
| author | Martin Lehoux <martin@lehoux.net> | 2020-04-04 15:40:11 -0700 |
|---|---|---|
| committer | Martin Lehoux <martin@lehoux.net> | 2020-04-04 15:40:11 -0700 |
| commit | 1008f463687a373f5c5d4024d25a09125b6c49ec (patch) | |
| tree | 32c05771f90cb72340bb9f51ab4ad61441332f0c /hitable.go | |
| parent | 19124e8c21b86dccb9e9d874c614aacb05c0fd4a (diff) | |
feat - Use HitRecord
Diffstat (limited to 'hitable.go')
| -rw-r--r-- | hitable.go | 24 |
1 files changed, 21 insertions, 3 deletions
@@ -1,7 +1,16 @@ package main +import "math" + +type HitRecord struct { + time float64 + point Vec3D + normal Vec3D +} + type Hitable interface { - Hit(ray Ray) bool + Hit(ray Ray) HitRecord + Normal(point Vec3D) Vec3D } type Sphere struct { @@ -9,11 +18,20 @@ type Sphere struct { radius float64 } -func (sphere Sphere) Hit(ray Ray) bool { +func (sphere Sphere) Normal(point Vec3D) Vec3D { + return (SubVec3D(point, sphere.center)).Unit() +} + +func (sphere Sphere) Hit(ray Ray) HitRecord { oc := SubVec3D(ray.origin, sphere.center) a := DotProduct(ray.direction, ray.direction) b := 2.0 * DotProduct(oc, ray.direction) c := DotProduct(oc, oc) - sphere.radius*sphere.radius discriminant := b*b - 4*a*c - return discriminant > 0 + if discriminant < 0 { + return HitRecord{} + } + time := (-b - math.Sqrt(discriminant)) / 2.0 / a + point := ray.PointAt(time) + return HitRecord{time, point, SubVec3D(point, sphere.Normal(point))} } |
