diff --git a/features/step_definitions/tuple.rb b/features/step_definitions/tuple.rb index 2804c44..7b66620 100644 --- a/features/step_definitions/tuple.rb +++ b/features/step_definitions/tuple.rb @@ -107,3 +107,13 @@ expected = Tuple.new(x: float, y: float2, z: float3, w: float4) assert_equal(@a.negate, expected) end + +Then('a * {float} = tuple\({float}, {float}, {float}, {float})') do |float, float1, float2, float3, float4| + expected = Tuple.new(x: float1, y: float2, z: float3, w: float4) + assert_equal(@a * float, expected) +end + +Then('a \/ {float} = tuple\({float}, {float}, {float}, {float})') do |float, float1, float2, float3, float4| + expected = Tuple.new(x: float1, y: float2, z: float3, w: float4) + assert_equal(@a / float, expected) +end diff --git a/features/tuples.feature b/features/tuples.feature index 4ada035..bcec81f 100755 --- a/features/tuples.feature +++ b/features/tuples.feature @@ -55,18 +55,18 @@ Scenario: Negating a tuple Given a ← tuple(1, -2, 3, -4) Then -a = tuple(-1, 2, -3, 4) -# Scenario: Multiplying a tuple by a scalar -# Given a ← tuple(1, -2, 3, -4) -# Then a * 3.5 = tuple(3.5, -7, 10.5, -14) -# -# Scenario: Multiplying a tuple by a fraction -# Given a ← tuple(1, -2, 3, -4) -# Then a * 0.5 = tuple(0.5, -1, 1.5, -2) -# -# Scenario: Dividing a tuple by a scalar -# Given a ← tuple(1, -2, 3, -4) -# Then a / 2 = tuple(0.5, -1, 1.5, -2) -# +Scenario: Multiplying a tuple by a scalar + Given a ← tuple(1, -2, 3, -4) + Then a * 3.5 = tuple(3.5, -7, 10.5, -14) + +Scenario: Multiplying a tuple by a fraction + Given a ← tuple(1, -2, 3, -4) + Then a * 0.5 = tuple(0.5, -1, 1.5, -2) + +Scenario: Dividing a tuple by a scalar + Given a ← tuple(1, -2, 3, -4) + Then a / 2 = tuple(0.5, -1, 1.5, -2) + # Scenario: Computing the magnitude of vector(1, 0, 0) # Given v ← vector(1, 0, 0) # Then magnitude(v) = 1 diff --git a/lib/tuple.rb b/lib/tuple.rb index ba6e21a..7c9a62c 100644 --- a/lib/tuple.rb +++ b/lib/tuple.rb @@ -8,6 +8,14 @@ def initialize(x:, y:, z:, w:) @w = w end + def *(other) + Tuple.new(x: @x * other, y: @y * other, z: @z * other, w: @w * other) + end + + def /(other) + Tuple.new(x: @x / other, y: @y / other, z: @z / other, w: @w / other) + end + def +(other) Tuple.new(x: @x + other.x, y: @y + other.y, z: @z + other.z, w: @w + other.w) end diff --git a/sig/lib/tuple.rbs b/sig/lib/tuple.rbs index 8e614e9..58c0627 100644 --- a/sig/lib/tuple.rbs +++ b/sig/lib/tuple.rbs @@ -7,6 +7,8 @@ class Tuple def z: -> Float def w: -> Float def initialize: (x: Float, y: Float, z: Float, w: Float) -> void + def *: (Float) -> Tuple + def /: (Float) -> Tuple def +: (Tuple) -> Tuple def -: (Tuple) -> Tuple def negate: -> Tuple