aboutsummaryrefslogtreecommitdiff
path: root/hitable.go
blob: a44026dc72d356ffe1af9b06cdd8cf2558c4433a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

package main

type Hitable interface {
	Hit(ray Ray) bool
}

type Sphere struct {
	center Vec3D
	radius float64
}

func (sphere Sphere) Hit(ray Ray) bool {
	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
}