Class: Silicium::Geometry3d::Vector3d
- Inherits:
-
Object
- Object
- Silicium::Geometry3d::Vector3d
- Defined in:
- lib/geometry3d.rb
Overview
Class represents vector in three-dimensional space
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
-
#z ⇒ Object
readonly
Returns the value of attribute z.
Instance Method Summary collapse
-
#addition!(other_vector) ⇒ Object
Add one vector to another.
-
#collinear?(vector2) ⇒ Boolean
Check if two vectors are collinear.
-
#cos_between_vectors(other_vector) ⇒ Object
Returns cos between two vectors.
-
#help_check(vector2, x, y, z) ⇒ Object
help function for collinear function.
-
#helper(value1, value2) ⇒ Object
helps to divide correctly.
-
#initialize(point) ⇒ Vector3d
constructor
Initializes with one objects of type Point3d 2nd point is (0,0,0).
-
#length ⇒ Object
Returns length of the vector.
-
#multiplication_by_number!(r) ⇒ Object
Mult vector by number.
-
#norm_vector(point2, point3) ⇒ Object
Find normal vector.
-
#scalar_multiplication(other_vector) ⇒ Object
Returns scalar multiplication of 2 vectors.
-
#sign(integer) ⇒ Object
Function for checking sign of number.
-
#subtraction!(other_vector) ⇒ Object
Sub one vector from another.
-
#vector_multiplication(other_vector) ⇒ Object
Returns vector multiplication of 2 vectors.
-
#zero_vector? ⇒ Boolean
Checks if vector is zero vector.
Constructor Details
#initialize(point) ⇒ Vector3d
Initializes with one objects of type Point3d 2nd point is (0,0,0)
117 118 119 120 121 |
# File 'lib/geometry3d.rb', line 117 def initialize(point) @x = point.x @y = point.y @z = point.z end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
110 111 112 |
# File 'lib/geometry3d.rb', line 110 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
111 112 113 |
# File 'lib/geometry3d.rb', line 111 def y @y end |
#z ⇒ Object (readonly)
Returns the value of attribute z.
112 113 114 |
# File 'lib/geometry3d.rb', line 112 def z @z end |
Instance Method Details
#addition!(other_vector) ⇒ Object
Add one vector to another
137 138 139 140 141 |
# File 'lib/geometry3d.rb', line 137 def addition!(other_vector) @x += other_vector.x @y += other_vector.y @z += other_vector.z end |
#collinear?(vector2) ⇒ Boolean
Check if two vectors are collinear
224 225 226 227 228 229 230 231 232 |
# File 'lib/geometry3d.rb', line 224 def collinear?(vector2) x1 = (vector2.x).abs y1 = (vector2.y).abs z1 = (vector2.z).abs x = helper(x1,@x.abs) y = helper(y1,@y.abs) z = helper(z1,@z.abs) help_check(vector2, x, y, z) end |
#cos_between_vectors(other_vector) ⇒ Object
Returns cos between two vectors
167 168 169 |
# File 'lib/geometry3d.rb', line 167 def cos_between_vectors(other_vector) scalar_multiplication(other_vector) / (length * other_vector.length).to_f end |
#help_check(vector2, x, y, z) ⇒ Object
help function for collinear function
205 206 207 208 209 210 |
# File 'lib/geometry3d.rb', line 205 def help_check(vector2, x, y, z) check1 = x * sign(vector2.x) * sign(@x) == y * sign(vector2.y) * sign(@y) check2 = x * sign(vector2.x) * sign(@x) == z * sign(vector2.z) * sign(@z) check3 = z * sign(vector2.z) * sign(@z) == y * sign(vector2.y) * sign(@y) check1 && check2 && check3 end |
#helper(value1, value2) ⇒ Object
helps to divide correctly
213 214 215 216 217 218 219 220 221 |
# File 'lib/geometry3d.rb', line 213 def helper(value1, value2) result = 0 if value1 > value2 result = value1 / value2 else result = value2 / value1 end result end |
#length ⇒ Object
Returns length of the vector
131 132 133 |
# File 'lib/geometry3d.rb', line 131 def length Math.sqrt(@x**2 + @y**2 + @z**2) end |
#multiplication_by_number!(r) ⇒ Object
Mult vector by number
153 154 155 156 157 |
# File 'lib/geometry3d.rb', line 153 def multiplication_by_number!(r) @x *= r @y *= r @z *= r end |
#norm_vector(point2, point3) ⇒ Object
Find normal vector
vector mult
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/geometry3d.rb', line 184 def norm_vector(point2, point3) point1 = Point3d.new(@x, @y, @z) # checking if the points isn't on the same line # finding vector between points 1 and 2 ;1 and 3 vector12 = Vector3d.new(Point3d.new(point2.x - point1.x, point2.y - point1.y, point2.z - point1.z)) vector13 = Vector3d.new(Point3d.new(point3.x - point1.x, point3.y - point1.y, point3.z - point1.z)) # vector13=vector1.scalar_multiplication(vector3) x = vector12.y * vector13.z - vector12.z * vector13.y y = -(vector12.x * vector13.z - vector12.z * vector13.x) z = vector12.x * vector13.y - vector12.y * vector13.x Vector3d.new(Point3d.new(x, y, z)) end |
#scalar_multiplication(other_vector) ⇒ Object
Returns scalar multiplication of 2 vectors
161 162 163 |
# File 'lib/geometry3d.rb', line 161 def scalar_multiplication(other_vector) x * other_vector.x + y * other_vector.y + z * other_vector.z end |
#sign(integer) ⇒ Object
Function for checking sign of number
199 200 201 |
# File 'lib/geometry3d.rb', line 199 def sign(integer) integer >= 0 ? 1 : -1 end |
#subtraction!(other_vector) ⇒ Object
Sub one vector from another
145 146 147 148 149 |
# File 'lib/geometry3d.rb', line 145 def subtraction!(other_vector) @x -= other_vector.x @y -= other_vector.y @z -= other_vector.z end |
#vector_multiplication(other_vector) ⇒ Object
Returns vector multiplication of 2 vectors
173 174 175 176 177 178 |
# File 'lib/geometry3d.rb', line 173 def vector_multiplication(other_vector) x = @y * other_vector.z - @z * other_vector.y y = @z * other_vector.x - @x * other_vector.z z = @x * other_vector.y - @y * other_vector.x Vector3d.new(Point3d.new(x, y, z)) end |
#zero_vector? ⇒ Boolean
Checks if vector is zero vector
125 126 127 |
# File 'lib/geometry3d.rb', line 125 def zero_vector? (@x.eql?(0) && @y.eql?(0) && @z.eql?(0)).eql?(true) ? true : false end |