Class: V
- Inherits:
-
Object
- Object
- V
- Defined in:
- lib/graphics/v.rb
Overview
Simple and fast 2 dimensional vector
Constant Summary collapse
- ZERO =
zero vector
V[0.0, 0.0]
- ONE =
one vector
V[1.0, 1.0]
Instance Attribute Summary collapse
-
#x ⇒ Object
x coordinate accessors – preferably float.
-
#y ⇒ Object
y coordinate accessors – preferably float.
Instance Method Summary collapse
-
#*(s) ⇒ Object
Multiply a vector by a scalar, returning a new vector.
-
#+(v) ⇒ Object
Add two vectors, returning a new vector.
-
#-(v) ⇒ Object
Subtract two vectors, returning a new vector.
-
#-@ ⇒ Object
Unary negation.
-
#/(s) ⇒ Object
Divide a vector by a scalar, returning a new vector.
-
#==(other) ⇒ Object
:nodoc:.
-
#initialize(x, y) ⇒ V
constructor
Create a new vector with x & y coordinates.
-
#inspect ⇒ Object
(also: #to_s)
:nodoc:.
-
#magnitude ⇒ Object
Return the length of the vector from the origin.
Constructor Details
#initialize(x, y) ⇒ V
Create a new vector with x & y coordinates.
17 18 19 20 |
# File 'lib/graphics/v.rb', line 17 def initialize x, y @x = x @y = y end |
Instance Attribute Details
#x ⇒ Object
x coordinate accessors – preferably float
6 7 8 |
# File 'lib/graphics/v.rb', line 6 def x @x end |
#y ⇒ Object
y coordinate accessors – preferably float
8 9 10 |
# File 'lib/graphics/v.rb', line 8 def y @y end |
Instance Method Details
#*(s) ⇒ Object
Multiply a vector by a scalar, returning a new vector.
52 53 54 |
# File 'lib/graphics/v.rb', line 52 def * s V[x*s, y*s] end |
#+(v) ⇒ Object
Add two vectors, returning a new vector.
31 32 33 |
# File 'lib/graphics/v.rb', line 31 def + v V[x+v.x, y+v.y] end |
#-(v) ⇒ Object
Subtract two vectors, returning a new vector.
38 39 40 |
# File 'lib/graphics/v.rb', line 38 def - v V[x-v.x, y-v.y] end |
#/(s) ⇒ Object
Divide a vector by a scalar, returning a new vector.
59 60 61 |
# File 'lib/graphics/v.rb', line 59 def / s V[x/s, y/s] end |
#==(other) ⇒ Object
:nodoc:
63 64 65 |
# File 'lib/graphics/v.rb', line 63 def == other # :nodoc: x == other.x && y == other.y end |
#inspect ⇒ Object Also known as: to_s
:nodoc:
74 75 76 |
# File 'lib/graphics/v.rb', line 74 def inspect # :nodoc: "#{self.class.name}[%.2f, %.2f]" % [x, y] end |
#magnitude ⇒ Object
Return the length of the vector from the origin.
70 71 72 |
# File 'lib/graphics/v.rb', line 70 def magnitude @magnitude ||= Math.sqrt(x*x + y*y) end |