diff --git a/lib/squib/api/shapes.rb b/lib/squib/api/shapes.rb index b49b398..aa94988 100644 --- a/lib/squib/api/shapes.rb +++ b/lib/squib/api/shapes.rb @@ -52,6 +52,32 @@ module Squib opts[:fill_color], opts[:stroke_color], opts[:stroke_width]) end end + + # Draw a triangle using the given coordinates + # + # @example + # triangle :x1 => 0, :y1 => 0, :x2 => 50, :y2 => 50, :x3 => 0, :y3 => 50 + # + # @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges} + # @option opts x1 [Integer] (0) the x-coordinate to place + # @option opts y1 [Integer] (0) the y-coordinate to place + # @option opts x2 [Integer] (50) the x-coordinate to place + # @option opts y2 [Integer] (50) the y-coordinate to place + # @option opts x3 [Integer] (0) the x-coordinate to place + # @option opts y3 [Integer] (50) the y-coordinate to place + # @option opts fill_color [String] ('#0000') the color with which to fill the rectangle + # @option opts stroke_color [String] (:black) the color with which to stroke the outside of the rectangle + # @option opts stroke_width [Decimal] (2.0) the width of the outside stroke + # @return [nil] intended to be void + # @api public + def triangle(opts = {}) + opts = needs(opts, [:range, :x1, :y1, :x2, :y2, :x3, :y3, :layout, + :fill_color, :stroke_color, :stroke_width]) + opts[:range].each do |i| + @cards[i].triangle(opts[:x1], opts[:y1], opts[:x2], opts[:y2],opts[:x3], opts[:y3], + opts[:fill_color], opts[:stroke_color], opts[:stroke_width]) + end + end end end \ No newline at end of file diff --git a/lib/squib/constants.rb b/lib/squib/constants.rb index 1d13d74..d22bc33 100644 --- a/lib/squib/constants.rb +++ b/lib/squib/constants.rb @@ -13,6 +13,12 @@ module Squib :sheet => 0, :x => 0, :y => 0, + :x1 => 100, + :y1 => 100, + :x2 => 150, + :y2 => 150, + :x3 => 100, + :y3 => 150, :x_radius => 0, :y_radius => 0, :align => :left, diff --git a/lib/squib/graphics/shapes.rb b/lib/squib/graphics/shapes.rb index 73bb7d6..5054f5f 100644 --- a/lib/squib/graphics/shapes.rb +++ b/lib/squib/graphics/shapes.rb @@ -27,6 +27,19 @@ module Squib cc.set_source_color(fill_color) cc.fill end + + # :nodoc: + # @api private + def triangle(x1, y1, x2, y2, x3, y3, fill_color, stroke_color, stroke_width) + cc = cairo_context + cc.triangle(x1, y1, x2, y2, x3, y3) + cc.set_source_color(stroke_color) + cc.set_line_width(stroke_width) + cc.stroke + cc.triangle(x1, y1, x2, y2, x3, y3) + cc.set_source_color(fill_color) + cc.fill + end end end \ No newline at end of file diff --git a/samples/draw_shapes.rb b/samples/draw_shapes.rb new file mode 100644 index 0000000..d19510e --- /dev/null +++ b/samples/draw_shapes.rb @@ -0,0 +1,15 @@ +require 'squib' + +Squib::Deck.new do + rect x: 300, y: 300, width: 400, height: 400, + fill_color: :blue, stroke_color: :red, stroke_width: 50.0 + + circle x: 600, y: 600, radius: 75, + fill_color: :gray, stroke_color: :green, stroke_width: 8.0 + + triangle x1: 50, y1: 50, + x2: 150, y2: 150, + x3: 75, y3: 250 + + save_png prefix: 'shape_' +end \ No newline at end of file