Fixed Vector class

This commit is contained in:
2019-02-20 14:59:11 -06:00
parent cad7b5c263
commit 22bb98cb64

View File

@@ -1,11 +1,26 @@
class IMICFPS class IMICFPS
class Vector class Vector
attr_accessor :x, :y, :z, :weight
def initialize(x = 0, y = 0, z = 0, weight = 0) def initialize(x = 0, y = 0, z = 0, weight = 0)
@x, @y, @z, @weight = x, y, z, weight @x, @y, @z, @weight = x, y, z, weight
end end
def x; @x; end
def x=(n); @x = n; end
def y; @y; end
def y=(n); @y = n; end
def z; @z; end
def z=(n); @z = n; end
def weight; @weight; end
def weight=(n); @weight = n; end
# def xy=(nx, ny); @x = nx; @y = ny; end
# def xyz=(nx, ny, nz); @x = nx; @y = ny; @z = nz; end
# def xyzw=(nx, ny, nz, nw); @x = nx; @y = ny; @z = nz; @weight = nw; end
def ==(other) def ==(other)
@x == other.x && @x == other.x &&
@y == other.y && @y == other.y &&
@@ -16,67 +31,40 @@ class IMICFPS
def +(other) def +(other)
Vector.new( Vector.new(
@x + other.x, @x + other.x,
@y + other.y @y + other.y,
@z + other.z @z + other.z,
@weight + other.weight @weight + other.weight
) )
end end
def +=(other)
@x += other.x
@y += other.y
@z += other.z
@weight += other.weight
end
def -(other) def -(other)
Vector.new( Vector.new(
@x - other.x, @x - other.x,
@y - other.y @y - other.y,
@z - other.z @z - other.z,
@weight - other.weight @weight - other.weight
) )
end end
def -=(other)
@x -= other.x
@y -= other.y
@z -= other.z
@weight -= other.weight
end
def *(other) def *(other)
Vector.new( Vector.new(
@x * other.x, @x * other.x,
@y * other.y @y * other.y,
@z * other.z @z * other.z,
@weight * other.weight @weight * other.weight
) )
end end
def *=(other)
@x *= other.x
@y *= other.y
@z *= other.z
@weight *= other.weight
end
def /(other) def /(other)
# Endeavors to prevent division by zero
Vector.new( Vector.new(
@x / other.x, @x == 0 || other.x == 0 ? 0 : @x / other.x,
@y / other.y @y == 0 || other.y == 0 ? 0 : @y / other.y,
@z / other.z @z == 0 || other.z == 0 ? 0 : @z / other.z,
@weight / other.weight @weight == 0 || other.weight == 0 ? 0 : @weight / other.weight
) )
end end
def /=(other)
@x /= other.x
@y /= other.y
@z /= other.z
@weight /= other.weight
end
def to_a def to_a
[@x, @y, @z, @weight] [@x, @y, @z, @weight]
end end