aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-07-03 11:20:22 +0100
committerNat Lasseter <user@4574.co.uk>2024-07-03 11:20:22 +0100
commit7ee9e31e40353daa24d0a7ef325acc9878095325 (patch)
tree6cfdea5fff05d27f9fb0e69b930a450de3858af9
parent9e67a8260fee8f8a423a3131bcf13db3b9de6922 (diff)
Refactor Hittables typeHEADmain
-rw-r--r--hittable.go10
-rw-r--r--rtiaw.go14
2 files changed, 11 insertions, 13 deletions
diff --git a/hittable.go b/hittable.go
index 73e2e3c..b31b205 100644
--- a/hittable.go
+++ b/hittable.go
@@ -69,18 +69,16 @@ func (s Sphere) Hit(r Ray, ray_t Interval) (rec HitRecord) {
//Hittables < Hittable
-type Hittables struct {
- Objects []Hittable
-}
+type Hittables []Hittable
-func (l *Hittables) Add(o Hittable) {
- l.Objects = append(l.Objects, o);
+func (l Hittables) Add(o Hittable) Hittables {
+ return append(l, o);
}
func (l Hittables) Hit(r Ray, ray_t Interval) (rec HitRecord) {
closest_so_far := ray_t.Max;
- for _, o := range l.Objects {
+ for _, o := range l {
temp_rec := o.Hit(r, Interval{ray_t.Min, closest_so_far});
if temp_rec.Valid {
rec = temp_rec;
diff --git a/rtiaw.go b/rtiaw.go
index 2daa826..b0d2106 100644
--- a/rtiaw.go
+++ b/rtiaw.go
@@ -13,7 +13,7 @@ func main() {
var world Hittables;
mat_ground := Lambertian{Vec3{0.5, 0.5, 0.5}};
- world.Add(Sphere{Vec3{0, -1000, 0}, 1000, mat_ground});
+ world = world.Add(Sphere{Vec3{0, -1000, 0}, 1000, mat_ground});
i := Interval{0, 1};
for a := -11; a < 11; a++ {
@@ -29,31 +29,31 @@ func main() {
albedo := Vec3{i.Sample(), i.Sample(), i.Sample()}.
Mul(Vec3{i.Sample(), i.Sample(), i.Sample()});
mat_sphere := Lambertian{albedo};
- world.Add(Sphere{centre, 0.2, mat_sphere});
+ world = world.Add(Sphere{centre, 0.2, mat_sphere});
} else if (choose_mat < 0.95) {
// metal
j := Interval{0.5, 1};
albedo := Vec3{j.Sample(), j.Sample(), j.Sample()};
fuzz := j.Sample() - 0.5;
mat_sphere := Metal{albedo, fuzz};
- world.Add(Sphere{centre, 0.2, mat_sphere});
+ world = world.Add(Sphere{centre, 0.2, mat_sphere});
} else {
// glass
mat_sphere := Dielectric{1.5};
- world.Add(Sphere{centre, 0.2, mat_sphere});
+ world = world.Add(Sphere{centre, 0.2, mat_sphere});
}
}
}
}
mat1 := Dielectric{1.5};
- world.Add(Sphere{Vec3{0, 1, 0}, 1.0, mat1});
+ world = world.Add(Sphere{Vec3{0, 1, 0}, 1.0, mat1});
mat2 := Lambertian{Vec3{0.4, 0.2, 0.1}};
- world.Add(Sphere{Vec3{-4, 1, 0}, 1.0, mat2});
+ world = world.Add(Sphere{Vec3{-4, 1, 0}, 1.0, mat2});
mat3 := Metal{Vec3{0.7, 0.6, 0.5}, 0.0};
- world.Add(Sphere{Vec3{4, 1, 0}, 1.0, mat3});
+ world = world.Add(Sphere{Vec3{4, 1, 0}, 1.0, mat3});
cam := NewCamera(1200, 16.0/9.0,
500, 50,