Class: Overlap::Segment
- Inherits:
-
Object
- Object
- Overlap::Segment
- Defined in:
- lib/overlap/segment.rb
Instance Attribute Summary collapse
-
#center ⇒ Object
readonly
Returns the value of attribute center.
-
#end_position ⇒ Object
readonly
Returns the value of attribute end_position.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#quantity ⇒ Object
readonly
Returns the value of attribute quantity.
-
#radius ⇒ Object
readonly
Returns the value of attribute radius.
-
#start_position ⇒ Object
readonly
Returns the value of attribute start_position.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(start_position, end_position) ⇒ Segment
constructor
A new instance of Segment.
- #intersection(other) ⇒ Object
- #merge!(other) ⇒ Object
- #overlap?(other) ⇒ Boolean
- #same?(other) ⇒ Boolean
- #to_a ⇒ Object
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(start_position, end_position) ⇒ Segment
Returns a new instance of Segment.
6 7 8 9 10 |
# File 'lib/overlap/segment.rb', line 6 def initialize(start_position, end_position) @start_position = start_position @end_position = end_position build! end |
Instance Attribute Details
#center ⇒ Object (readonly)
Returns the value of attribute center.
4 5 6 |
# File 'lib/overlap/segment.rb', line 4 def center @center end |
#end_position ⇒ Object (readonly)
Returns the value of attribute end_position.
4 5 6 |
# File 'lib/overlap/segment.rb', line 4 def end_position @end_position end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
4 5 6 |
# File 'lib/overlap/segment.rb', line 4 def key @key end |
#quantity ⇒ Object (readonly)
Returns the value of attribute quantity.
4 5 6 |
# File 'lib/overlap/segment.rb', line 4 def quantity @quantity end |
#radius ⇒ Object (readonly)
Returns the value of attribute radius.
4 5 6 |
# File 'lib/overlap/segment.rb', line 4 def radius @radius end |
#start_position ⇒ Object (readonly)
Returns the value of attribute start_position.
4 5 6 |
# File 'lib/overlap/segment.rb', line 4 def start_position @start_position end |
Instance Method Details
#<=>(other) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/overlap/segment.rb', line 58 def <=>(other) if start_position == other.start_position end_position <=> other.end_position else start_position <=> other.start_position end end |
#==(other) ⇒ Object Also known as: eql?
45 46 47 |
# File 'lib/overlap/segment.rb', line 45 def ==(other) self.class == other.class && to_a == other.to_a end |
#hash ⇒ Object
50 51 52 |
# File 'lib/overlap/segment.rb', line 50 def hash to_a.hash end |
#intersection(other) ⇒ Object
54 55 56 |
# File 'lib/overlap/segment.rb', line 54 def intersection(other) end_position - other.start_position end |
#merge!(other) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/overlap/segment.rb', line 12 def merge!(other) if other.start_position < start_position @start_position = other.start_position end if end_position < other.end_position @end_position = other.end_position end build! end |
#overlap?(other) ⇒ Boolean
41 42 43 |
# File 'lib/overlap/segment.rb', line 41 def overlap?(other) (center - other.center).abs.round(3) <= (radius + other.radius).round(3) end |
#same?(other) ⇒ Boolean
37 38 39 |
# File 'lib/overlap/segment.rb', line 37 def same?(other) self == other end |
#to_a ⇒ Object
33 34 35 |
# File 'lib/overlap/segment.rb', line 33 def to_a [ start_position, end_position ] end |
#to_s ⇒ Object Also known as: inspect
24 25 26 |
# File 'lib/overlap/segment.rb', line 24 def to_s "[#{start_position.to_f}, #{end_position.to_f}]" end |