class ChunkyPNG::Vector

Class that represents a vector of points, i.e. a list of {ChunkyPNG::Point} instances.

Vectors can be created quite flexibly. See the {ChunkyPNG.Vector} factory methods for more information on how to construct vectors.

Attributes

points[R]

@return [Array<ChunkyPNG::Point>] The array that holds all the points in this vector.

Public Class Methods

multiple_from_array(source) click to toggle source

@return [Array<ChunkyPNG::Point>] The list of points interpreted from the input array.

    # File lib/chunky_png/vector.rb
165 def self.multiple_from_array(source)
166   return [] if source.empty?
167   if source.first.is_a?(Numeric) || source.first =~ /^\d+$/
168     raise ArgumentError, "The points array is expected to have an even number of items!" if source.length % 2 != 0
169 
170     points = []
171     source.each_slice(2) { |x, y| points << ChunkyPNG::Point.new(x, y) }
172     return points
173   else
174     source.map { |p| ChunkyPNG::Point(p) }
175   end
176 end
multiple_from_string(source_str) click to toggle source

@return [Array<ChunkyPNG::Point>] The list of points parsed from the string.

    # File lib/chunky_png/vector.rb
179 def self.multiple_from_string(source_str)
180   multiple_from_array(source_str.scan(/[\(\[\{]?(\d+)\s*[,x]?\s*(\d+)[\)\]\}]?/))
181 end
new(points = []) click to toggle source

Initializes a vector based on a list of Point instances.

You usually do not want to use this method directly, but call {ChunkyPNG.Vector} instead.

@param [Array<ChunkyPNG::Point>] points @see ChunkyPNG.Vector

   # File lib/chunky_png/vector.rb
44 def initialize(points = [])
45   @points = points
46 end

Public Instance Methods

==(other)
Alias for: eql?
[](index) click to toggle source

Returns the point with the given indexof this vector. @param [Integer] index The 0-based index of the point in this vector. @return [ChunkyPNG::Point] The point instance.

   # File lib/chunky_png/vector.rb
67 def [](index)
68   points[index]
69 end
dimension() click to toggle source

Returns the dimension of the minimal bounding rectangle of the points in this vector. @return [ChunkyPNG::Dimension] The dimension instance with the width and height

    # File lib/chunky_png/vector.rb
160 def dimension
161   ChunkyPNG::Dimension.new(width, height)
162 end
each(&block) click to toggle source

Iterates over all the points in this vector @yield [ChunkyPNG::Point] The points in the correct order. @return [void]

   # File lib/chunky_png/vector.rb
89 def each(&block)
90   points.each(&block)
91 end
each_edge(close = true) { |a, b| ... } click to toggle source

Iterates over all the edges in this vector.

An edge is a combination of two subsequent points in the vector. Together, they will form a path from the first point to the last point

@param [true, false] close Whether to close the path, i.e. return an edge that connects the last

point in the vector back to the first point.

@return [void] @raise [ChunkyPNG::ExpectationFailed] if the vector contains less than two points. @see edges

   # File lib/chunky_png/vector.rb
58 def each_edge(close = true)
59   raise ChunkyPNG::ExpectationFailed, "Not enough points in this path to draw an edge!" if length < 2
60   points.each_cons(2) { |a, b| yield(a, b) }
61   yield(points.last, points.first) if close
62 end
edges(close = true) click to toggle source

Returns an enumerator that will iterate over all the edges in this vector. @param (see each_edge) @return [Enumerator] The enumerator that iterates over the edges. @raise [ChunkyPNG::ExpectationFailed] if the vector contains less than two points. @see each_edge

   # File lib/chunky_png/vector.rb
76 def edges(close = true)
77   to_enum(:each_edge, close)
78 end
eql?(other) click to toggle source

Comparison between two vectors for quality. @param [ChunkyPNG::Vector] other The vector to compare with. @return [true, false] true if the list of points are identical

   # File lib/chunky_png/vector.rb
96 def eql?(other)
97   other.points == points
98 end
Also aliased as: ==
height() click to toggle source

Returns the height of the minimal bounding box of all the points in this vector. @return [Integer] The y-distance between the points that are farthest from each other.

    # File lib/chunky_png/vector.rb
154 def height
155   1 + (max_y - min_y)
156 end
length() click to toggle source

Returns the number of points in this vector. @return [Integer] The length of the points array.

   # File lib/chunky_png/vector.rb
82 def length
83   points.length
84 end
max_x() click to toggle source

Finds the highest x-coordinate in this vector. @return [Integer] The highest x-coordinate of all the points in the vector.

    # File lib/chunky_png/vector.rb
122 def max_x
123   x_range.last
124 end
max_y() click to toggle source

Finds the highest y-coordinate in this vector. @return [Integer] The highest y-coordinate of all the points in the vector.

    # File lib/chunky_png/vector.rb
134 def max_y
135   y_range.last
136 end
min_x() click to toggle source

Finds the lowest x-coordinate in this vector. @return [Integer] The lowest x-coordinate of all the points in the vector.

    # File lib/chunky_png/vector.rb
116 def min_x
117   x_range.first
118 end
min_y() click to toggle source

Finds the lowest y-coordinate in this vector. @return [Integer] The lowest y-coordinate of all the points in the vector.

    # File lib/chunky_png/vector.rb
128 def min_y
129   y_range.first
130 end
offset() click to toggle source

Returns the offset from (0,0) of the minimal bounding box of all the points in this vector @return [ChunkyPNG::Point] A point that describes the top left corner if a

minimal bounding box would be drawn around all the points in the vector.
    # File lib/chunky_png/vector.rb
142 def offset
143   ChunkyPNG::Point.new(min_x, min_y)
144 end
width() click to toggle source

Returns the width of the minimal bounding box of all the points in this vector. @return [Integer] The x-distance between the points that are farthest from each other.

    # File lib/chunky_png/vector.rb
148 def width
149   1 + (max_x - min_x)
150 end
x_range() click to toggle source

Returns the range in x-coordinates for all the points in this vector. @return [Range] The (inclusive) range of x-coordinates.

    # File lib/chunky_png/vector.rb
104 def x_range
105   Range.new(*points.map { |p| p.x }.minmax)
106 end
y_range() click to toggle source

Returns the range in y-coordinates for all the points in this vector. @return [Range] The (inclusive) range of y-coordinates.

    # File lib/chunky_png/vector.rb
110 def y_range
111   Range.new(*points.map { |p| p.y }.minmax)
112 end