Class: ChunkyPNG::Vector
- Inherits:
-
Object
- Object
- ChunkyPNG::Vector
- Includes:
- Enumerable
- Defined in:
- lib/chunky_png/vector.rb
Overview
Instance Attribute Summary collapse
-
#points ⇒ Array<ChunkyPNG::Point>
readonly
The array that holds all the points in this vector.
Class Method Summary collapse
-
.multiple_from_array(source) ⇒ Array<ChunkyPNG::Point>
The list of points interpreted from the input array.
-
.multiple_from_string(source_str) ⇒ Array<ChunkyPNG::Point>
The list of points parsed from the string.
Instance Method Summary collapse
-
#[](index) ⇒ ChunkyPNG::Point
Returns the point with the given indexof this vector.
-
#dimension ⇒ ChunkyPNG::Dimension
Returns the dimension of the minimal bounding rectangle of the points in this vector.
-
#each {|ChunkyPNG::Point| ... }
Iterates over all the points in this vector.
-
#each_edge(close = true) {|points.last, points.first| ... }
Iterates over all the edges in this vector.
-
#edges(close = true) ⇒ Enumerator
Returns an enumerator that will iterate over all the edges in this vector.
-
#eql?(other) ⇒ true, false
(also: #==)
Comparison between two vectors for quality.
-
#height ⇒ Integer
Returns the height of the minimal bounding box of all the points in this vector.
-
#initialize(points = []) ⇒ Vector
constructor
Initializes a vector based on a list of Point instances.
-
#length ⇒ Integer
Returns the number of points in this vector.
-
#max_x ⇒ Integer
Finds the highest x-coordinate in this vector.
-
#max_y ⇒ Integer
Finds the highest y-coordinate in this vector.
-
#min_x ⇒ Integer
Finds the lowest x-coordinate in this vector.
-
#min_y ⇒ Integer
Finds the lowest y-coordinate in this vector.
-
#offset ⇒ ChunkyPNG::Point
Returns the offset from (0,0) of the minimal bounding box of all the points in this vector.
-
#width ⇒ Integer
Returns the width of the minimal bounding box of all the points in this vector.
-
#x_range ⇒ Range
Returns the range in x-coordinates for all the points in this vector.
-
#y_range ⇒ Range
Returns the range in y-coordinates for all the points in this vector.
Constructor Details
#initialize(points = []) ⇒ Vector
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.
46 47 48 |
# File 'lib/chunky_png/vector.rb', line 46 def initialize(points = []) @points = points end |
Instance Attribute Details
#points ⇒ Array<ChunkyPNG::Point> (readonly)
Returns The array that holds all the points in this vector.
38 39 40 |
# File 'lib/chunky_png/vector.rb', line 38 def points @points end |
Class Method Details
.multiple_from_array(source) ⇒ Array<ChunkyPNG::Point>
Returns The list of points interpreted from the input array.
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/chunky_png/vector.rb', line 167 def self.multiple_from_array(source) return [] if source.empty? if source.first.is_a?(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 |
.multiple_from_string(source_str) ⇒ Array<ChunkyPNG::Point>
Returns The list of points parsed from the string.
181 182 183 |
# File 'lib/chunky_png/vector.rb', line 181 def self.multiple_from_string(source_str) multiple_from_array(source_str.scan(/[\(\[\{]?(\d+)\s*[,x]?\s*(\d+)[\)\]\}]?/)) end |
Instance Method Details
#[](index) ⇒ ChunkyPNG::Point
Returns the point with the given indexof this vector.
69 70 71 |
# File 'lib/chunky_png/vector.rb', line 69 def [](index) points[index] end |
#dimension ⇒ ChunkyPNG::Dimension
Returns the dimension of the minimal bounding rectangle of the points in this vector.
162 163 164 |
# File 'lib/chunky_png/vector.rb', line 162 def dimension ChunkyPNG::Dimension.new(width, height) end |
#each {|ChunkyPNG::Point| ... }
This method returns an undefined value.
Iterates over all the points in this vector
91 92 93 |
# File 'lib/chunky_png/vector.rb', line 91 def each(&block) points.each(&block) end |
#each_edge(close = true) {|points.last, points.first| ... }
This method returns an undefined value.
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
60 61 62 63 64 |
# File 'lib/chunky_png/vector.rb', line 60 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 |
#edges(close = true) ⇒ Enumerator
Returns an enumerator that will iterate over all the edges in this vector.
78 79 80 |
# File 'lib/chunky_png/vector.rb', line 78 def edges(close = true) to_enum(:each_edge, close) end |
#eql?(other) ⇒ true, false Also known as: ==
Comparison between two vectors for quality.
98 99 100 |
# File 'lib/chunky_png/vector.rb', line 98 def eql?(other) other.points == points end |
#height ⇒ Integer
Returns the height of the minimal bounding box of all the points in this vector.
156 157 158 |
# File 'lib/chunky_png/vector.rb', line 156 def height 1 + (max_y - min_y) end |
#length ⇒ Integer
Returns the number of points in this vector.
84 85 86 |
# File 'lib/chunky_png/vector.rb', line 84 def length points.length end |
#max_x ⇒ Integer
Finds the highest x-coordinate in this vector.
124 125 126 |
# File 'lib/chunky_png/vector.rb', line 124 def max_x x_range.last end |
#max_y ⇒ Integer
Finds the highest y-coordinate in this vector.
136 137 138 |
# File 'lib/chunky_png/vector.rb', line 136 def max_y y_range.last end |
#min_x ⇒ Integer
Finds the lowest x-coordinate in this vector.
118 119 120 |
# File 'lib/chunky_png/vector.rb', line 118 def min_x x_range.first end |
#min_y ⇒ Integer
Finds the lowest y-coordinate in this vector.
130 131 132 |
# File 'lib/chunky_png/vector.rb', line 130 def min_y y_range.first end |
#offset ⇒ ChunkyPNG::Point
Returns the offset from (0,0) of the minimal bounding box of all the points in this vector
144 145 146 |
# File 'lib/chunky_png/vector.rb', line 144 def offset ChunkyPNG::Point.new(min_x, min_y) end |
#width ⇒ Integer
Returns the width of the minimal bounding box of all the points in this vector.
150 151 152 |
# File 'lib/chunky_png/vector.rb', line 150 def width 1 + (max_x - min_x) end |
#x_range ⇒ Range
Returns the range in x-coordinates for all the points in this vector.
106 107 108 |
# File 'lib/chunky_png/vector.rb', line 106 def x_range Range.new(*points.map { |p| p.x }.minmax) end |
#y_range ⇒ Range
Returns the range in y-coordinates for all the points in this vector.
112 113 114 |
# File 'lib/chunky_png/vector.rb', line 112 def y_range Range.new(*points.map { |p| p.y }.minmax) end |