Class: Tabula::Point
- Inherits:
-
Object
- Object
- Tabula::Point
- Defined in:
- lib/tabula/core/point.rb
Overview
Represents a 2D point with x and y coordinates
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #distance_squared_to(other) ⇒ Object
- #distance_to(other) ⇒ Object
- #hash ⇒ Object
-
#initialize(x, y) ⇒ Point
constructor
A new instance of Point.
- #inspect ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(x, y) ⇒ Point
Returns a new instance of Point.
8 9 10 11 |
# File 'lib/tabula/core/point.rb', line 8 def initialize(x, y) @x = x.to_f @y = y.to_f end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
6 7 8 |
# File 'lib/tabula/core/point.rb', line 6 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
6 7 8 |
# File 'lib/tabula/core/point.rb', line 6 def y @y end |
Instance Method Details
#*(other) ⇒ Object
44 45 46 |
# File 'lib/tabula/core/point.rb', line 44 def *(other) Point.new(x * other, y * other) end |
#+(other) ⇒ Object
36 37 38 |
# File 'lib/tabula/core/point.rb', line 36 def +(other) Point.new(x + other.x, y + other.y) end |
#-(other) ⇒ Object
40 41 42 |
# File 'lib/tabula/core/point.rb', line 40 def -(other) Point.new(x - other.x, y - other.y) end |
#/(other) ⇒ Object
48 49 50 |
# File 'lib/tabula/core/point.rb', line 48 def /(other) Point.new(x / other, y / other) end |
#==(other) ⇒ Object Also known as: eql?
17 18 19 20 21 |
# File 'lib/tabula/core/point.rb', line 17 def ==(other) return false unless other.is_a?(Point) x == other.x && y == other.y end |
#distance_squared_to(other) ⇒ Object
32 33 34 |
# File 'lib/tabula/core/point.rb', line 32 def distance_squared_to(other) ((x - other.x)**2) + ((y - other.y)**2) end |
#distance_to(other) ⇒ Object
28 29 30 |
# File 'lib/tabula/core/point.rb', line 28 def distance_to(other) Math.sqrt(((x - other.x)**2) + ((y - other.y)**2)) end |
#hash ⇒ Object
24 25 26 |
# File 'lib/tabula/core/point.rb', line 24 def hash [x, y].hash end |
#inspect ⇒ Object
56 57 58 |
# File 'lib/tabula/core/point.rb', line 56 def inspect to_s end |
#to_a ⇒ Object
13 14 15 |
# File 'lib/tabula/core/point.rb', line 13 def to_a [x, y] end |
#to_s ⇒ Object
52 53 54 |
# File 'lib/tabula/core/point.rb', line 52 def to_s "Point(#{x}, #{y})" end |