Created
December 24, 2012 12:42
-
-
Save dannvix/4369126 to your computer and use it in GitHub Desktop.
simply draw Bezier curve and display, with RMagick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'RMagick' | |
include Magick | |
def tpow (t, pow_a, pow_b) | |
(t ** pow_a) * ((1 - t) ** pow_b) | |
end | |
# Bezier curve parameters | |
p1, p2, p3, p4 = 8, 16, 220, 236 | |
image = Image.new 500, 500 | |
draw = Draw.new | |
draw.stroke_antialias true | |
# title | |
draw.annotate(image, 75, 50, 75, 75, "Bezier Curve") | |
# axis | |
draw.line 100, 100, 100, 400 | |
draw.line 100, 400, 400, 400 | |
# linear interpolation | |
draw.line 100, 400, 355, 145 | |
# Bezier interpolation | |
draw.fill '#FF0000' | |
last = 145 | |
(1..255).each do |p| | |
t = p / 255.0 | |
bezier = ((5 * p1 * tpow(t, 1, 4)) + (10 * p2 * tpow(t, 2, 3)) + (10 * p3 * tpow(t, 3, 2)) + (5 * p4 * tpow(t, 4, 1)) + (255 * tpow(t, 5, 0))) | |
draw.line (355 - p + 1), last, (355 - p), (bezier + 145) | |
last = bezier + 145 | |
end | |
draw.draw image | |
image.display |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment