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
Source
# File lib/chunky_png/vector.rb 167 def self.multiple_from_array(source) 168 return [] if source.empty? 169 if source.first.is_a?(Numeric) || ( source.first.respond_to?(:=~) && source.first =~ /^\d+$/ ) 170 raise ArgumentError, "The points array is expected to have an even number of items!" if source.length % 2 != 0 171 172 points = [] 173 source.each_slice(2) { |x, y| points << ChunkyPNG::Point.new(x, y) } 174 return points 175 else 176 source.map { |p| ChunkyPNG::Point(p) } 177 end 178 end
@return [Array<ChunkyPNG::Point>] The list of points interpreted from the input array.
Source
# File lib/chunky_png/vector.rb 181 def self.multiple_from_string(source_str) 182 multiple_from_array(source_str.scan(/[\(\[\{]?(\d+)\s*[,x]?\s*(\d+)[\)\]\}]?/)) 183 end
@return [Array<ChunkyPNG::Point>] The list of points parsed from the string.
Source
# File lib/chunky_png/vector.rb 46 def initialize(points = []) 47 @points = points 48 end
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
Public Instance Methods
Source
# File lib/chunky_png/vector.rb 69 def [](index) 70 points[index] 71 end
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.
Source
# File lib/chunky_png/vector.rb 162 def dimension 163 ChunkyPNG::Dimension.new(width, height) 164 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
Source
# File lib/chunky_png/vector.rb 91 def each(&block) 92 points.each(&block) 93 end
Iterates over all the points in this vector @yield [ChunkyPNG::Point] The points in the correct order. @return [void]
Source
# File lib/chunky_png/vector.rb 60 def each_edge(close = true) 61 raise ChunkyPNG::ExpectationFailed, "Not enough points in this path to draw an edge!" if length < 2 62 points.each_cons(2) { |a, b| yield(a, b) } 63 yield(points.last, points.first) if close 64 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
Source
# File lib/chunky_png/vector.rb 78 def edges(close = true) 79 to_enum(:each_edge, close) 80 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
Source
# File lib/chunky_png/vector.rb 98 def eql?(other) 99 other.points == points 100 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
Source
# File lib/chunky_png/vector.rb 156 def height 157 1 + (max_y - min_y) 158 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.
Source
# File lib/chunky_png/vector.rb 84 def length 85 points.length 86 end
Returns the number of points in this vector. @return [Integer] The length of the points array.
Source
# File lib/chunky_png/vector.rb 124 def max_x 125 x_range.last 126 end
Finds the highest x-coordinate in this vector. @return [Integer] The highest x-coordinate of all the points in the vector.
Source
# File lib/chunky_png/vector.rb 136 def max_y 137 y_range.last 138 end
Finds the highest y-coordinate in this vector. @return [Integer] The highest y-coordinate of all the points in the vector.
Source
# File lib/chunky_png/vector.rb 118 def min_x 119 x_range.first 120 end
Finds the lowest x-coordinate in this vector. @return [Integer] The lowest x-coordinate of all the points in the vector.
Source
# File lib/chunky_png/vector.rb 130 def min_y 131 y_range.first 132 end
Finds the lowest y-coordinate in this vector. @return [Integer] The lowest y-coordinate of all the points in the vector.
Source
# File lib/chunky_png/vector.rb 144 def offset 145 ChunkyPNG::Point.new(min_x, min_y) 146 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.
Source
# File lib/chunky_png/vector.rb 150 def width 151 1 + (max_x - min_x) 152 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.
Source
# File lib/chunky_png/vector.rb 106 def x_range 107 Range.new(*points.map { |p| p.x }.minmax) 108 end
Returns the range in x-coordinates for all the points in this vector. @return [Range] The (inclusive) range of x-coordinates.
Source
# File lib/chunky_png/vector.rb 112 def y_range 113 Range.new(*points.map { |p| p.y }.minmax) 114 end
Returns the range in y-coordinates for all the points in this vector. @return [Range] The (inclusive) range of y-coordinates.