Class: CfSim::Link
- Inherits:
-
Object
- Object
- CfSim::Link
- Defined in:
- lib/cf_sim/link.rb
Instance Attribute Summary collapse
-
#destination_point ⇒ Object
readonly
Returns the value of attribute destination_point.
-
#source_point ⇒ Object
readonly
Returns the value of attribute source_point.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(source_point, destination_point) ⇒ Link
constructor
A new instance of Link.
- #intersected?(other) ⇒ Boolean
- #length ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(source_point, destination_point) ⇒ Link
Returns a new instance of Link.
4 5 6 7 8 |
# File 'lib/cf_sim/link.rb', line 4 def initialize(source_point, destination_point) raise 'Cannot link to same point' if source_point == destination_point @source_point = source_point @destination_point = destination_point end |
Instance Attribute Details
#destination_point ⇒ Object (readonly)
Returns the value of attribute destination_point.
2 3 4 |
# File 'lib/cf_sim/link.rb', line 2 def destination_point @destination_point end |
#source_point ⇒ Object (readonly)
Returns the value of attribute source_point.
2 3 4 |
# File 'lib/cf_sim/link.rb', line 2 def source_point @source_point end |
Instance Method Details
#==(other) ⇒ Object
14 15 16 |
# File 'lib/cf_sim/link.rb', line 14 def ==(other) eql?(other) end |
#eql?(other) ⇒ Boolean
18 19 20 21 |
# File 'lib/cf_sim/link.rb', line 18 def eql?(other) (source_point == other.source_point && destination_point == other.destination_point) || (source_point == other.destination_point && destination_point == other.source_point) end |
#hash ⇒ Object
23 24 25 |
# File 'lib/cf_sim/link.rb', line 23 def hash source_point.hash + destination_point.hash end |
#intersected?(other) ⇒ Boolean
27 28 29 30 31 32 33 |
# File 'lib/cf_sim/link.rb', line 27 def intersected?(other) ta = (other.source_point.x - other.destination_point.x) * (source_point.y - other.source_point.y) + (other.source_point.y - other.destination_point.y) * (other.source_point.x - source_point.x) tb = (other.source_point.x - other.destination_point.x) * (destination_point.y - other.source_point.y) + (other.source_point.y - other.destination_point.y) * (other.source_point.x - destination_point.x) tc = (source_point.x - destination_point.x) * (other.source_point.y - source_point.y) + (source_point.y - destination_point.y) * (source_point.x - other.source_point.x) td = (source_point.x - destination_point.x) * (other.destination_point.y - source_point.y) + (source_point.y - destination_point.y) * (source_point.x - other.destination_point.x) tc * td < 0 && ta * tb < 0 end |
#length ⇒ Object
10 11 12 |
# File 'lib/cf_sim/link.rb', line 10 def length @length ||= Math.sqrt((source_point.x - destination_point.x) ** 2 + (source_point.y - destination_point.y) ** 2) end |
#to_s ⇒ Object
35 36 37 |
# File 'lib/cf_sim/link.rb', line 35 def to_s "#{source_point}->#{destination_point}" end |