lib/chunky_png/canvas/drawing.rb in chunky_png-1.3.11 vs lib/chunky_png/canvas/drawing.rb in chunky_png-1.3.12

- old
+ new

@@ -1,18 +1,16 @@ module ChunkyPNG class Canvas - # Module that adds some primitive drawing methods to {ChunkyPNG::Canvas}. # # All of these methods change the current canvas instance and do not create # a new one, even though the method names do not end with a bang. # # @note Drawing operations will not fail when something is drawn outside of # the bounds of the canvas; these pixels will simply be ignored. # @see ChunkyPNG::Canvas module Drawing - # Composes a pixel on the canvas by alpha blending a color with its # background color. # # @param [Integer] x The x-coordinate of the pixel to blend. # @param [Integer] y The y-coordinate of the pixel to blend. @@ -31,48 +29,48 @@ def compose_pixel_unsafe(x, y, color) set_pixel(x, y, ChunkyPNG::Color.compose(color, get_pixel(x, y))) end # Draws a Bezier curve - # @param [Array, Point] A collection of control points + # @param [Array, Point] points A collection of control points + # @param [Integer] stroke_color # @return [Chunky:PNG::Canvas] Itself, with the curve drawn def bezier_curve(points, stroke_color = ChunkyPNG::Color::BLACK) points = ChunkyPNG::Vector(*points) case points.length - when 0, 1; return self - when 2; return line(points[0].x, points[0].y, points[1].x, points[1].y, stroke_color) + when 0, 1 then return self + when 2 then return line(points[0].x, points[0].y, points[1].x, points[1].y, stroke_color) end - curve_points = Array.new + curve_points = [] t = 0 n = points.length - 1 - bicof = 0 while t <= 100 - cur_p = ChunkyPNG::Point.new(0,0) + bicof = 0 + cur_p = ChunkyPNG::Point.new(0, 0) # Generate a float of t. t_f = t / 100.00 - cur_p.x += ((1 - t_f) ** n) * points[0].x - cur_p.y += ((1 - t_f) ** n) * points[0].y + cur_p.x += ((1 - t_f)**n) * points[0].x + cur_p.y += ((1 - t_f)**n) * points[0].y for i in 1...points.length - 1 - bicof = binomial_coefficient(n , i) + bicof = binomial_coefficient(n, i) - cur_p.x += (bicof * (1 - t_f) ** (n - i)) * (t_f ** i) * points[i].x - cur_p.y += (bicof * (1 - t_f) ** (n - i)) * (t_f ** i) * points[i].y + cur_p.x += (bicof * (1 - t_f)**(n - i)) * (t_f**i) * points[i].x + cur_p.y += (bicof * (1 - t_f)**(n - i)) * (t_f**i) * points[i].y i += 1 end - cur_p.x += (t_f ** n) * points[n].x - cur_p.y += (t_f ** n) * points[n].y + cur_p.x += (t_f**n) * points[n].x + cur_p.y += (t_f**n) * points[n].y curve_points << cur_p - bicof = 0 t += 1 end curve_points.each_cons(2) do |p1, p2| line_xiaolin_wu(p1.x.round, p1.y.round, p2.x.round, p2.y.round, stroke_color) @@ -125,13 +123,11 @@ e_acc_temp, e_acc = e_acc, (e_acc + e) & 0xffff x0 += sx if e_acc <= e_acc_temp w = 0xff - (e_acc >> 8) compose_pixel(x0, y0, ChunkyPNG::Color.fade(stroke_color, w)) if inclusive || i > 0 - compose_pixel(x0 + sx, - y0 + sy, - ChunkyPNG::Color.fade(stroke_color, 0xff - w)) + compose_pixel(x0 + sx, y0 + sy, ChunkyPNG::Color.fade(stroke_color, 0xff - w)) end y0 += sy end compose_pixel(x1, y1, stroke_color) if inclusive @@ -143,39 +139,34 @@ e_acc_temp, e_acc = e_acc, (e_acc + e) & 0xffff y0 += sy if e_acc <= e_acc_temp w = 0xff - (e_acc >> 8) compose_pixel(x0, y0, ChunkyPNG::Color.fade(stroke_color, w)) if inclusive || i > 0 - compose_pixel(x0 + sx, - y0 + sy, - ChunkyPNG::Color.fade(stroke_color, 0xff - w)) + compose_pixel(x0 + sx, y0 + sy, ChunkyPNG::Color.fade(stroke_color, 0xff - w)) end x0 += sx end compose_pixel(x1, y1, stroke_color) if inclusive end self end - alias_method :line, :line_xiaolin_wu + alias line line_xiaolin_wu # Draws a polygon on the canvas using the stroke_color, filled using the # fill_color if any. # - # @param [Array, String] The control point vector. Accepts everything + # @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. - def polygon(path, - stroke_color = ChunkyPNG::Color::BLACK, - fill_color = ChunkyPNG::Color::TRANSPARENT) - + 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' + raise ArgumentError, "A polygon requires at least 3 points" end stroke_color = ChunkyPNG::Color.parse(stroke_color) fill_color = ChunkyPNG::Color.parse(fill_color) @@ -213,14 +204,11 @@ # @param [Integer] x1 The x-coordinate of the second control point. # @param [Integer] y1 The y-coordinate of the second control point. # @param [Integer] stroke_color The line color to use for this rectangle. # @param [Integer] fill_color The fill color to use for this rectangle. # @return [ChunkyPNG::Canvas] Itself, with the rectangle drawn. - def rect(x0, y0, x1, y1, - stroke_color = ChunkyPNG::Color::BLACK, - fill_color = ChunkyPNG::Color::TRANSPARENT) - + def rect(x0, y0, x1, y1, stroke_color = ChunkyPNG::Color::BLACK, fill_color = ChunkyPNG::Color::TRANSPARENT) stroke_color = ChunkyPNG::Color.parse(stroke_color) fill_color = ChunkyPNG::Color.parse(fill_color) # Fill unless fill_color == ChunkyPNG::Color::TRANSPARENT @@ -246,20 +234,17 @@ # @param [Integer] y0 The y-coordinate of the center of the circle. # @param [Integer] radius The radius of the circle from the center point. # @param [Integer] stroke_color The color to use for the line. # @param [Integer] fill_color The color to use that fills the circle. # @return [ChunkyPNG::Canvas] Itself, with the circle drawn. - def circle(x0, y0, radius, - stroke_color = ChunkyPNG::Color::BLACK, - fill_color = ChunkyPNG::Color::TRANSPARENT) - + def circle(x0, y0, radius, stroke_color = ChunkyPNG::Color::BLACK, fill_color = ChunkyPNG::Color::TRANSPARENT) stroke_color = ChunkyPNG::Color.parse(stroke_color) fill_color = ChunkyPNG::Color.parse(fill_color) f = 1 - radius - ddF_x = 1 - ddF_y = -2 * radius + dd_f_x = 1 + dd_f_y = -2 * radius x = 0 y = radius compose_pixel(x0, y0 + radius, stroke_color) compose_pixel(x0, y0 - radius, stroke_color) @@ -270,16 +255,16 @@ while x < y if f >= 0 y -= 1 - ddF_y += 2 - f += ddF_y + dd_f_y += 2 + f += dd_f_y end x += 1 - ddF_x += 2 - f += ddF_x + dd_f_x += 2 + f += dd_f_x unless fill_color == ChunkyPNG::Color::TRANSPARENT lines[y] = lines[y] ? [lines[y], x - 1].min : x - 1 lines[x] = lines[x] ? [lines[x], y - 1].min : y - 1 end