class Sangaku::Line
Public Class Methods
new(p1, p2)
click to toggle source
# File lib/sangaku/line.rb, line 5 def initialize(p1, p2) @coords = Point::convert([p1, p2]) end
Public Instance Methods
+(point)
click to toggle source
# File lib/sangaku/line.rb, line 42 def +(point) Line.new(p1 + point, p2 + point) end
-(point)
click to toggle source
# File lib/sangaku/line.rb, line 45 def -(point) Line.new(p1 - point, p2 - point) end
contain?(x = nil, y = nil)
click to toggle source
# File lib/sangaku/line.rb, line 61 def contain?(x = nil, y = nil) (x.nil? || x.between?(*[p1.x, p2.x].sort)) && (y.nil? || y.between?(*[p1.y, p2.y].sort)) end
cross(other)
click to toggle source
# File lib/sangaku/line.rb, line 57 def cross(other) self.vx * other.vy - self.vy * other.vx end
dot(other)
click to toggle source
# File lib/sangaku/line.rb, line 53 def dot(other) (self.vx * other.vx + self.vy * other.vy)/(self.length * other.length) end
get_x(y)
click to toggle source
# File lib/sangaku/line.rb, line 33 def get_x(y) ratio = p1.y != p2.y ? (y-p1.y)/(p2.y-p1.y).to_f : 0.5 p1.x + ratio * (p2.x-p1.x) end
get_y(x)
click to toggle source
# File lib/sangaku/line.rb, line 37 def get_y(x) ratio = p1.x != p2.x ? (x-p1.x)/(p2.x-p1.x).to_f : 0.5 p1.y + ratio * (p2.y-p1.y) end
h()
click to toggle source
# File lib/sangaku/line.rb, line 17 def h (p2.y - p1.y).abs end
length()
click to toggle source
# File lib/sangaku/line.rb, line 21 def length Math.sqrt(vx ** 2 + vy ** 2) end
mid()
click to toggle source
# File lib/sangaku/line.rb, line 49 def mid (p1 + p2) * 0.5 end
p1()
click to toggle source
# File lib/sangaku/line.rb, line 9 def p1; @coords[0]; end
p1=(val)
click to toggle source
# File lib/sangaku/line.rb, line 10 def p1=(val); @coords[0] = val; end
p2()
click to toggle source
# File lib/sangaku/line.rb, line 11 def p2; @coords[1]; end
p2=(val)
click to toggle source
# File lib/sangaku/line.rb, line 12 def p2=(val); @coords[1] = val; end
to_a()
click to toggle source
# File lib/sangaku/line.rb, line 66 def to_a [@p1.to_a, @p2.to_a] end
to_s()
click to toggle source
# File lib/sangaku/line.rb, line 70 def to_s "#{p1}->#{p2}" end
Also aliased as: inspect
vx()
click to toggle source
# File lib/sangaku/line.rb, line 25 def vx p2.x - p1.x end
vy()
click to toggle source
# File lib/sangaku/line.rb, line 29 def vy p2.y - p1.y end
w()
click to toggle source
# File lib/sangaku/line.rb, line 14 def w (p2.x - p1.x).abs end