Class: Cartesius::Point
- Inherits:
-
Object
- Object
- Cartesius::Point
- Includes:
- Numerificator
- Defined in:
- lib/cartesius/point.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #==(point) ⇒ Object (also: #eql?)
- #congruent?(point) ⇒ Boolean
-
#initialize(x:, y:) ⇒ Point
constructor
A new instance of Point.
- #origin? ⇒ Boolean
- #to_coordinates ⇒ Object
- #to_equation ⇒ Object
Constructor Details
#initialize(x:, y:) ⇒ Point
Returns a new instance of Point.
8 9 10 |
# File 'lib/cartesius/point.rb', line 8 def initialize(x:, y:) @x, @y = x.to_r, y.to_r end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
6 7 8 |
# File 'lib/cartesius/point.rb', line 6 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
6 7 8 |
# File 'lib/cartesius/point.rb', line 6 def y @y end |
Class Method Details
.distance(point1, point2) ⇒ Object
16 17 18 |
# File 'lib/cartesius/point.rb', line 16 def self.distance(point1, point2) Math.sqrt((point1.x - point2.x) ** 2 + (point1.y - point2.y) ** 2) end |
.origin ⇒ Object
12 13 14 |
# File 'lib/cartesius/point.rb', line 12 def self.origin new(x: 0, y: 0) end |
Instance Method Details
#==(point) ⇒ Object Also known as: eql?
36 37 38 39 |
# File 'lib/cartesius/point.rb', line 36 def == (point) point.instance_of?(self.class) and point.x == @x and point.y == @y end |
#congruent?(point) ⇒ Boolean
32 33 34 |
# File 'lib/cartesius/point.rb', line 32 def congruent?(point) point.instance_of?(self.class) end |
#origin? ⇒ Boolean
20 21 22 |
# File 'lib/cartesius/point.rb', line 20 def origin? self == Point.origin end |
#to_coordinates ⇒ Object
24 25 26 |
# File 'lib/cartesius/point.rb', line 24 def to_coordinates "(#{stringfy(x)}; #{stringfy(y)})" end |
#to_equation ⇒ Object
28 29 30 |
# File 'lib/cartesius/point.rb', line 28 def to_equation equationfy('x^2' => 1, 'y^2' => 1, 'x' => -2 * @x, 'y' => -2 * @y, '1' => @x ** 2 + @y ** 2) end |