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.

Parameters:

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)

Returns The array that holds all the points in this vector.

Returns:

  • (Array<ChunkyPNG::Point>)

    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.

Returns:

  • (Array<ChunkyPNG::Point>)

    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.

Returns:



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.

Parameters:

  • index (Integer)

    The 0-based index of the point in this vector.

  • The (ChunkyPNG::Point)

    point instance.



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.

Returns:



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

Parameters:

  • close (true, false) (defaults to: true)

    Whether to close the path, i.e. return an edge that connects the last point in the vector back to the first point.

Yields:

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.

Parameters:

  • close (true, false) (defaults to: true)

    Whether to close the path, i.e. return an edge that connects the last point in the vector back to the first point.

Returns:

  • (Enumerator)

    The enumerator that iterates over the edges.

Raises:

See Also:



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.

Parameters:

Returns:

  • (true, false)

    true if the list of points are identical



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.

Returns:

  • (Integer)

    The y-distance between the points that are farthest from each other.



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.

Returns:

  • (Integer)

    The length of the points array.



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.

Returns:

  • (Integer)

    The highest x-coordinate of all the points in the 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.

Returns:

  • (Integer)

    The highest y-coordinate of all the points in the 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.

Returns:

  • (Integer)

    The lowest x-coordinate of all the points in the 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.

Returns:

  • (Integer)

    The lowest y-coordinate of all the points in the 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

Returns:

  • (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.



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.

Returns:

  • (Integer)

    The x-distance between the points that are farthest from each other.



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.

Returns:

  • (Range)

    The (inclusive) range of x-coordinates.



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.

Returns:

  • (Range)

    The (inclusive) range of y-coordinates.



113
114
115
# File 'lib/chunky_png/vector.rb', line 113

def y_range
  Range.new(*points.map { |p| p.y }.minmax)
end