Class: ChunkyPNG::Point

Inherits:
Object
  • Object
show all
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.

See Also:

Constant Summary

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Point) initialize(x, y)

Initializes a new point instance.

Parameters:

  • x (Integer, :to_i)

    The x-coordinate.

  • y (Integer, :to_i)

    The y-coordinate.



86
87
88
# File 'lib/chunky_png/point.rb', line 86

def initialize(x, y)
  @x, @y = x.to_i, y.to_i
end

Instance Attribute Details

- (Integer) x

Returns The x-coordinate of the point.

Returns:

  • (Integer)

    The x-coordinate of the point.



78
79
80
# File 'lib/chunky_png/point.rb', line 78

def x
  @x
end

- (Integer) y

Returns The y-coordinate of the point.

Returns:

  • (Integer)

    The y-coordinate of the point.



81
82
83
# File 'lib/chunky_png/point.rb', line 81

def y
  @y
end

Instance Method Details

- (-1, ...) <=>(other)

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.

Parameters:

Returns:

  • (-1, 0, 1)

    -1 If this point comes before the other one, 1 if after, and 0 if the points are identical.



108
109
110
# File 'lib/chunky_png/point.rb', line 108

def <=>(other)
  ((y <=> other.y) == 0) ? x <=> other.x : y <=> other.y
end

- (true, false) eql?(other) Also known as: ==

Checks whether 2 points are identical.

Returns:

  • (true, false)

    true iff the x and y coordinates match



92
93
94
# File 'lib/chunky_png/point.rb', line 92

def eql?(other)
  other.x == x && other.y == y
end

- (Array) to_a Also known as: to_ary

Converts the point instance to an array.

Returns:

  • (Array)

    A 2-element array, i.e. [x, y].



114
115
116
# File 'lib/chunky_png/point.rb', line 114

def to_a
  [x, y]
end

- (true, false) within_bounds?(*dimension_like)

Checks whether the point falls into a dimension

Parameters:

  • dimension_like (ChunkyPNG::Dimension, ...)

    The dimension of which the bounds should be taken for the check.

Returns:

  • (true, false)

    true iff the x and y coordinate fall width the width and height of the dimension.



125
126
127
# File 'lib/chunky_png/point.rb', line 125

def within_bounds?(*dimension_like)
  ChunkyPNG::Dimension(*dimension_like).include?(self)
end