xenobrain / ruby_vectormath Goto Github PK
View Code? Open in Web Editor NEWLicense: The Unlicense
License: The Unlicense
Since DragonRuby 3.9, there is a built-in Vec2:
e = Vec2.new
# {}
e.class
# Vec2
Vec2.superclass
# Hash
a = MatrixFunctions.vec2(1, 2)
# {:x=>1, :y=>2}
b = MatrixFunctions.vec2(3, 4)
# {:x=>3, :y=>4}
c = a + b
# {:x=>4.0, :y=>6.0}
d = Vec2.new(1, 2)
# EXCEPTION: wrong number of arguments (given 2, expected 0..1)
if you include vectormath_2d from this library (without wrapping it in a module), it monkey-patches the built-in class which results in some odd behavior:
a = MatrixFunctions.vec2(1, 2)
b = MatrixFunctions.vec2(3, 4)
c = a + b
c.x
# 0.0
c.y
# 0.0
c[:x]
# 1
c[:y]
# 2
Some options that spring to mind:
Vec2
to add all of the in-place methodsRelevant part of the DR changelog:
** [Standard] The following matrix math apis have been added to Object
(accessible from anywhere):
The intent of these matrix functions is to mirror GLSL matrix apis.
vec(2|3|4)
: Creates a vec
with the given dimension. Represented as a Hash
primitive_marker
set to :vec2
, :vec3
, or vec4
.mat(2|3|4)
: Creates a mat
with the given dimension. Represented as a Hash
of Hash
esprimitive_marker
set to :mat2
, :mat3
, :mat4
at the top level,:vec(2|3|4)
for each vector component.add
: Adds matrices given to the function (returns nil
if a matrix multiplication isn't valid).mul
: Multiplies matrices given to the function. Returns nil
if the matrices are incompatible.dot
: Performs a scalar dot product for matrices given a list of matrices. Returns nil
if the matrices are incompatible.cross
: Performs a cross product for matrices given a list of matrices. Returns nil
if the matrices are incompatible.normalize
: Normalizes a vector. Returns the new normalized vector as a new Hash
.normalize!
: Normalizes a vector. Mutates the vector passed in.distance
: Returns the distance between to vectors. Returns nil
if the vectors are incompatible.Hash#+
: Delegates to add
for matrix multiplication. Returns nil
if the matrices areprimitive_marker
isn't set on the Hash
.Hash#*
: Delegates to mul
for matrix multiplication. Returns nil
if the matrices areprimitive_marker
isn't set on the Hash
.Calling sub_from!
always sets @y
to 0.
def sub_from!(vec2_lhs, vec2_rhs)
@x = vec2_lhs.x - vec2_rhs.x
- @y = vec2_lhs.y - vec2_lhs.y
+ @y = vec2_lhs.y - vec2_rhs.y
self
end
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.