Class: BracketNotation::Geometry::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/bracket_notation/geometry/point.rb

Overview

Point represents a point on a cartesian plane using an x and y fields.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0) ⇒ Point

Returns a new instance of Point.



35
36
37
38
# File 'lib/bracket_notation/geometry/point.rb', line 35

def initialize(x = 0, y = 0)
  @x = x
  @y = y
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



33
34
35
# File 'lib/bracket_notation/geometry/point.rb', line 33

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



33
34
35
# File 'lib/bracket_notation/geometry/point.rb', line 33

def y
  @y
end

Instance Method Details

#==(rvalue) ⇒ Object

Test to see if two points are equal, where equality is defined as having identical X and Y values



42
43
44
# File 'lib/bracket_notation/geometry/point.rb', line 42

def ==(rvalue)
  @x == rvalue.x && @y == rvalue.y
end

#inspectObject Also known as: to_s



62
63
64
# File 'lib/bracket_notation/geometry/point.rb', line 62

def inspect
  "{x: #{@x}, y: #{@y}}"
end

#point_by_adding_to_x(delta_x) ⇒ Object

Create a new point by adding the given amount to the current point’s X value



47
48
49
# File 'lib/bracket_notation/geometry/point.rb', line 47

def point_by_adding_to_x(delta_x)
  self.class.new(@x + delta_x, @y)
end

#point_by_adding_to_x_and_y(delta_x, delta_y) ⇒ Object

Create a new point by adding the given amounts to the current point’s X and Y values



58
59
60
# File 'lib/bracket_notation/geometry/point.rb', line 58

def point_by_adding_to_x_and_y(delta_x, delta_y)
  self.class.new(@x + delta_x, @y + delta_y)
end

#point_by_adding_to_y(delta_y) ⇒ Object

Create a new point by adding the given amount to the current point’s Y value



52
53
54
# File 'lib/bracket_notation/geometry/point.rb', line 52

def point_by_adding_to_y(delta_y)
  self.class.new(@x, @y + delta_y)
end