Class: Shoes::Point

Inherits:
Object
  • Object
show all
Includes:
Common::Inspect
Defined in:
shoes-core/lib/shoes/point.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common::Inspect

#inspect

Constructor Details

#initialize(x, y) ⇒ Point

Returns a new instance of Point.



7
8
9
10
11
# File 'shoes-core/lib/shoes/point.rb', line 7

def initialize(x, y)
  @x = x
  @y = y
  @dimensions = 2
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



13
14
15
# File 'shoes-core/lib/shoes/point.rb', line 13

def x
  @x
end

#yObject

Returns the value of attribute y.



13
14
15
# File 'shoes-core/lib/shoes/point.rb', line 13

def y
  @y
end

Instance Method Details

#+(other) ⇒ Object



37
38
39
40
41
# File 'shoes-core/lib/shoes/point.rb', line 37

def +(other)
  other = other.to_a
  assert_dimensions_match(other.length)
  Shoes::Point.new(x + other[0], y + other[1])
end

#-(other) ⇒ Object



43
44
45
46
47
# File 'shoes-core/lib/shoes/point.rb', line 43

def -(other)
  other = other.to_a
  assert_dimensions_match(other.length)
  Shoes::Point.new(x - other[0], y - other[1])
end

#==(other) ⇒ Object



57
58
59
# File 'shoes-core/lib/shoes/point.rb', line 57

def ==(other)
  other.respond_to?(:x, :y) && [x, y] == [other.x, other.y]
end

#height(other = self) ⇒ Object



53
54
55
# File 'shoes-core/lib/shoes/point.rb', line 53

def height(other = self)
  (@y - other.y).abs
end

#left(other = self) ⇒ Integer

Returns if this is further left, this.x; otherwise other.x.

Parameters:

  • other (Shoes::Point) (defaults to: self)

    the other point

Returns:

  • (Integer)

    if this is further left, this.x; otherwise other.x



17
18
19
# File 'shoes-core/lib/shoes/point.rb', line 17

def left(other = self)
  [x, other.x].min
end

#to(x, y) ⇒ Shoes::Point

Creates a new point at an offset of (x,y) from this point. Positive offsets move right and down; negative offsets move left and up.

Parameters:

  • x (Integer)

    the x-component of the offset

  • y (Integer)

    the y-component of the offset

Returns:

  • (Shoes::Point)

    a new point at offset (x,y) from this point



33
34
35
# File 'shoes-core/lib/shoes/point.rb', line 33

def to(x, y)
  Shoes::Point.new(@x + x, @y + y)
end

#to_aObject



66
67
68
# File 'shoes-core/lib/shoes/point.rb', line 66

def to_a
  [x, y]
end

#to_sObject



61
62
63
64
# File 'shoes-core/lib/shoes/point.rb', line 61

def to_s
  nothing = '_'
  "(#{@x || nothing},#{@y || nothing})"
end

#top(other = self) ⇒ Integer

Returns if this is higher, this.y; otherwise other.y.

Parameters:

  • other (Shoes::Point) (defaults to: self)

    the other point

Returns:

  • (Integer)

    if this is higher, this.y; otherwise other.y



23
24
25
# File 'shoes-core/lib/shoes/point.rb', line 23

def top(other = self)
  [y, other.y].min
end

#width(other = self) ⇒ Object



49
50
51
# File 'shoes-core/lib/shoes/point.rb', line 49

def width(other = self)
  (@x - other.x).abs
end