class Vec attr_accessor :x, :y, :z EPSILON = 9.999999747378752e-05 # a value used by processing.org def initialize(x = 0 ,y = 0, z = 0) @x, @y, @z = x, y, z post_initialize end def post_initialize nil end def ==(vec) (x - vec.x).abs < EPSILON && (y - vec.y).abs < EPSILON && (z - vec.z).abs < EPSILON end end class Vec2D < Vec # Modulus of vec. Also known as length, size or norm def modulus Math.hypot(x, y) end def self.dist_squared(vec_a, vec_b) (vec_a.x - vec_b.x)**2 + (vec_a.y - vec_b.y)**2 end def self.dist(vec_a, vec_b) Math.hypot(vec_a.x - vec_b.x, vec_a.y - vec_b.y) end # vanilla processing returns a Vector, rather than Scalar (defaults to 3D result when z = 0) def cross_product(vec) x * vec.y - y * vec.x end # Scalar product, also known as inner product or dot product def dot(vec) x * vec.x + y * vec.y end def collinear_with?(vec) cross_product(vec).abs < EPSILON end def +(vec) Vec2D.new(x + vec.x, y + vec.y) end def -(vec) Vec2D.new(x - vec.x, y - vec.y) end def *(scalar) Vec2D.new(x * scalar, y * scalar) end def / (scalar) Vec2D.new(x / scalar, y / scalar) unless scalar == 0 end def normalize! @x, @y = x / modulus, y / modulus return self end alias :mag :modulus end class Vec3D < Vec def modulus Math.sqrt(x**2 + y**2 + z**2) end def self.dist_squared(vec_a, vec_b) (vec_a.x - vec_b.x)**2 + (vec_a.y - vec_b.y)**2 + (vec_a.z - vec_b.z)**2 end def self.dist(vec_a, vec_b) Math.sqrt(self.dist_squared(vec_a, vec_b)) end def cross_product(vec) xc = y * vec.z - z * vec.y yc = z * vec.x - x * vec.z zc = x * vec.y - y * vec.x Vec3D.new(xc, yc, zc) end # Scalar product, also known as inner product or dot product def dot(vec) x * vec.x + y * vec.y + z * vec.z end def collinear_with?(vec) cross_product(vec) == Vec3D.new end def +(vec) Vec3D.new(x + vec.x, y + vec.y, z + vec.z) end def -(vec) Vec3D.new(x - vec.x, y - vec.y, z - vec.z) end def * (scalar) Vec3D.new(x * scalar, y * scalar, z * scalar) end def / (scalar) Vec3D.new(x / scalar, y / scalar, z / scalar) unless scalar.abs < EPSILON end def normalize! @x, @y, @z = x / modulus, y / modulus, z / modulus return self end alias :mag :modulus end
A Little Test courtesy of Daniel Shiffman
# # Vector # by Daniel Shiffman. # # Demonstration some basic vector math: subtraction, normalization, scaling # Normalizing a vector sets its length to 1. # load_library :vec def setup size(640,360) end def draw background(0) # A vector that points to the mouse location mouse = Vec2D.new(mouse_x, mouse_y) # A vector that points to the center of the window center = Vec2D.new(width/2,height/2) # Subtract center from mouse which results in a vector that points from center to mouse mouse = mouse - center # note need assign result to mouse # Normalize the vector the ! means we are changing the value of mouse mouse.normalize! # Multiply its length by 150 (Scaling its length) mouse = mouse * 150 # note need assign the result to mouse translate(width/2,height/2) # Draw the resulting vector stroke(255) stroke_weight(4) line(0, 0, mouse.x, mouse.y) end
Now interestingly the following also works so you can use the
+=, -=, /=, and *= assignment methods ( but you cannot override += etc as a methods unlike C++ )So what this means is that in ruby += etc are just syntactic shortcuts, and not an operator in its own right (which is probably a good thing).
# # Vector # by Daniel Shiffman. # # Demonstration some basic vector math: subtraction, normalization, scaling # Normalizing a vector sets its length to 1. # load_library :vec def setup size(640,360) end def draw background(0) # A vector that points to the mouse location mouse = Vec2D.new(mouse_x, mouse_y) # A vector that points to the center of the window center = Vec2D.new(width/2,height/2) # Subtract center from mouse which results in a vector that points from center to mouse mouse -= center # note we can assign result to mouse using -= # Normalize the vector the ! means we are changing the value of mouse mouse.normalize! # Multiply its length by 150 (Scaling its length) mouse *= 150 # note we can assign result to mouse using *= translate(width/2,height/2) # Draw the resulting vector stroke(255) stroke_weight(4) line(0, 0, mouse.x, mouse.y) end
No comments:
Post a Comment