Module: NPoint

Defined in:
lib/bezier_curve/n_point.rb

Overview

Extends an array to treat it as an n-dimensional point. Should not occlude any original Array methods

Instance Method Summary collapse

Instance Method Details

#angle_to(a, b) ⇒ Object

calculates the angle formed by this point and two others in radians



13
14
15
16
17
18
19
# File 'lib/bezier_curve/n_point.rb', line 13

def angle_to(a,b)
  a,b = *[a,b].map(&:to_np)
  d0,d1 = dist_from(a), a.dist_from(b)
  z = self.to(a,1+d1/d0)
  dz = z.dist_from(b)
  Math.asin(dz/2/d1)*2
end

#dist_from(other) ⇒ Object

calculates how far this point is from ‘other`



7
8
9
10
# File 'lib/bezier_curve/n_point.rb', line 7

def dist_from(other)
  raise "Dimension counts don't match! (%i and %i)" %[size,other.size] unless size == other.size
  zip(other).map{|a,b|(a-b)**2}.inject{|a,b|a+b}**0.5
end

#to(other, t) ⇒ Object

calculates the new point on the vector of self->other, scaled by ‘t`. t=0 returns `self`, t=.5 is the midpoint, t=1 is `other`.



23
24
25
# File 'lib/bezier_curve/n_point.rb', line 23

def to(other, t)
  self.zip(other).map{|a,b|t*(b-a)+a}.to_np
end

#to_npObject

convert to NPoint; returns self



28
29
30
# File 'lib/bezier_curve/n_point.rb', line 28

def to_np
  self
end

#xObject

get the x value



33
# File 'lib/bezier_curve/n_point.rb', line 33

def x() self[0]; end

#yObject

get the y value



35
# File 'lib/bezier_curve/n_point.rb', line 35

def y() self[1]; end

#zObject

get the z value



37
# File 'lib/bezier_curve/n_point.rb', line 37

def z() self[2]; end