Class: Shoes::Point
- Inherits:
-
Object
- Object
- Shoes::Point
- Includes:
- Common::Inspect
- Defined in:
- shoes-core/lib/shoes/point.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #==(other) ⇒ Object
- #height(other = self) ⇒ Object
-
#initialize(x, y) ⇒ Point
constructor
A new instance of Point.
-
#left(other = self) ⇒ Integer
If this is further left, this.x; otherwise other.x.
-
#to(x, y) ⇒ Shoes::Point
Creates a new point at an offset of (x,y) from this point.
- #to_a ⇒ Object
- #to_s ⇒ Object
-
#top(other = self) ⇒ Integer
If this is higher, this.y; otherwise other.y.
- #width(other = self) ⇒ Object
Methods included from Common::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
#x ⇒ Object
Returns the value of attribute x.
13 14 15 |
# File 'shoes-core/lib/shoes/point.rb', line 13 def x @x end |
#y ⇒ Object
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.
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.
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_a ⇒ Object
66 67 68 |
# File 'shoes-core/lib/shoes/point.rb', line 66 def to_a [x, y] end |
#to_s ⇒ Object
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.
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 |