Class: ChunkyPNG::Dimension

Inherits:
Object
  • Object
show all
Defined in:
lib/chunky_png/dimension.rb

Overview

Class that represents the dimension of something, e.g. a Canvas.

This class contains some methods to simplify performing dimension related checks.

Constant Summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ Dimension

Initializes a new dimension instance.



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

def initialize(width, height)
  @width, @height = width.to_i, height.to_i
end

Instance Attribute Details

#heightInteger



76
77
78
# File 'lib/chunky_png/dimension.rb', line 76

def height
  @height
end

#widthInteger



73
74
75
# File 'lib/chunky_png/dimension.rb', line 73

def width
  @width
end

Instance Method Details

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

Compares the size of 2 dimensions.



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

def <=>(other)
  other.area <=> area
end

#areaInteger

Returns the area of this dimension.



87
88
89
# File 'lib/chunky_png/dimension.rb', line 87

def area
  width * height
end

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

Checks whether 2 dimensions are identical.



103
104
105
106
# File 'lib/chunky_png/dimension.rb', line 103

def eql?(other)
  return false unless other.respond_to?(:width) && other.respond_to?(:height)
  other.width == width && other.height == height
end

#include?(*point_like) ⇒ true, false

Checks whether a point is within bounds of this dimension.

See Also:



95
96
97
98
# File 'lib/chunky_png/dimension.rb', line 95

def include?(*point_like)
  point = ChunkyPNG::Point(*point_like)
  point.x >= 0 && point.x < width && point.y >= 0 && point.y < height
end

#to_aArray<Integer> Also known as: to_ary

Casts this dimension into an array.



120
121
122
# File 'lib/chunky_png/dimension.rb', line 120

def to_a
  [width, height]
end