Last active
March 27, 2017 02:15
-
-
Save JoshCheek/bf1a1f16f5e24bcd88c19b5defa34fc2 to your computer and use it in GitHub Desktop.
Bezier With Sindy (Ruby version)
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
# The JavaScript version is @ http://codepen.io/josh_cheek/pen/VpBJVQ?editors=0010 | |
require 'graphics' | |
class BezierForSindy < Graphics::Simulation | |
def initialize | |
super 500, 500, 24 | |
@initial_points = [ | |
[50, h-50], | |
[50, 50], | |
[w/2-50, h-50], | |
[w-50, h-50], | |
[w-50, 50], | |
] | |
@bezier_points = [] | |
@iteration = 0 | |
@direction = 1 | |
@ticks = 100 | |
@radius = 4 | |
end | |
def draw(n) | |
clear :white | |
points = @initial_points | |
while points.length > 1 | |
draw_lines_between(points) | |
draw_every_endpoint(points) | |
points = get_next_points(points, @iteration/@ticks.to_f) | |
end | |
@bezier_points[@iteration] = points[0] | |
crnt_bezier_points = @bezier_points[0, @iteration] # we fill in more points than we need | |
draw_lines_between(crnt_bezier_points) | |
draw_first_and_last_endpoint(crnt_bezier_points) | |
if @iteration === @ticks | |
@direction = -1 | |
elsif @iteration == 0 | |
@direction = 1 | |
end | |
@iteration = @iteration + @direction | |
end | |
def draw_lines_between(points) | |
points.each_cons 2 do |(x1, y1), (x2, y2)| | |
line x1, y1, x2, y2, :black | |
end | |
end | |
def draw_every_endpoint(points) | |
points.each { |point| circle point[0], point[1], @radius, :black, true } | |
end | |
def draw_first_and_last_endpoint(points) | |
if points.length > 0 | |
first_point = points.first | |
last_point = points.last | |
circle first_point[0], first_point[1], @radius, :black, true | |
circle last_point[0], last_point[1], @radius, :black, true | |
end | |
end | |
def get_next_points(points, percentage) | |
points.each_cons(2).map do |(x1, y1), (x2, y2)| | |
x = (x2 - x1) * percentage + x1 | |
y = (y2 - y1) * percentage + y1 | |
[x, y] | |
end | |
end | |
end | |
BezierForSindy.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment