Class ChunkyPNG::Point
In: lib/chunky_png/point.rb
Parent: Object

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

Methods

<=>   ==   eql?   new   to_a   to_ary   within_bounds?  

Constants

POINT_REGEXP = /^[\(\[\{]?(\d+)\s*[,]?\s*(\d+)[\)\]\}]?$/   @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

Initializes a new point instance. @param [Integer, :to_i] x The x-coordinate. @param [Integer, :to_i] y The y-coordinate.

[Source]

    # File lib/chunky_png/point.rb, line 72
72:     def initialize(x, y)
73:       @x, @y = x.to_i, y.to_i
74:     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.

[Source]

    # File lib/chunky_png/point.rb, line 94
94:     def <=>(other)
95:       ((y <=> other.y) == 0) ? x <=> other.x : y <=> other.y
96:     end
==(other)

Alias for eql?

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

[Source]

    # File lib/chunky_png/point.rb, line 78
78:     def eql?(other)
79:       other.x == x && other.y == y
80:     end

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

[Source]

     # File lib/chunky_png/point.rb, line 100
100:     def to_a
101:       [x, y]
102:     end
to_ary()

Alias for to_a

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.

[Source]

     # File lib/chunky_png/point.rb, line 111
111:     def within_bounds?(*dimension_like)
112:       ChunkyPNG::Dimension(*dimension_like).include?(self)
113:     end

[Validate]