Class: ChunkyPNG::Vector

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/chunky_png/vector.rb

Overview

Class that represents a vector of points, i.e. a list of Point instances.

Vectors can be created quite flexibly. See the Vector factory methods for more information on how to construct vectors.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

See Also:



46
47
48
# File 'lib/chunky_png/vector.rb', line 46

def initialize(points = [])
  @points = points
end

Instance Attribute Details

#pointsArray<ChunkyPNG::Point> (readonly)



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>



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>



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

#dimensionChunkyPNG::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

Yields:



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

Yields:

Raises:

See Also:



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.

Raises:

See Also:



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

#heightInteger

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

#lengthInteger

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_xInteger

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_yInteger

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_xInteger

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_yInteger

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

#offsetChunkyPNG::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

#widthInteger

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_rangeRange

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_rangeRange

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