Class: Ra::Ray
- Inherits:
-
Object
- Object
- Ra::Ray
- 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
-
#direction ⇒ Object
Returns the value of attribute direction.
-
#origin ⇒ Object
Returns the value of attribute origin.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#initialize(origin:, direction:) ⇒ Ray
constructor
A new instance of Ray.
- #position(t:) ⇒ Vector
- #transform(transform) ⇒ Ra::Ray
Constructor Details
#initialize(origin:, direction:) ⇒ Ray
Returns a new instance of Ray.
28 29 30 31 |
# File 'lib/ra/ray.rb', line 28 def initialize(origin:, direction:) @origin = origin @direction = direction end |
Instance Attribute Details
#direction ⇒ Object
Returns the value of attribute direction.
24 25 26 |
# File 'lib/ra/ray.rb', line 24 def direction @direction end |
#origin ⇒ Object
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
49 50 51 |
# File 'lib/ra/ray.rb', line 49 def ==(other) origin == other.origin && direction == other.direction end |
#position(t:) ⇒ Vector
35 36 37 |
# File 'lib/ra/ray.rb', line 35 def position(t:) @origin + (@direction * t) end |
#transform(transform) ⇒ Ra::Ray
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 |