Class: ChunkyPNG::Point
- Inherits:
-
Object
- Object
- ChunkyPNG::Point
- Defined in:
- lib/chunky_png/point.rb
Overview
Simple class that represents a point on a canvas using an x and y coordinate.
This class implements some basic methods to handle comparison, the splat operator and bounds checking that make it easier to work with coordinates.
Instance Attribute Summary collapse
-
#x ⇒ Integer
The x-coordinate of the point.
-
#y ⇒ Integer
The y-coordinate of the point.
Instance Method Summary collapse
-
#<=>(other) ⇒ -1, ...
Compares 2 points.
-
#eql?(other) ⇒ true, false
(also: #==)
Checks whether 2 points are identical.
-
#initialize(x, y) ⇒ Point
constructor
Initializes a new point instance.
-
#to_a ⇒ Array
(also: #to_ary)
Converts the point instance to an array.
-
#within_bounds?(*dimension_like) ⇒ true, false
Checks whether the point falls into a dimension.
Constructor Details
#initialize(x, y) ⇒ Point
Initializes a new point instance.
87 88 89 |
# File 'lib/chunky_png/point.rb', line 87 def initialize(x, y) @x, @y = x.to_i, y.to_i end |
Instance Attribute Details
#x ⇒ Integer
Returns The x-coordinate of the point.
79 80 81 |
# File 'lib/chunky_png/point.rb', line 79 def x @x end |
#y ⇒ Integer
Returns The y-coordinate of the point.
82 83 84 |
# File 'lib/chunky_png/point.rb', line 82 def y @y end |
Instance Method Details
#<=>(other) ⇒ -1, ...
Compares 2 points.
It will first compare the y coordinate, and it only takes the x-coordinate into account if the y-coordinates of the points are identical. This way, an array of points will be sorted into the order in which they would occur in the pixels array returned by Canvas#pixels.
109 110 111 |
# File 'lib/chunky_png/point.rb', line 109 def <=>(other) (y <=> other.y) == 0 ? x <=> other.x : y <=> other.y end |
#eql?(other) ⇒ true, false Also known as: ==
Checks whether 2 points are identical.
93 94 95 |
# File 'lib/chunky_png/point.rb', line 93 def eql?(other) other.x == x && other.y == y end |
#to_a ⇒ Array Also known as: to_ary
Converts the point instance to an array.
115 116 117 |
# File 'lib/chunky_png/point.rb', line 115 def to_a [x, y] end |