blob: 144311074f9b89dad4a7c2e388084e7616c3a512 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package main
import "math"
type HitRecord struct {
time float64
point Vec3D
normal Vec3D
}
type Hitable interface {
Hit(ray Ray) HitRecord
Normal(point Vec3D) Vec3D
}
type Sphere struct {
center Vec3D
radius float64
}
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
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))}
}
|