aboutsummaryrefslogtreecommitdiff
path: root/lib/vec3.rb
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-06-14 12:23:49 +0100
committerNat Lasseter <user@4574.co.uk>2024-06-14 12:23:49 +0100
commit3549acb5dbd2f6fe43be6cce9828785ccead7e77 (patch)
treec4cb0f2db631d3b60b641f6c8e654663940b3d38 /lib/vec3.rb
parent3bf56da7aa64ea3ca05559a4e52df8f596bcf86b (diff)
Finished! Good god performance nope.HEADmain
Diffstat (limited to 'lib/vec3.rb')
-rw-r--r--lib/vec3.rb27
1 files changed, 15 insertions, 12 deletions
diff --git a/lib/vec3.rb b/lib/vec3.rb
index 478fe4c..46d8ee1 100644
--- a/lib/vec3.rb
+++ b/lib/vec3.rb
@@ -1,8 +1,8 @@
class Vec3
- def initialize(x = 0, y = 0, z = 0)
- @x = x.to_f
- @y = y.to_f
- @z = z.to_f
+ def initialize(x = 0.0, y = 0.0, z = 0.0)
+ @x = x
+ @y = y
+ @z = z
end
attr_reader :x, :y, :z
@@ -67,7 +67,7 @@ class Vec3
rout_perp + rout_parr
end
- def in_unit_sphere?
+ def in_unit?
mag_sqr < 1
end
@@ -79,18 +79,21 @@ class Vec3
"{#{@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)
+ def self.random(min: -1.0, max: 1.0, dimensions: 3)
+ interval = Interval.new(min, max)
+ self.new(
+ dimensions >= 1 ? interval.sample : 0,
+ dimensions >= 2 ? interval.sample : 0,
+ dimensions >= 3 ? interval.sample : 0
+ )
end
- def self.random_unit(normal = nil)
+ def self.random_in_unit(normal: nil, dimensions: 3)
p = nil
while p.nil?
- c = Vec3.random
- p = c.unit if c.in_unit_sphere?
+ c = self.random(dimensions: dimensions)
+ p = c.unit if c.in_unit?
end
-
normal.nil? || p.dot(normal) > 0 ? p : -p
end
end