Class: Vector2D
- Inherits:
-
Object
- Object
- Vector2D
- Defined in:
- lib/vector2d.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
- .clamp(input, min, max) ⇒ Object
- .degrees_to_radians(degrees) ⇒ Object
- .expect(vector) ⇒ Object
- .from_radians(radians) ⇒ Object
- .from_scalar(scalar) ⇒ Object
- .lerp(start_value, end_value, amount) ⇒ Object
- .map(value, oldMin, oldMax, newMin, newMax) ⇒ Object
- .radians_to_degrees(radians) ⇒ Object
- .unit ⇒ Object
- .zero ⇒ Object
Instance Method Summary collapse
- #add_vector(vector) ⇒ Object
- #angle_between(vector) ⇒ Object
- #clamp_to_scalars(min, max) ⇒ Object
- #clamp_to_vectors(min_vector, max_vector) ⇒ Object
- #copy ⇒ Object
- #distance(vector) ⇒ Object
- #divide_scalar(scalar) ⇒ Object
- #divide_vector(vector) ⇒ Object
- #dot(vector) ⇒ Object
- #floor ⇒ Object
- #get_angle ⇒ Object
-
#initialize(x, y = nil) ⇒ Vector2D
constructor
A new instance of Vector2D.
- #lerp_vector(vector, amount) ⇒ Object
- #limit(maximum) ⇒ Object
- #magnitude ⇒ Object
- #magnitude_squared ⇒ Object
- #map_to_scalars(oldMin, oldMax, newMin, newMax) ⇒ Object
- #map_to_vectors(oldMinVector, oldMaxVector, newMinVector, newMaxVector) ⇒ Object
- #multiply_scalar(scalar) ⇒ Object
- #multiply_vector(vector) ⇒ Object
- #negate ⇒ Object
- #normalize ⇒ Object
- #reflect(normal) ⇒ Object
- #rotate(angle) ⇒ Object
- #subtract_vector(vector) ⇒ Object
Constructor Details
#initialize(x, y = nil) ⇒ Vector2D
Returns a new instance of Vector2D.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/vector2d.rb', line 4 def initialize(x, y=nil) # If x is a vector, use x's values if x.is_a? Vector2D @x = x.x @y = x.y else @x = x # If no y was provided, use x as a scalar if y == nil @y = x else @y = y end end end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
2 3 4 |
# File 'lib/vector2d.rb', line 2 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
2 3 4 |
# File 'lib/vector2d.rb', line 2 def y @y end |
Class Method Details
.clamp(input, min, max) ⇒ Object
149 150 151 152 153 |
# File 'lib/vector2d.rb', line 149 def self.clamp(input, min, max) return min if input <= min return max if input >= max return input end |
.degrees_to_radians(degrees) ⇒ Object
141 142 143 |
# File 'lib/vector2d.rb', line 141 def self.degrees_to_radians(degrees) (degrees - 90) * Math::PI / 180 end |
.expect(vector) ⇒ Object
20 21 22 23 |
# File 'lib/vector2d.rb', line 20 def self.expect(vector) raise "expected type of Vector2D, got #{vector.inspect}" unless vector.is_a?(Vector2D) vector end |
.from_radians(radians) ⇒ Object
37 38 39 |
# File 'lib/vector2d.rb', line 37 def self.from_radians(radians) Vector2D.new(Math.cos(radians), Math.sin(radians)) end |
.from_scalar(scalar) ⇒ Object
33 34 35 |
# File 'lib/vector2d.rb', line 33 def self.from_scalar(scalar) Vector2D.new(scalar,scalar) end |
.lerp(start_value, end_value, amount) ⇒ Object
111 112 113 |
# File 'lib/vector2d.rb', line 111 def self.lerp(start_value, end_value, amount) start_value + (end_value - start_value) * amount end |
.map(value, oldMin, oldMax, newMin, newMax) ⇒ Object
120 121 122 |
# File 'lib/vector2d.rb', line 120 def self.map(value, oldMin, oldMax, newMin, newMax) newMin + (newMax - newMin) * ((value - oldMin) / (oldMax - oldMin)) end |
.radians_to_degrees(radians) ⇒ Object
145 146 147 |
# File 'lib/vector2d.rb', line 145 def self.radians_to_degrees(radians) (radians * 180 / Math::PI) + 90 end |
Instance Method Details
#add_vector(vector) ⇒ Object
53 54 55 |
# File 'lib/vector2d.rb', line 53 def add_vector(vector) Vector2D.new(@x + vector.x, @y + vector.y) end |
#angle_between(vector) ⇒ Object
134 135 136 137 138 139 |
# File 'lib/vector2d.rb', line 134 def angle_between(vector) angle = dot(vector) / magnitude * vector.magnitude return Math::PI if angle <= -1 return 0 if angle >= 0 return angle end |
#clamp_to_scalars(min, max) ⇒ Object
155 156 157 158 |
# File 'lib/vector2d.rb', line 155 def clamp_to_scalars(min, max) Vector2D.new(Vector2D.clamp(@x, min, max), Vector2D.clamp(@y, min, max)) end |
#clamp_to_vectors(min_vector, max_vector) ⇒ Object
160 161 162 163 |
# File 'lib/vector2d.rb', line 160 def clamp_to_vectors(min_vector, max_vector) Vector2D.new(Vector2D.clamp(@x, min_vector.x, max_vector.x), Vector2D.clamp(@y, min_vector.y, max_vector.y)) end |
#distance(vector) ⇒ Object
77 78 79 |
# File 'lib/vector2d.rb', line 77 def distance(vector) Math.sqrt(((@x - vector.x) + (@y - vector.y))**2) end |
#divide_scalar(scalar) ⇒ Object
73 74 75 |
# File 'lib/vector2d.rb', line 73 def divide_scalar(scalar) Vector2D.new(@x / scalar, @y / scalar) end |
#divide_vector(vector) ⇒ Object
69 70 71 |
# File 'lib/vector2d.rb', line 69 def divide_vector(vector) Vector2D.new(@x / vector.x, @y / vector.y) end |
#dot(vector) ⇒ Object
81 82 83 |
# File 'lib/vector2d.rb', line 81 def dot(vector) @x * vector.x + @y * vector.y end |
#floor ⇒ Object
165 166 167 |
# File 'lib/vector2d.rb', line 165 def floor Vector2D.new(@x.floor, @y.floor) end |
#get_angle ⇒ Object
102 103 104 |
# File 'lib/vector2d.rb', line 102 def get_angle -1 * Math.atan2(@y * -1, @x) end |
#lerp_vector(vector, amount) ⇒ Object
115 116 117 118 |
# File 'lib/vector2d.rb', line 115 def lerp_vector(vector, amount) Vector2D.new(Vector2D.lerp(@x, vector.x, amount), Vector2D.lerp(@y, vector.y, amount)) end |
#limit(maximum) ⇒ Object
96 97 98 99 100 |
# File 'lib/vector2d.rb', line 96 def limit(maximum) mag_squared = magnitude_squared return copy if magnitude_squared <= maximum**2 return (normalize).multiply_scalar(maximum) end |
#magnitude ⇒ Object
45 46 47 |
# File 'lib/vector2d.rb', line 45 def magnitude Math.sqrt magnitude_squared end |
#magnitude_squared ⇒ Object
49 50 51 |
# File 'lib/vector2d.rb', line 49 def magnitude_squared @x * @x + @y * @y end |
#map_to_scalars(oldMin, oldMax, newMin, newMax) ⇒ Object
124 125 126 127 |
# File 'lib/vector2d.rb', line 124 def map_to_scalars(oldMin, oldMax, newMin, newMax) Vector2D.new(Vector2D.map(@x, oldMin, oldMax, newMin, newMax), Vector2D.map(@y, oldMin, oldMax, newMin, newMax)) end |
#map_to_vectors(oldMinVector, oldMaxVector, newMinVector, newMaxVector) ⇒ Object
129 130 131 132 |
# File 'lib/vector2d.rb', line 129 def map_to_vectors(oldMinVector, oldMaxVector, newMinVector, newMaxVector) Vector2D.new(Vector2D.map(@x, oldMinVector.x, oldMaxVector.x, newMinVector.x, newMaxVector.x), Vector2D.map(@y, oldMinVector.y, oldMaxVector.y, newMinVector.y, newMaxVector.y)) end |
#multiply_scalar(scalar) ⇒ Object
65 66 67 |
# File 'lib/vector2d.rb', line 65 def multiply_scalar(scalar) Vector2D.new(@x * scalar, @y * scalar) end |
#multiply_vector(vector) ⇒ Object
61 62 63 |
# File 'lib/vector2d.rb', line 61 def multiply_vector(vector) Vector2D.new(@x * vector.x, @y * vector.y) end |
#negate ⇒ Object
169 170 171 |
# File 'lib/vector2d.rb', line 169 def negate multiply_scalar(-1) end |
#normalize ⇒ Object
90 91 92 93 94 |
# File 'lib/vector2d.rb', line 90 def normalize mag = magnitude return copy if mag == 0 || mag == 1 return divide_scalar mag end |
#reflect(normal) ⇒ Object
85 86 87 88 |
# File 'lib/vector2d.rb', line 85 def reflect(normal) dot_product = dot(vector) Vector2D.new(@x - (2 * dot_product * normal.x), @y - (2 * dot_.roduct * normal.y)) end |