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) ⇒ Object
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.
Methods included from Enumerable
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.
47 48 49 |
# File 'lib/chunky_png/vector.rb', line 47 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.
39 40 41 |
# File 'lib/chunky_png/vector.rb', line 39 def points @points end |
Class Method Details
.multiple_from_array(source) ⇒ Array<ChunkyPNG::Point>
Returns The list of points interpreted from the input array.
168 169 170 171 172 173 174 175 176 177 178 179 |
# 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 |
.multiple_from_string(source_str) ⇒ Array<ChunkyPNG::Point>
Returns The list of points parsed from the string.
182 183 184 |
# 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 |
Instance Method Details
#[](index) ⇒ Object
Returns the point with the given indexof this vector.
70 71 72 |
# File 'lib/chunky_png/vector.rb', line 70 def [](index) points[index] end |
#dimension ⇒ ChunkyPNG::Dimension
Returns the dimension of the minimal bounding rectangle of the points in this vector.
163 164 165 |
# File 'lib/chunky_png/vector.rb', line 163 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
92 93 94 |
# File 'lib/chunky_png/vector.rb', line 92 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
61 62 63 64 65 |
# 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 |
#edges(close = true) ⇒ Enumerator
Returns an enumerator that will iterate over all the edges in this vector.
79 80 81 |
# File 'lib/chunky_png/vector.rb', line 79 def edges(close = true) Enumerator.new(self, :each_edge, close) end |
#eql?(other) ⇒ true, false Also known as: ==
Comparison between two vectors for quality.
99 100 101 |
# File 'lib/chunky_png/vector.rb', line 99 def eql?(other) other.points == points end |
#height ⇒ Integer
Returns the height of the minimal bounding box of all the points in this vector.
157 158 159 |
# File 'lib/chunky_png/vector.rb', line 157 def height 1 + (max_y - min_y) end |
#length ⇒ Integer
Returns the number of points in this vector.
85 86 87 |
# File 'lib/chunky_png/vector.rb', line 85 def length points.length end |
#max_x ⇒ Integer
Finds the highest x-coordinate in this vector.
125 126 127 |
# File 'lib/chunky_png/vector.rb', line 125 def max_x x_range.last end |
#max_y ⇒ Integer
Finds the highest y-coordinate in this vector.
137 138 139 |
# File 'lib/chunky_png/vector.rb', line 137 def max_y y_range.last end |
#min_x ⇒ Integer
Finds the lowest x-coordinate in this vector.
119 120 121 |
# File 'lib/chunky_png/vector.rb', line 119 def min_x x_range.first end |
#min_y ⇒ Integer
Finds the lowest y-coordinate in this vector.
131 132 133 |
# File 'lib/chunky_png/vector.rb', line 131 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
145 146 147 |
# File 'lib/chunky_png/vector.rb', line 145 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.
151 152 153 |
# File 'lib/chunky_png/vector.rb', line 151 def width 1 + (max_x - min_x) end |
#x_range ⇒ Range
Returns the range in x-coordinates for all the points in this vector.
107 108 109 |
# File 'lib/chunky_png/vector.rb', line 107 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.
113 114 115 |
# File 'lib/chunky_png/vector.rb', line 113 def y_range Range.new(*points.map { |p| p.y }.minmax) end |