Skip to content

Commit

Permalink
tuple scalar operations
Browse files Browse the repository at this point in the history
  • Loading branch information
CraigTreptow committed Feb 6, 2025
1 parent 0651d26 commit 138f68d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
10 changes: 10 additions & 0 deletions features/step_definitions/tuple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 12 additions & 12 deletions features/tuples.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/tuple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions sig/lib/tuple.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 138f68d

Please sign in to comment.