class Silicium::Geometry3d::Vector3d

Class represents vector in three-dimensional space

Attributes

x[R]
y[R]
z[R]

Public Class Methods

new(point) click to toggle source

Initializes with one objects of type Point3d 2nd point is (0,0,0)

# File lib/geometry3d.rb, line 117
def initialize(point)
  @x = point.x
  @y = point.y
  @z = point.z
end

Public Instance Methods

addition!(other_vector) click to toggle source

Add one vector to another

# 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) click to toggle source

Check if two vectors are collinear

# 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) click to toggle source

Returns cos between two vectors

# 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) click to toggle source

help function for collinear function

# 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) click to toggle source

helps to divide correctly

# 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() click to toggle source

Returns length of the vector

# File lib/geometry3d.rb, line 131
def length
  Math.sqrt(@x**2 + @y**2 + @z**2)
end
multiplication_by_number!(r) click to toggle source

Mult vector by number

# File lib/geometry3d.rb, line 153
def multiplication_by_number!(r)
  @x *= r
  @y *= r
  @z *= r
end
norm_vector(point2, point3) click to toggle source

Find normal vector

vector mult

# 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) click to toggle source

Returns scalar multiplication of 2 vectors

# 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) click to toggle source

Function for checking sign of number

# File lib/geometry3d.rb, line 199
def sign(integer)
  integer >= 0 ? 1 : -1
end
subtraction!(other_vector) click to toggle source

Sub one vector from another

# 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) click to toggle source

Returns vector multiplication of 2 vectors

# 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?() click to toggle source

Checks if vector is zero vector

# File lib/geometry3d.rb, line 125
def zero_vector?
  (@x.eql?(0) && @y.eql?(0) && @z.eql?(0)).eql?(true) ? true : false
end