Sha256: 65c319c65de8f90296f836ffb7da57c3544bb01bc80c10030221fb1c2c27f6ad
Contents?: true
Size: 1.09 KB
Versions: 2
Compression:
Stored size: 1.09 KB
Contents
require_relative 'point' module Geometry =begin rdoc An edge. It's a line segment between 2 points. Generally part of a {Polygon}. == Usage edge = Geometry::Edge([1,1], [2,2]) =end class Edge attr_reader :first, :last # Construct a new {Edge} object from any two things that can be converted # to a {Point}. def initialize(point0, point1) @first, @last = [point0, point1].map {|p| p.is_a?(Point) ? p : Point[p] } end # Two Edges are equal if both have equal {Point}s in the same order def ==(other) (@first == other.first) && (@last == other.last) end # Return a new {Edge} with swapped endpoints def reverse Edge.new(@last, @first) end # In-place swap the endpoints def reverse! @first, @last = @last, @first self end # Return the {Edge}'s length along the Y axis def height (@first.y - @last.y).abs end # Return the {Edge}'s length along the X axis def width (@first.x - @last.x).abs end def inspect 'Edge(' + @first.inspect + ', ' + @last.inspect + ')' end alias :to_s :inspect def to_a [@first, @last] end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
geometry-3 | lib/geometry/edge.rb |
geometry-2 | lib/geometry/edge.rb |