Sha256: 34ba2f62886ac4f4bb365b145b48dce9e161241015600368ab96db26ccdbf5d4

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module Triangular
  class Line
    
    attr_accessor :start, :end
    
    def initialize(line_start, line_end)
      @start = line_start
      @end = line_end
    end
    
    def ==(other)
      return false unless other.is_a?(Line)
      self.start == other.start && self.end == other.end
    end
    
    def intersects_z?(z_plane)
      if (@start.z >= z_plane && @end.z <= z_plane) || (@start.z <= z_plane && @end.z >= z_plane)
        true
      else
        false
      end
    end
    
    def intersection_at_z(z_plane)
      return nil if !self.intersects_z?(z_plane)
      raise "Cannot calculate intersection for line that lies on the target Z plane" if @start.z == z_plane && @end.z == z_plane
      
      x_intersect = (@end.x - @start.x) / (@end.z - @start.z) * (z_plane - @start.z) + @start.x
      y_intersect = (@end.y - @start.y) / (@end.z - @start.z) * (z_plane - @start.z) + @start.y
      
      Point.new(x_intersect, y_intersect, z_plane)
    end
    
    def to_svg_path
      "<path d=\"M #{@start.x} #{@start.y} L #{@end.x} #{@end.y}\" fill=\"none\" stroke=\"black\" stroke-width=\"1\" />"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
triangular-0.0.1 lib/triangular/line.rb