Class: Overlap::Segment

Inherits:
Object
  • Object
show all
Defined in:
lib/overlap/segment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#centerObject (readonly)

Returns the value of attribute center.


4
5
6
# File 'lib/overlap/segment.rb', line 4

def center
  @center
end

#end_positionObject (readonly)

Returns the value of attribute end_position.


4
5
6
# File 'lib/overlap/segment.rb', line 4

def end_position
  @end_position
end

#keyObject (readonly)

Returns the value of attribute key.


4
5
6
# File 'lib/overlap/segment.rb', line 4

def key
  @key
end

#quantityObject (readonly)

Returns the value of attribute quantity.


4
5
6
# File 'lib/overlap/segment.rb', line 4

def quantity
  @quantity
end

#radiusObject (readonly)

Returns the value of attribute radius.


4
5
6
# File 'lib/overlap/segment.rb', line 4

def radius
  @radius
end

#start_positionObject (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

#hashObject


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

Returns:

  • (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

Returns:

  • (Boolean)

37
38
39
# File 'lib/overlap/segment.rb', line 37

def same?(other)
  self == other
end

#to_aObject


33
34
35
# File 'lib/overlap/segment.rb', line 33

def to_a
  [ start_position, end_position ]
end

#to_sObject 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