mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 13:12:34 +00:00
Improve Vector arithmetic performance by 2x
This commit is contained in:
@@ -95,39 +95,58 @@ module CyberarmEngine
|
|||||||
Vector.new(@x, @y)
|
Vector.new(@x, @y)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Performs math operation, excluding {weight}
|
|
||||||
private def operator(function, other)
|
|
||||||
if other.is_a?(Numeric)
|
|
||||||
Vector.new(
|
|
||||||
@x.send(:"#{function}", other),
|
|
||||||
@y.send(:"#{function}", other),
|
|
||||||
@z.send(:"#{function}", other)
|
|
||||||
)
|
|
||||||
else
|
|
||||||
Vector.new(
|
|
||||||
@x.send(:"#{function}", other.x),
|
|
||||||
@y.send(:"#{function}", other.y),
|
|
||||||
@z.send(:"#{function}", other.z)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Adds Vector and Numeric or Vector and Vector, excluding {weight}
|
# Adds Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
# @return [CyberarmEngine::Vector]
|
# @return [CyberarmEngine::Vector]
|
||||||
def +(other)
|
def +(other)
|
||||||
operator("+", other)
|
if other.is_a?(Numeric)
|
||||||
|
Vector.new(
|
||||||
|
@x + other,
|
||||||
|
@y + other,
|
||||||
|
@z + other
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Vector.new(
|
||||||
|
@x + other.x,
|
||||||
|
@y + other.y,
|
||||||
|
@z + other.z
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Subtracts Vector and Numeric or Vector and Vector, excluding {weight}
|
# Subtracts Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
# @return [CyberarmEngine::Vector]
|
# @return [CyberarmEngine::Vector]
|
||||||
def -(other)
|
def -(other)
|
||||||
operator("-", other)
|
if other.is_a?(Numeric)
|
||||||
|
Vector.new(
|
||||||
|
@x - other,
|
||||||
|
@y - other,
|
||||||
|
@z - other
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Vector.new(
|
||||||
|
@x - other.x,
|
||||||
|
@y - other.y,
|
||||||
|
@z - other.z
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Multiplies Vector and Numeric or Vector and Vector, excluding {weight}
|
# Multiplies Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
# @return [CyberarmEngine::Vector]
|
# @return [CyberarmEngine::Vector]
|
||||||
def *(other)
|
def *(other)
|
||||||
operator("*", other)
|
if other.is_a?(Numeric)
|
||||||
|
Vector.new(
|
||||||
|
@x * other,
|
||||||
|
@y * other,
|
||||||
|
@z * other
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Vector.new(
|
||||||
|
@x * other.x,
|
||||||
|
@y * other.y,
|
||||||
|
@z * other.z
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def multiply_transform(transform)
|
def multiply_transform(transform)
|
||||||
|
|||||||
Reference in New Issue
Block a user