lib/chunky_png/canvas/drawing.rb
# Draws a polygon on the canvas using the stroke_color, filled using the
# fill_color if any.
#
# @param [Array, String] path The control point vector. Accepts everything
#   {ChunkyPNG.Vector} accepts.
# @param [Integer] stroke_color The stroke color to use for this polygon.
# @param [Integer] fill_color The fill color to use for this polygon.
# @return [ChunkyPNG::Canvas] Itself, with the polygon drawn.
Draws a polygon on the canvas using the stroke_color, filled using the
fill_color if any.
Draws a polygon on the canvas using the stroke_color filled using the fill_color if any .
def polygon(path, stroke_color = ChunkyPNG::Color::BLACK, fill_color = ChunkyPNG::Color::TRANSPARENT)
        vector = ChunkyPNG::Vector(*path)
        if path.length < 3
          raise ArgumentError, "A polygon requires at least 3 points"
        end
        stroke_color = ChunkyPNG::Color.parse(stroke_color)
        fill_color   = ChunkyPNG::Color.parse(fill_color)
        unless fill_color == ChunkyPNG::Color::TRANSPARENT
          vector.y_range.each do |y|
            intersections = []
            vector.edges.each do |p1, p2|
              if (p1.y < y && p2.y >= y) || (p2.y < y && p1.y >= y)
                intersections << (p1.x + (y - p1.y).to_f / (p2.y - p1.y) * (p2.x - p1.x)).round
              end
            end
            intersections.sort!
            0.step(intersections.length - 1, 2) do |i|
              intersections[i].upto(intersections[i + 1]) do |x|
                compose_pixel(x, y, fill_color)
              end
            end
          end
        end
        vector.each_edge do |(from_x, from_y), (to_x, to_y)|
          line(from_x, from_y, to_x, to_y, stroke_color, false)
        end
        self
      end
def polygon ( path , stroke_color = ChunkyPNG :: Color :: BLACK , fill_color = ChunkyPNG :: Color :: TRANSPARENT ) vector = ChunkyPNG :: Vector ( * path ) if path . length < 3 raise ArgumentError , "A polygon requires at least 3 points" end stroke_color = ChunkyPNG :: Color . parse ( stroke_color ) fill_color = ChunkyPNG :: Color . parse ( fill_color ) unless fill_color == ChunkyPNG :: Color :: TRANSPARENT vector . y_range . each do | y | intersections = [ ] vector . edges . each do | p1 , p2 | if ( p1 . y < y && p2 . y >= y ) || ( p2 . y < y && p1 . y >= y ) intersections << ( p1 . x + ( y - p1 . y ) . to_f / ( p2 . y - p1 . y ) * ( p2 . x - p1 . x ) ) . round end end intersections . sort! 0 . step ( intersections . length - 1 , 2 ) do | i | intersections [ i ] . upto ( intersections [ i + 1 ] ) do | x | compose_pixel ( x , y , fill_color ) end end end end vector . each_edge do | ( from_x , from_y ) , ( to_x , to_y ) | line ( from_x , from_y , to_x , to_y , stroke_color , false ) end self end
Draws a polygon on the canvas using the stroke_color, filled using the
fill_color if any.
Draws a polygon on the canvas using the stroke_color filled using the fill_color if any .
# Fill # Stroke
Itself, with the polygon drawn.
Itself with the polygon drawn .
ChunkyPNG::Canvas
path
<not_specific>
stroke_color
<not_specific>
fill_color
<not_specific>
<not_specific>
fill_color The fill color to use for this polygon.
fill_color The fill color to use for this polygon .