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

x[RW]

@return [Integer] The x-coordinate of the point.

y[RW]

@return [Integer] The y-coordinate of the point.

Public Class Methods

new(x, y) click to toggle source

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, line 72
def initialize(x, y)
  @x, @y = x.to_i, y.to_i
end

Public Instance Methods

<=>(other) click to toggle source

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, line 94
def <=>(other)
  ((y <=> other.y) == 0) ? x <=> other.x : y <=> other.y
end
==(other)
Alias for: eql?
eql?(other) click to toggle source

Checks whether 2 points are identical. @return [true, false] true iff the x and y coordinates match

# File lib/chunky_png/point.rb, line 78
def eql?(other)
  other.x == x && other.y == y
end
Also aliased as: ==
to_a() click to toggle source

Converts the point instance to an array. @return [Array] A 2-element array, i.e. [x, y].

# File lib/chunky_png/point.rb, line 100
def to_a
  [x, y]
end
Also aliased as: to_ary
to_ary()
Alias for: to_a
within_bounds?(*dimension_like) click to toggle source

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, line 111
def within_bounds?(*dimension_like)
  ChunkyPNG::Dimension(*dimension_like).include?(self)
end