Sha256: 78d8f5c612a3e199c5a77ab0208ab8722e89d12cac135b01a86a0de194761044

Contents?: true

Size: 1.97 KB

Versions: 6

Compression:

Stored size: 1.97 KB

Contents

# sketch by Jan Vantomme
# Part of a series of articles on Processing 2.
# Blog post here:
# http://vormplus.be/blog/article/drawing-shapes-with-quadratic-vertices
# translated for ruby-processing by Martin Prout
# Note how to specify fill and background using hexadecimal string for color,
# this is different from vanilla processing
#

load_library :control_panel

attr_reader :debug, :save_one, :step_angle, :cr, :detail, :panel, :hide

def setup
  size 450, 320
  @hide = false
  control_panel do |c|
    c.title = "controller"
    c.menu(:detail, %w(4 5 6 7 8 9 10), '7')
    c.button :toggle_debug
    c.button :save_image
    @panel = c
  end

  @debug = false
  @save_one = false
  smooth 8
end

def draw
  if (!hide)
    panel.set_visible true
    @hide = true
  end
  background color('#BDF018')
  translate width / 2, height / 2
  @step_angle = TAU / (detail.to_i - 1)
  fill color('#ffffff')
  no_stroke
  @cr = map(mouse_x, 0, width, 20, 200)
  begin_shape
  detail.to_i.times do |i|
    if (i == 0)
      vertex cos_x(i), sin_y(i)
    else
      quadratic_vertex cos_cx(i), sin_cy(i), cos_x(i), sin_y(i)
    end
  end
  end_shape(CLOSE)

  if (debug)
    # draw lines between points
    stroke_weight(1)
    no_fill
    stroke(0)

    begin_shape
    detail.to_i.times do |i|
      vertex cos_cx(i), sin_cy(i) unless i == 0
      vertex cos_x(i), sin_y(i)
    end
    end_shape CLOSE

    # draw points
    stroke_weight 8
    detail.to_i.times do |i|
      stroke 0
      point cos_x(i), sin_y(i)
      stroke 255, 0, 0
      point cos_cx(i), sin_cy(i)
    end
  end

  if save_one
    save_frame("images/quadraticvertex-#####.png")
    @save_one = false
  end
end

def mouse_pressed
  @hide = false
end

def cos_x(n)
  cos(step_angle * n) * 100
end

def sin_y(n)
  sin(step_angle * n) * 100
end

def cos_cx(n)
  cos(step_angle * n - (step_angle / 2 )) * cr
end

def sin_cy(n)
  sin(step_angle * n - (step_angle / 2 )) * cr
end

def toggle_debug
  @debug = !debug
end

def save_image
  @save_one = true
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ruby-processing-2.6.3 samples/contributed/quadraticvertex.rb
ruby-processing-2.6.2 samples/contributed/quadraticvertex.rb
ruby-processing-2.6.1 samples/contributed/quadraticvertex.rb
ruby-processing-2.6.0 samples/contributed/quadraticvertex.rb
ruby-processing-2.5.1 samples/contributed/quadraticvertex.rb
ruby-processing-2.5.0 samples/contributed/quadraticvertex.rb