Class: Cartesius::Point

Inherits:
Object
  • Object
show all
Includes:
Numerificator
Defined in:
lib/cartesius/point.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#xObject (readonly)

Returns the value of attribute x.



6
7
8
# File 'lib/cartesius/point.rb', line 6

def x
  @x
end

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

.originObject



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

Returns:

  • (Boolean)


32
33
34
# File 'lib/cartesius/point.rb', line 32

def congruent?(point)
  point.instance_of?(self.class)
end

#origin?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/cartesius/point.rb', line 20

def origin?
  self == Point.origin
end

#to_coordinatesObject



24
25
26
# File 'lib/cartesius/point.rb', line 24

def to_coordinates
  "(#{stringfy(x)}; #{stringfy(y)})"
end

#to_equationObject



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