# rmagick/rmagick forked from rmagick-temp/rmagick

### Subversion checkout URL

You can clone with
or
.

Enhance RVG's polyline, polygon methods to accept array arguments

`(feature #1941)`
1 parent 0e0d89c commit 3ed196b3163856afd3431d0281af8ba6816a9a2c rmagick committed
Showing with 77 additions and 20 deletions.
1. +43 −6 doc/rvgshape.html
2. +32 −12 lib/rvg/embellishable.rb
3. +2 −2 lib/rvg/misc.rb
49 doc/rvgshape.html
 @@ -250,7 +250,14 @@

polygon

obj.polygon(x1, y1, x2, y2...) -> aPolygon

+ "arg">y2...) -> aPolygon
+ obj.polygon(array) -> aPolygon
+ obj.polygon(array1, array2) -> + aPolygon

@@ -261,8 +268,33 @@

polygon

Arguments
-

A list of points that make up the polygon. It is an error to - define an odd number of points.

+

The arguments to polygon and polyline can be

+ +
+
1. At least 4 numbers that describe the [x, y] coordinates + of the points of the polygon/polyline.
2. + +
3. One array containing at least 4 numbers.
4. + +
5. Two arrays. The first array is a list of x-coordinates. + The second array is a list of y-coordinates. Both arrays must + have at least one element. If one array is shorter than the + other, the shorter array is extended by duplicating its + elements as necessary. The combined arrays must describe at + least 2 pairs of [x,y] coordinates. For example +
+    x = [1, 3, 5, 7, 9]
+    y = [2,4]
+    canvas.polygon(x, y)
+    # is equivalent to canvas.polygon(1,2, 3,4, 5,2, 7,4, 9,2)
+
+
6. +
+ +

It is an error to specify an odd number of coordinates. + Array arguments can be any objects that can be converted to + arrays by the Kernel#Array method.

Example
@@ -277,7 +309,13 @@

polyline

obj.polyline(x1, y1, x2, y2...) -> apolyline

+ "arg">aPolyline
+ obj.polyline(array) -> aPolyline
+ obj.polyline(array1, array2) -> + aPolyline

@@ -288,8 +326,7 @@

polyline

Arguments
-

A list of points that make up the polyline. It is an error - to define an odd number of points.

+

See polygon

Example
44 lib/rvg/embellishable.rb
 @@ -1,5 +1,5 @@ #-- -# \$Id: embellishable.rb,v 1.3 2005/04/10 15:12:45 rmagick Exp \$ +# \$Id: embellishable.rb,v 1.4 2005/05/22 22:31:30 rmagick Exp \$ # Copyright (C) 2005 Timothy P. Hunter #++ @@ -111,7 +111,34 @@ def round(rx, ry=nil) end # class Rect - class Polygon < Shape + class PolyShape < Shape + + def polypoints(points) + case points.length + when 1 + points = Array(points[0]) + when 2 + x_coords = Array(points[0]) + y_coords = Array(points[1]) + unless x_coords.length > 0 && y_coords.length > 0 + raise ArgumentError, "array arguments must contain at least one point" + end + n = x_coords.length - y_coords.length + short = n > 0 ? y_coords : x_coords + olen = short.length + n.abs.times {|x| short << short[x % olen]} + points = x_coords.zip(y_coords).flatten + end + n = points.length + if n < 4 || n % 2 != 0 + raise ArgumentError, "insufficient/odd number of points specified: #{n}" + end + return RVG.convert_to_float(*points) + end + + end # class PolyShape + + class Polygon < PolyShape # Draws a polygon. The arguments are [x, y] pairs that # define the points that make up the polygon. At least two @@ -121,17 +148,13 @@ class Polygon < Shape # Use the RVG::ShapeConstructors#polygon method to create Polygon objects in a container. def initialize(*points) super() - n = points.length - if n < 4 || n % 2 != 0 - raise ArgumentError, "insufficient/odd number of points specified: #{n}" - end @primitive = :polygon - @args = RVG.convert_to_float(*points) + @args = polypoints(points) end end # class Polygon - class Polyline < Shape + class Polyline < PolyShape # Draws a polyline. The arguments are [x, y] pairs that # define the points that make up the polyline. At least two @@ -139,10 +162,7 @@ class Polyline < Shape # Use the RVG::ShapeConstructors#polyline method to create Polyline objects in a container. def initialize(*points) super() - n = points.length - if n < 4 || n % 2 != 0 - raise ArgumentError, "insufficient/odd number of points specified: #{n}" - end + points = polypoints(points) @primitive = :polyline @args = RVG.convert_to_float(*points) end
4 lib/rvg/misc.rb
 @@ -1,4 +1,4 @@ -# \$Id: misc.rb,v 1.4 2005/04/23 15:28:00 rmagick Exp \$ +# \$Id: misc.rb,v 1.5 2005/05/22 22:31:30 rmagick Exp \$ # Copyright (C) 2005 Timothy P. Hunter class Magick::RVG @@ -51,7 +51,7 @@ def self.convert_to_float(*args) begin fargs = args.collect { |a| (allow_nil && a.nil?) ? a : Float(a) } rescue ArgumentError, TypeError - raise ArgumentError, self.fmsg(args) + raise ArgumentError, self.fmsg(*args) end return fargs end