Class ChunkyPNG::Vector
In: lib/chunky_png/vector.rb
Parent: Object

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.

Methods

==   []   dimension   each   each_edge   edges   eql?   height   length   max_x   max_y   min_x   min_y   multiple_from_array   multiple_from_string   new   offset   width   x_range   y_range  

Included Modules

Enumerable

Attributes

points  [R]  @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.

[Source]

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

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

[Source]

     # File lib/chunky_png/vector.rb, line 182
182:     def self.multiple_from_string(source_str)
183:       multiple_from_array(source_str.scan(/[\(\[\{]?(\d+)\s*[,x]?\s*(\d+)[\)\]\}]?/))
184:     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

[Source]

    # File lib/chunky_png/vector.rb, line 47
47:     def initialize(points = [])
48:       @points = points
49:     end

Public Instance methods

==(other)

Alias for eql?

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.

[Source]

    # File lib/chunky_png/vector.rb, line 70
70:     def [](index)
71:       points[index]
72:     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, line 163
163:     def dimension
164:       ChunkyPNG::Dimension.new(width, height)
165:     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, line 92
92:     def each(&block)
93:       points.each(&block)
94:     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, line 61
61:     def each_edge(close = true)
62:       raise ChunkyPNG::ExpectationFailed, "Not enough points in this path to draw an edge!" if length < 2
63:       points.each_cons(2) { |a, b| yield(a, b) }
64:       yield(points.last, points.first) if close
65:     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, line 79
79:     def edges(close = true)
80:       Enumerator.new(self, :each_edge, close)
81:     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, line 99
 99:     def eql?(other)
100:       other.points == points
101:     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, line 157
157:     def height
158:       1 + (max_y - min_y)
159:     end

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

[Source]

    # File lib/chunky_png/vector.rb, line 85
85:     def length
86:       points.length
87:     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, line 125
125:     def max_x
126:       x_range.last
127:     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, line 137
137:     def max_y
138:       y_range.last
139:     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, line 119
119:     def min_x
120:       x_range.first
121:     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, line 131
131:     def min_y
132:       y_range.first
133:     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, line 145
145:     def offset
146:       ChunkyPNG::Point.new(min_x, min_y)
147:     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, line 151
151:     def width
152:       1 + (max_x - min_x)
153:     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, line 107
107:     def x_range
108:       Range.new(*points.map { |p| p.x }.minmax)
109:     end

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

[Source]

     # File lib/chunky_png/vector.rb, line 113
113:     def y_range
114:       Range.new(*points.map { |p| p.y }.minmax)
115:     end

[Validate]