module ChunkyPNG::Canvas::Drawing
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
Public Instance Methods
Source
# File lib/chunky_png/canvas/drawing.rb 39 def bezier_curve(points, stroke_color = ChunkyPNG::Color::BLACK) 40 points = ChunkyPNG::Vector(*points) 41 case points.length 42 when 0, 1 then return self 43 when 2 then return line(points[0].x, points[0].y, points[1].x, points[1].y, stroke_color) 44 end 45 46 curve_points = [] 47 48 t = 0 49 n = points.length - 1 50 51 while t <= 100 52 bicof = 0 53 cur_p = ChunkyPNG::Point.new(0, 0) 54 55 # Generate a float of t. 56 t_f = t / 100.00 57 58 cur_p.x += ((1 - t_f)**n) * points[0].x 59 cur_p.y += ((1 - t_f)**n) * points[0].y 60 61 for i in 1...points.length - 1 62 bicof = binomial_coefficient(n, i) 63 64 cur_p.x += (bicof * (1 - t_f)**(n - i)) * (t_f**i) * points[i].x 65 cur_p.y += (bicof * (1 - t_f)**(n - i)) * (t_f**i) * points[i].y 66 i += 1 67 end 68 69 cur_p.x += (t_f**n) * points[n].x 70 cur_p.y += (t_f**n) * points[n].y 71 72 curve_points << cur_p 73 74 t += 1 75 end 76 77 curve_points.each_cons(2) do |p1, p2| 78 line_xiaolin_wu(p1.x.round, p1.y.round, p2.x.round, p2.y.round, stroke_color) 79 end 80 81 self 82 end
Draws a Bezier curve @param [Array, Point] points A collection of control points @param [Integer] stroke_color @return [Chunky:PNG::Canvas] Itself, with the curve drawn
Source
# File lib/chunky_png/canvas/drawing.rb 241 def circle(x0, y0, radius, stroke_color = ChunkyPNG::Color::BLACK, fill_color = ChunkyPNG::Color::TRANSPARENT) 242 stroke_color = ChunkyPNG::Color.parse(stroke_color) 243 fill_color = ChunkyPNG::Color.parse(fill_color) 244 245 f = 1 - radius 246 dd_f_x = 1 247 dd_f_y = -2 * radius 248 x = 0 249 y = radius 250 251 compose_pixel(x0, y0 + radius, stroke_color) 252 compose_pixel(x0, y0 - radius, stroke_color) 253 compose_pixel(x0 + radius, y0, stroke_color) 254 compose_pixel(x0 - radius, y0, stroke_color) 255 256 lines = [radius - 1] unless fill_color == ChunkyPNG::Color::TRANSPARENT 257 258 while x < y 259 260 if f >= 0 261 y -= 1 262 dd_f_y += 2 263 f += dd_f_y 264 end 265 266 x += 1 267 dd_f_x += 2 268 f += dd_f_x 269 270 unless fill_color == ChunkyPNG::Color::TRANSPARENT 271 lines[y] = lines[y] ? [lines[y], x - 1].min : x - 1 272 lines[x] = lines[x] ? [lines[x], y - 1].min : y - 1 273 end 274 275 compose_pixel(x0 + x, y0 + y, stroke_color) 276 compose_pixel(x0 - x, y0 + y, stroke_color) 277 compose_pixel(x0 + x, y0 - y, stroke_color) 278 compose_pixel(x0 - x, y0 - y, stroke_color) 279 280 unless x == y 281 compose_pixel(x0 + y, y0 + x, stroke_color) 282 compose_pixel(x0 - y, y0 + x, stroke_color) 283 compose_pixel(x0 + y, y0 - x, stroke_color) 284 compose_pixel(x0 - y, y0 - x, stroke_color) 285 end 286 end 287 288 unless fill_color == ChunkyPNG::Color::TRANSPARENT 289 lines.each_with_index do |length, y_offset| 290 if length > 0 291 line(x0 - length, y0 - y_offset, x0 + length, y0 - y_offset, fill_color) 292 end 293 if length > 0 && y_offset > 0 294 line(x0 - length, y0 + y_offset, x0 + length, y0 + y_offset, fill_color) 295 end 296 end 297 end 298 299 self 300 end
Draws a circle on the canvas.
@param [Integer] x0 The x-coordinate of the center of the circle. @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.
Source
# File lib/chunky_png/canvas/drawing.rb 21 def compose_pixel(x, y, color) 22 return unless include_xy?(x, y) 23 compose_pixel_unsafe(x, y, ChunkyPNG::Color.parse(color)) 24 end
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. @param [Integer] color The foreground color to blend with @return [Integer] The composed color.
Source
# File lib/chunky_png/canvas/drawing.rb 31 def compose_pixel_unsafe(x, y, color) 32 set_pixel(x, y, ChunkyPNG::Color.compose(color, get_pixel(x, y))) 33 end
Composes a pixel on the canvas by alpha blending a color with its background color, without bounds checking.
@param (see compose_pixel
) @return [Integer] The composed color.
Source
# File lib/chunky_png/canvas/drawing.rb 94 def line_xiaolin_wu(x0, y0, x1, y1, stroke_color, inclusive = true) 95 stroke_color = ChunkyPNG::Color.parse(stroke_color) 96 97 dx = x1 - x0 98 sx = dx < 0 ? -1 : 1 99 dx *= sx 100 dy = y1 - y0 101 sy = dy < 0 ? -1 : 1 102 dy *= sy 103 104 if dy == 0 # vertical line 105 x0.step(inclusive ? x1 : x1 - sx, sx) do |x| 106 compose_pixel(x, y0, stroke_color) 107 end 108 109 elsif dx == 0 # horizontal line 110 y0.step(inclusive ? y1 : y1 - sy, sy) do |y| 111 compose_pixel(x0, y, stroke_color) 112 end 113 114 elsif dx == dy # diagonal 115 x0.step(inclusive ? x1 : x1 - sx, sx) do |x| 116 compose_pixel(x, y0, stroke_color) 117 y0 += sy 118 end 119 120 elsif dy > dx # vertical displacement 121 compose_pixel(x0, y0, stroke_color) 122 e_acc = 0 123 e = ((dx << 16) / dy.to_f).round 124 (dy - 1).downto(0) do |i| 125 e_acc_temp, e_acc = e_acc, (e_acc + e) & 0xffff 126 x0 += sx if e_acc <= e_acc_temp 127 w = 0xff - (e_acc >> 8) 128 compose_pixel(x0, y0, ChunkyPNG::Color.fade(stroke_color, w)) 129 if inclusive || i > 0 130 compose_pixel(x0 + sx, y0 + sy, ChunkyPNG::Color.fade(stroke_color, 0xff - w)) 131 end 132 y0 += sy 133 end 134 compose_pixel(x1, y1, stroke_color) if inclusive 135 136 else # horizontal displacement 137 compose_pixel(x0, y0, stroke_color) 138 e_acc = 0 139 e = ((dy << 16) / dx.to_f).round 140 (dx - 1).downto(0) do |i| 141 e_acc_temp, e_acc = e_acc, (e_acc + e) & 0xffff 142 y0 += sy if e_acc <= e_acc_temp 143 w = 0xff - (e_acc >> 8) 144 compose_pixel(x0, y0, ChunkyPNG::Color.fade(stroke_color, w)) 145 if inclusive || i > 0 146 compose_pixel(x0 + sx, y0 + sy, ChunkyPNG::Color.fade(stroke_color, 0xff - w)) 147 end 148 x0 += sx 149 end 150 compose_pixel(x1, y1, stroke_color) if inclusive 151 end 152 153 self 154 end
Draws an anti-aliased line using Xiaolin Wu’s algorithm.
@param [Integer] x0 The x-coordinate of the first control point. @param [Integer] y0 The y-coordinate of the first control point. @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 color to use for this line. @param [true, false] inclusive Whether to draw the last pixel. Set to
false when drawing multiple lines in a path.
@return [ChunkyPNG::Canvas] Itself, with the line drawn.
Source
# File lib/chunky_png/canvas/drawing.rb 166 def polygon(path, stroke_color = ChunkyPNG::Color::BLACK, fill_color = ChunkyPNG::Color::TRANSPARENT) 167 vector = ChunkyPNG::Vector(*path) 168 if path.length < 3 169 raise ArgumentError, "A polygon requires at least 3 points" 170 end 171 172 stroke_color = ChunkyPNG::Color.parse(stroke_color) 173 fill_color = ChunkyPNG::Color.parse(fill_color) 174 175 # Fill 176 unless fill_color == ChunkyPNG::Color::TRANSPARENT 177 vector.y_range.each do |y| 178 intersections = [] 179 vector.edges.each do |p1, p2| 180 if (p1.y < y && p2.y >= y) || (p2.y < y && p1.y >= y) 181 intersections << (p1.x + (y - p1.y).to_f / (p2.y - p1.y) * (p2.x - p1.x)).round 182 end 183 end 184 185 intersections.sort! 186 0.step(intersections.length - 1, 2) do |i| 187 intersections[i].upto(intersections[i + 1]) do |x| 188 compose_pixel(x, y, fill_color) 189 end 190 end 191 end 192 end 193 194 # Stroke 195 vector.each_edge do |(from_x, from_y), (to_x, to_y)| 196 line(from_x, from_y, to_x, to_y, stroke_color, false) 197 end 198 199 self 200 end
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.
Source
# File lib/chunky_png/canvas/drawing.rb 211 def rect(x0, y0, x1, y1, stroke_color = ChunkyPNG::Color::BLACK, fill_color = ChunkyPNG::Color::TRANSPARENT) 212 stroke_color = ChunkyPNG::Color.parse(stroke_color) 213 fill_color = ChunkyPNG::Color.parse(fill_color) 214 215 # Fill 216 unless fill_color == ChunkyPNG::Color::TRANSPARENT 217 [x0, x1].min.upto([x0, x1].max) do |x| 218 [y0, y1].min.upto([y0, y1].max) do |y| 219 compose_pixel(x, y, fill_color) 220 end 221 end 222 end 223 224 # Stroke 225 line(x0, y0, x0, y1, stroke_color, false) 226 line(x0, y1, x1, y1, stroke_color, false) 227 line(x1, y1, x1, y0, stroke_color, false) 228 line(x1, y0, x0, y0, stroke_color, false) 229 230 self 231 end
Draws a rectangle on the canvas, using two control points.
@param [Integer] x0 The x-coordinate of the first control point. @param [Integer] y0 The y-coordinate of the first control point. @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.
Private Instance Methods
Source
# File lib/chunky_png/canvas/drawing.rb 311 def binomial_coefficient(n, k) 312 return 1 if n == k || k == 0 313 return n if k == 1 314 return -1 if n < k 315 316 # calculate factorials 317 fact_n = (2..n).inject(1) { |carry, i| carry * i } 318 fact_k = (2..k).inject(1) { |carry, i| carry * i } 319 fact_n_sub_k = (2..(n - k)).inject(1) { |carry, i| carry * i } 320 321 fact_n / (fact_k * fact_n_sub_k) 322 end
Calculates the binomial coefficient for n over k.
@param [Integer] n first parameter in coeffient (the number on top when
looking at the mathematic formula)
@param [Integer] k k-element, second parameter in coeffient (the number
on the bottom when looking at the mathematic formula)
@return [Integer] The binomial coeffcient of (n,k)