aboutsummaryrefslogtreecommitdiff
path: root/lib/vec3.rb
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-06-12 22:51:25 +0100
committerNat Lasseter <user@4574.co.uk>2024-06-12 22:51:25 +0100
commit1bc4829392b3c91737fbe4ddccd81098ac9d8a12 (patch)
treec2339a79b1fdaf82955a5e38ed35a7f0a9481f21 /lib/vec3.rb
parent51416f5dfac39b6ede8340b57b9bed4eb3edc646 (diff)
chap 9.3
Diffstat (limited to 'lib/vec3.rb')
-rw-r--r--lib/vec3.rb19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/vec3.rb b/lib/vec3.rb
index b9a5a73..e24229a 100644
--- a/lib/vec3.rb
+++ b/lib/vec3.rb
@@ -56,9 +56,28 @@ class Vec3
self / mag
end
+ def in_unit_sphere?
+ mag_sqr < 1
+ end
+
def to_s
"{#{@x}, #{@y}, #{@z}}"
end
+
+ def self.random(min = 0, max = 1)
+ interval = Interval.new(min.to_f, max.to_f)
+ Vec3.new(interval.sample, interval.sample, interval.sample)
+ end
+
+ def self.random_unit(normal = nil)
+ p = nil
+ while p.nil?
+ c = Vec3.random
+ p = c.unit if c.in_unit_sphere?
+ end
+
+ normal.nil? || p.dot(normal) > 0 ? p : -p
+ end
end
class Point < Vec3; end