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

Methods included from Enumerable

#minmax

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:



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

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

Instance Attribute Details

#pointsArray<ChunkyPNG::Point> (readonly)



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>



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>



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

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

Yields:



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

Yields:

  • (points.last, points.first)

Raises:

See Also:



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.

Raises:

See Also:



79
80
81
# File 'lib/chunky_png/vector.rb', line 79

def edges(close = true)
  to_enum(: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

#heightInteger

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

#lengthInteger

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_xInteger

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_yInteger

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_xInteger

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_yInteger

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

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

#widthInteger

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_rangeRange

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_rangeRange

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