From 3549acb5dbd2f6fe43be6cce9828785ccead7e77 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Fri, 14 Jun 2024 12:23:49 +0100 Subject: Finished! Good god performance nope. --- lib/vec3.rb | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'lib/vec3.rb') 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 -- cgit v1.2.1