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 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
@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
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. @return [ChunkyPNG::Point] The point instance.
# File lib/chunky_png/vector.rb 67 def [](index) 68 points[index] 69 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 160 def dimension 161 ChunkyPNG::Dimension.new(width, height) 162 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 89 def each(&block) 90 points.each(&block) 91 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 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
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
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
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
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
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
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
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
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
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
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
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
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