aboutsummaryrefslogtreecommitdiff
path: root/lib/vec3.rb
diff options
context:
space:
mode:
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