Class: MiniGL::Vector
- Inherits:
-
Object
- Object
- MiniGL::Vector
- Defined in:
- lib/minigl/global.rb
Overview
This class represents a point or vector in a bidimensional space.
Instance Attribute Summary collapse
-
#x ⇒ Object
The x coordinate of the vector.
-
#y ⇒ Object
The y coordinate of the vector.
Instance Method Summary collapse
-
#!=(other_vector, precision = 6) ⇒ Object
Returns
true
if at least one coordinate of this vector is different from the corresponding coordinate ofother_vector
, withprecision
decimal places of precision. -
#*(scalar) ⇒ Object
Multiplies this vector by a scalar, i.e., each coordinate is multiplied by the given number.
-
#+(other_vector) ⇒ Object
Sums this vector with
other_vector
, i.e., sums each coordinate of this vector with the corresponding coordinate ofother_vector
. -
#-(other_vector) ⇒ Object
Subtracts
other_vector
from this vector, i.e., subtracts from each coordinate of this vector the corresponding coordinate ofother_vector
. -
#/(scalar) ⇒ Object
Divides this vector by a scalar, i.e., each coordinate is divided by the given number.
-
#==(other_vector, precision = 6) ⇒ Object
Returns
true
if both coordinates of this vector are equal to the corresponding coordinates ofother_vector
, withprecision
decimal places of precision. -
#distance(other_vector) ⇒ Object
Returns the euclidean distance between this vector and
other_vector
. -
#initialize(x = 0, y = 0) ⇒ Vector
constructor
Creates a new bidimensional vector.
-
#rotate(radians) ⇒ Object
Returns a vector corresponding to the rotation of this vector around the origin (0, 0) by
radians
radians. -
#rotate!(radians) ⇒ Object
Rotates this vector by
radians
radians around the origin (0, 0).
Constructor Details
#initialize(x = 0, y = 0) ⇒ Vector
Creates a new bidimensional vector.
Parameters:
- x
-
The x coordinate of the vector
- y
-
The y coordinate of the vector
18 19 20 21 |
# File 'lib/minigl/global.rb', line 18 def initialize(x = 0, y = 0) @x = x @y = y end |
Instance Attribute Details
#x ⇒ Object
The x coordinate of the vector
8 9 10 |
# File 'lib/minigl/global.rb', line 8 def x @x end |
#y ⇒ Object
The y coordinate of the vector
11 12 13 |
# File 'lib/minigl/global.rb', line 11 def y @y end |
Instance Method Details
#!=(other_vector, precision = 6) ⇒ Object
Returns true
if at least one coordinate of this vector is different from the corresponding coordinate of other_vector
, with precision
decimal places of precision.
34 35 36 37 |
# File 'lib/minigl/global.rb', line 34 def !=(other_vector, precision = 6) @x.round(precision) != other_vector.x.round(precision) or @y.round(precision) != other_vector.y.round(precision) end |
#*(scalar) ⇒ Object
Multiplies this vector by a scalar, i.e., each coordinate is multiplied by the given number.
53 54 55 |
# File 'lib/minigl/global.rb', line 53 def *(scalar) Vector.new @x * scalar, @y * scalar end |
#+(other_vector) ⇒ Object
Sums this vector with other_vector
, i.e., sums each coordinate of this vector with the corresponding coordinate of other_vector
.
41 42 43 |
# File 'lib/minigl/global.rb', line 41 def +(other_vector) Vector.new @x + other_vector.x, @y + other_vector.y end |
#-(other_vector) ⇒ Object
Subtracts other_vector
from this vector, i.e., subtracts from each coordinate of this vector the corresponding coordinate of other_vector
.
47 48 49 |
# File 'lib/minigl/global.rb', line 47 def -(other_vector) Vector.new @x - other_vector.x, @y - other_vector.y end |
#/(scalar) ⇒ Object
Divides this vector by a scalar, i.e., each coordinate is divided by the given number.
59 60 61 |
# File 'lib/minigl/global.rb', line 59 def /(scalar) Vector.new @x / scalar.to_f, @y / scalar.to_f end |
#==(other_vector, precision = 6) ⇒ Object
Returns true
if both coordinates of this vector are equal to the corresponding coordinates of other_vector
, with precision
decimal places of precision.
26 27 28 29 |
# File 'lib/minigl/global.rb', line 26 def ==(other_vector, precision = 6) @x.round(precision) == other_vector.x.round(precision) and @y.round(precision) == other_vector.y.round(precision) end |
#distance(other_vector) ⇒ Object
Returns the euclidean distance between this vector and other_vector
.
64 65 66 67 68 |
# File 'lib/minigl/global.rb', line 64 def distance(other_vector) dx = @x - other_vector.x dy = @y - other_vector.y Math.sqrt(dx ** 2 + dy ** 2) end |
#rotate(radians) ⇒ Object
Returns a vector corresponding to the rotation of this vector around the origin (0, 0) by radians
radians.
72 73 74 75 76 |
# File 'lib/minigl/global.rb', line 72 def rotate(radians) sin = Math.sin radians cos = Math.cos radians Vector.new cos * @x - sin * @y, sin * @x + cos * @y end |
#rotate!(radians) ⇒ Object
Rotates this vector by radians
radians around the origin (0, 0).
79 80 81 82 83 84 85 |
# File 'lib/minigl/global.rb', line 79 def rotate!(radians) sin = Math.sin radians cos = Math.cos radians prev_x = @x @x = cos * @x - sin * @y @y = sin * prev_x + cos * @y end |