class ChunkyPNG::Point
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.
@see ChunkyPNG.Point
Constants
- POINT_REGEXP
@return [Regexp] The regexp to parse points from a string. @private
Attributes
@return [Integer] The x-coordinate of the point.
@return [Integer] The y-coordinate of the point.
Public Class Methods
Initializes a new point instance. @param [Integer, :to_i] x The x-coordinate. @param [Integer, :to_i] y The y-coordinate.
# File lib/chunky_png/point.rb 87 def initialize(x, y) 88 @x, @y = x.to_i, y.to_i 89 end
Public Instance Methods
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 {ChunkyPNG::Canvas#pixels}.
@param [ChunkyPNG::Point] other The point to compare this point with. @return [-1, 0, 1] -1
If this point comes before the other one, 1
if after, and <tt>0</tt> if the points are identical.
# File lib/chunky_png/point.rb 109 def <=>(other) 110 (y <=> other.y) == 0 ? x <=> other.x : y <=> other.y 111 end
Checks whether 2 points are identical. @return [true, false] true
iff the x and y coordinates match
# File lib/chunky_png/point.rb 93 def eql?(other) 94 other.x == x && other.y == y 95 end
Converts the point instance to an array. @return [Array] A 2-element array, i.e. [x, y]
.
# File lib/chunky_png/point.rb 115 def to_a 116 [x, y] 117 end
Checks whether the point falls into a dimension @param [ChunkyPNG::Dimension, …] dimension_like The dimension of which the bounds
should be taken for the check.
@return [true, false] true
iff the x and y coordinate fall width the width
and height of the dimension.
# File lib/chunky_png/point.rb 126 def within_bounds?(*dimension_like) 127 ChunkyPNG::Dimension(*dimension_like).include?(self) 128 end