Class: Ra::Ray

Inherits:
Object
  • Object
show all
Defined in:
lib/ra/ray.rb

Overview

A ray is positioned at an origin travelling by a direction. A ray is cast and used to identify collisions with objects. For example:

ray = Ra::Ray.new(
  origin: Vector[0, 0, 0, Ra::Tuple::POINT],
  direction: Vector[1, 2, 3, Ra::Tuple::VECTOR],
)
ray.position(t: 1) == Vector[1, 2, 3, Ra::Tuple::VECTOR]
ray.position(t: 2) == Vector[2, 4, 6, Ra::Tuple::VECTOR]
ray.position(t: 3) == Vector[3, 6, 9, Ra::Tuple::VECTOR]

A ray can be transformed. This is useful when considering the ray relative to an object that has a transform associated with it. For example:

ray = Ra::Ray.new(
  origin: Vector[0, 0, 0, Ra::Tuple::POINT],
  direction: Vector[1, 2, 3, Ra::Tuple::VECTOR],
)
ray.transform(transform: Ra::Transform.scale(1, 2, 3))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin:, direction:) ⇒ Ray

Returns a new instance of Ray.

Parameters:

  • origin (Vector)

    e.g. Vector[1, 2, 3, Ra::Tuple::POINT]

  • direction (Vector)

    e.g. Vector[1, 2, 3, Ra::Tuple::VECTOR]



28
29
30
31
# File 'lib/ra/ray.rb', line 28

def initialize(origin:, direction:)
  @origin = origin
  @direction = direction
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



24
25
26
# File 'lib/ra/ray.rb', line 24

def direction
  @direction
end

#originObject

Returns the value of attribute origin.



24
25
26
# File 'lib/ra/ray.rb', line 24

def origin
  @origin
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ra/ray.rb', line 49

def ==(other)
  origin == other.origin && direction == other.direction
end

#position(t:) ⇒ Vector

Parameters:

  • t (Numeric)

Returns:

  • (Vector)


35
36
37
# File 'lib/ra/ray.rb', line 35

def position(t:)
  @origin + (@direction * t)
end

#transform(transform) ⇒ Ra::Ray

Parameters:

Returns:



41
42
43
44
45
46
# File 'lib/ra/ray.rb', line 41

def transform(transform)
  self.class.new(
    origin: transform * @origin,
    direction: transform * @direction,
  )
end