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.
9 10 11 |
# File 'lib/cartesius/point.rb', line 9 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.
7 8 9 |
# File 'lib/cartesius/point.rb', line 7 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
7 8 9 |
# File 'lib/cartesius/point.rb', line 7 def y @y end |
Class Method Details
.distance(point1, point2) ⇒ Object
17 18 19 |
# File 'lib/cartesius/point.rb', line 17 def self.distance(point1, point2) Math.sqrt((point1.x - point2.x) ** 2 + (point1.y - point2.y) ** 2) end |
.origin ⇒ Object
13 14 15 |
# File 'lib/cartesius/point.rb', line 13 def self.origin new(x: 0, y: 0) end |
Instance Method Details
#==(point) ⇒ Object Also known as: eql?
37 38 39 40 |
# File 'lib/cartesius/point.rb', line 37 def == (point) point.instance_of?(self.class) && point.x == @x && point.y == @y end |
#congruent?(point) ⇒ Boolean
33 34 35 |
# File 'lib/cartesius/point.rb', line 33 def congruent?(point) point.instance_of?(self.class) end |
#origin? ⇒ Boolean
21 22 23 |
# File 'lib/cartesius/point.rb', line 21 def origin? self == Point.origin end |
#to_coordinates ⇒ Object
25 26 27 |
# File 'lib/cartesius/point.rb', line 25 def to_coordinates "(#{stringfy(x)}; #{stringfy(y)})" end |
#to_equation ⇒ Object
29 30 31 |
# File 'lib/cartesius/point.rb', line 29 def to_equation equationfy('x^2' => 1, 'y^2' => 1, 'x' => -2 * @x, 'y' => -2 * @y, '1' => @x ** 2 + @y ** 2) end |