module ChunkyPNG::Canvas::Drawing

def bezier_curve(points, stroke_color = ChunkyPNG::Color::BLACK)

Returns:
  • (Chunky:PNG::Canvas) - Itself, with the curve drawn

Parameters:
  • A (Array, Point) -- collection of control points
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)
  end
  curve_points = Array.new
  t     = 0
  n     = points.length - 1
  bicof = 0
  while t <= 100
    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
    for i in 1...points.length - 1
      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
      i += 1
    end
    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)
  end
  self
end