aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--color.go5
-rw-r--r--hitable.go19
-rw-r--r--main.go3
3 files changed, 25 insertions, 2 deletions
diff --git a/color.go b/color.go
index 2bee0af..3514be6 100644
--- a/color.go
+++ b/color.go
@@ -16,7 +16,10 @@ func LinearBlend(color1 Color, color2 Color, param float64) Color {
}
}
-func RayColor(ray Ray) Color {
+func RayColor(ray Ray, hitable Hitable) Color {
+ if hitable.Hit(ray) {
+ return Color{1.0, 0.0, 0.0}
+ }
param := 0.5 * (ray.direction.Unit().y + 1)
return LinearBlend(Color{0.5, 0.7, 1.0}, Color{1.0, 1.0, 1.0}, param)
}
diff --git a/hitable.go b/hitable.go
new file mode 100644
index 0000000..a44026d
--- /dev/null
+++ b/hitable.go
@@ -0,0 +1,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
+}
diff --git a/main.go b/main.go
index 981a324..a1cb2c9 100644
--- a/main.go
+++ b/main.go
@@ -8,12 +8,13 @@ func main() {
ORIGIN := Vec3D{0.0, 0.0, 0.0}
HORIZONTAL := Vec3D{4.0, 0.0, 0.0}
VERTICAL := Vec3D{0.0, 2.0, 0.0}
+ SPHERE := Sphere{Vec3D{0.0, 0.0, -1.0}, .5}
frame := Frame{}
for x := 0; x < HEIGHT; x++ {
for y := 0; y < WIDTH; y++ {
u, v := float64(y)/float64(WIDTH), float64(x)/float64(HEIGHT)
ray := Ray{ORIGIN, AddVec3D(AddVec3D(LOWER_LEFT_CORNER, ScalarProduct(u, HORIZONTAL)), ScalarProduct(v, VERTICAL))}
- color := RayColor(ray)
+ color := RayColor(ray, SPHERE)
frame[HEIGHT-1-x][y] = color.ToPixel()
}
}