# ruby-processing sketch # http://qiita.com/norioc/items/99514cb659aad03ab7d7 # http://aidiary.hatenablog.com/entry/20050402/1251514618 N = 15 attr_reader :rows, :cols def setup sketch_title 'Bresenham`s Algorithm' @rows = height / N @cols = width / N end def settings size 400, 400 end def build_line(from:, to:) next_x = from.x next_y = from.y delta_x = to.x - from.x delta_y = to.y - from.y step_x = delta_x < 0 ? -1 : 1 step_y = delta_y < 0 ? -1 : 1 delta_x = (delta_x * 2).abs delta_y = (delta_y * 2).abs b_line = Array.new(1, Vec2D.new(next_x, next_y)) if delta_x > delta_y fraction = delta_y - delta_x / 2 while next_x != to.x if fraction >= 0 next_y += step_y fraction -= delta_x end next_x += step_x fraction += delta_y b_line << Vec2D.new(next_x, next_y) end else fraction = delta_x - delta_y / 2 while next_y != to.y if fraction >= 0 next_x += step_x fraction -= delta_y end next_y += step_y fraction += delta_x b_line << Vec2D.new(next_x, next_y) end end b_line end def draw background 255 translate width / 2, height / 2 rect_mode CENTER no_fill stroke 60 (0..rows).each do |i| (0..cols).each do |j| rect((j - cols / 2) * N, (i - rows / 2) * N, N, N) end end x = mouse_x - width / 2 + N / 2 y = mouse_y - height / 2 + N / 2 b_line = build_line(from: Vec2D.new, to: Vec2D.new(x / N, y / N)) unless b_line.empty? fill color('#C7B097') b_line.each { |v| rect(v.x * N, v.y * N, N, N) } end stroke color('#0000FF') stroke_width 2 line 0, 0, mouse_x - width / 2, mouse_y - height / 2 stroke 0 stroke_width 1 end
Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0
Tuesday, 15 September 2015
Bresenham's Algorithm in JRubyArt
After ruby-processing version, published by norioc on qiita
Labels:
Bresenhams algorithm,
jruby,
JRubyArt
Subscribe to:
Post Comments (Atom)
Followers
About Me
- monkstone
- I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2
No comments:
Post a Comment