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
@return [Array<ChunkyPNG::Point>] The array that holds all the points in this vector.
Public Class Methods
@return [Array<ChunkyPNG::Point>] The list of points interpreted from the input array.
# File lib/chunky_png/vector.rb, line 168 def self.multiple_from_array(source) return [] if source.empty? if source.first.kind_of?(Numeric) || source.first =~ /^\d+$/ raise ArgumentError, "The points array is expected to have an even number of items!" if source.length % 2 != 0 points = [] source.each_slice(2) { |x, y| points << ChunkyPNG::Point.new(x, y) } return points else source.map { |p| ChunkyPNG::Point(p) } end end
@return [Array<ChunkyPNG::Point>] The list of points parsed from the string.
# File lib/chunky_png/vector.rb, line 182 def self.multiple_from_string(source_str) multiple_from_array(source_str.scan(/[\(\[\{]?(\d+)\s*[,x]?\s*(\d+)[\)\]\}]?/)) end
Public Instance Methods
Returns the point with the given indexof this vector. @param [Integer] index The 0-based index of the point in this vector. @param [ChunkyPNG::Point] The point instance.
# File lib/chunky_png/vector.rb, line 70 def [](index) points[index] end
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, line 163 def dimension ChunkyPNG::Dimension.new(width, height) end
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, line 92 def each(&block) points.each(&block) end
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, line 61 def each_edge(close = true) raise ChunkyPNG::ExpectationFailed, "Not enough points in this path to draw an edge!" if length < 2 points.each_cons(2) { |a, b| yield(a, b) } yield(points.last, points.first) if close end
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, line 79 def edges(close = true) Enumerator.new(self, :each_edge, close) end
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, line 99 def eql?(other) other.points == points end
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, line 157 def height 1 + (max_y - min_y) end
Returns the number of points in this vector. @return [Integer] The length of the points array.
# File lib/chunky_png/vector.rb, line 85 def length points.length end
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, line 125 def max_x x_range.last end
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, line 137 def max_y y_range.last end
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, line 119 def min_x x_range.first end
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, line 131 def min_y y_range.first end
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, line 145 def offset ChunkyPNG::Point.new(min_x, min_y) end
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, line 151 def width 1 + (max_x - min_x) end
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, line 107 def x_range Range.new(*points.map { |p| p.x }.minmax) end
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, line 113 def y_range Range.new(*points.map { |p| p.y }.minmax) end