|
| 1 | +# A Bezier playground. Click to shape the curve. Drag to move it. |
| 2 | +# Arrows toggle between curves, delete removes them. |
| 3 | +# You can print out the parametric equations for t = 0..1 |
| 4 | +module Olap |
| 5 | + def self.overlaps(x, y, point_x, point_y) |
| 6 | + Math.hypot(x - point_x, y - point_y) < RADIUS |
| 7 | + end |
| 8 | +end |
| 9 | + |
| 10 | +class Curve |
| 11 | + include Olap, Processing::Proxy |
| 12 | + attr_accessor :x1, :y1, :c1x, :c1y, :c2x, :c2y, :x2, :y2 |
| 13 | + |
| 14 | + def initialize |
| 15 | + @x1, @y1, @x2, @y2 = X1, Y1, X2, Y2 |
| 16 | + set_control_points(X1 + 30, Y1, X2 - 30, Y2) |
| 17 | + end |
| 18 | + |
| 19 | + def contains(x, y) |
| 20 | + return :one if Olap.overlaps(x1, y1, x, y) |
| 21 | + return :two if Olap.overlaps(x2, y2, x, y) |
| 22 | + end |
| 23 | + |
| 24 | + def all_points |
| 25 | + return x1, y1, c1x, c1y, c2x, c2y, x2, y2 |
| 26 | + end |
| 27 | + |
| 28 | + def control_points |
| 29 | + return c1x, c1y, c2x, c2y |
| 30 | + end |
| 31 | + |
| 32 | + def set_control_points(*points) |
| 33 | + @c1x, @c1y, @c2x, @c2y = *points |
| 34 | + end |
| 35 | + |
| 36 | + def draw |
| 37 | + bezier(*all_points) |
| 38 | + oval x1, y1, 3, 3 |
| 39 | + oval x2, y2, 3, 3 |
| 40 | + end |
| 41 | + |
| 42 | + def print_equation(id) |
| 43 | + pt = all_points.map(&:to_i) |
| 44 | + puts '' |
| 45 | + puts format('*** line #%s ***', id) |
| 46 | + xformat = 'x = (1-t)^3 %s + 3(1-t)^2 t%s + 3(1-t)t^2 %s + t^3 %s' |
| 47 | + yformat = 'y = -1 * ((1-t)^3 %s + 3(1-t)^2 t%s + 3(1-t)t^2 %s + t^3 %s' |
| 48 | + puts format(xformat, pt[0], pt[2], pt[4], pt[6]) |
| 49 | + puts format(yformat, pt[1], pt[3], pt[5], pt[7]) |
| 50 | + puts '' |
| 51 | + end |
| 52 | +end |
| 53 | + |
| 54 | +attr_accessor :curves, :c1x, :c1y, :c2x, :c2y |
| 55 | +attr_reader :panel, :hide |
| 56 | + |
| 57 | +X1, Y1, X2, Y2 = 50.0, 50.0, 250.0, 250.0 |
| 58 | +REDDISH = [250, 100, 100] |
| 59 | +RADIUS = 7 |
| 60 | + |
| 61 | +load_library :control_panel |
| 62 | + |
| 63 | +include Olap |
| 64 | + |
| 65 | +def setup |
| 66 | + sketch_title 'Bezier playground' |
| 67 | + @curves = [] |
| 68 | + @hide = false |
| 69 | + control_panel do |c| |
| 70 | + c.look_feel 'Nimbus' |
| 71 | + c.button :new_curve |
| 72 | + c.button :print_equations |
| 73 | + @panel = c |
| 74 | + end |
| 75 | + generate_curve |
| 76 | +end |
| 77 | + |
| 78 | +def draw |
| 79 | + unless hide |
| 80 | + panel.set_visible true |
| 81 | + @hide = true |
| 82 | + end |
| 83 | + background 50 |
| 84 | + draw_control_tangent_lines |
| 85 | + draw_curves |
| 86 | + draw_current_control_points |
| 87 | +end |
| 88 | + |
| 89 | +def print_equations |
| 90 | + curves.each_with_index { |c, i| c.print_equation(i + 1) } |
| 91 | +end |
| 92 | + |
| 93 | +def control_points |
| 94 | + return c1x, c1y, c2x, c2y |
| 95 | +end |
| 96 | + |
| 97 | +def set_control_points(*points) |
| 98 | + @c1x, @c1y, @c2x, @c2y = points.any? ? points : [X1, Y1, X2, Y2] |
| 99 | +end |
| 100 | + |
| 101 | +def generate_curve |
| 102 | + curves << current_curve = Curve.new |
| 103 | + @current = curves.length - 1 |
| 104 | + set_control_points(*current_curve.control_points) |
| 105 | +end |
| 106 | + |
| 107 | +def current_curve |
| 108 | + curves[@current] |
| 109 | +end |
| 110 | + |
| 111 | +def new_curve |
| 112 | + current_curve.set_control_points(c1x, c1y, c2x, c2y) |
| 113 | + generate_curve |
| 114 | +end |
| 115 | + |
| 116 | +def clicked_control_point? |
| 117 | + x, y = mouse_x, mouse_y |
| 118 | + return :one if Olap.overlaps(c1x, c1y, x, y) |
| 119 | + return :two if Olap.overlaps(c2x, c2y, x, y) |
| 120 | +end |
| 121 | + |
| 122 | +def mouse_pressed |
| 123 | + switch_curve_if_endpoint_clicked |
| 124 | + @control = clicked_control_point? |
| 125 | + return if @control |
| 126 | + curve = curves.detect { |c| c.contains(mouse_x, mouse_y) } |
| 127 | + @end_point = curve.contains(mouse_x, mouse_y) if curve |
| 128 | +end |
| 129 | + |
| 130 | +def mouse_released |
| 131 | + @control, @end_point = nil, nil |
| 132 | + @hide = false |
| 133 | +end |
| 134 | + |
| 135 | +def mouse_dragged |
| 136 | + offs = compute_offsets |
| 137 | + return if offs.map(&:abs).max > 100 |
| 138 | + return move_control_point(*offs) if @control |
| 139 | + return move_end_point(*offs) && move_control_point(*offs) if @end_point |
| 140 | + move_current_curve(*offs) |
| 141 | +end |
| 142 | + |
| 143 | +def switch_curve_if_endpoint_clicked |
| 144 | + become = curves.detect { |c| c.contains(mouse_x, mouse_y) } |
| 145 | + return unless become && become != current_curve |
| 146 | + current_curve.set_control_points(*control_points) |
| 147 | + self.set_control_points(*become.control_points) |
| 148 | + @current = curves.index(become) |
| 149 | +end |
| 150 | + |
| 151 | +def move_current_curve(x_off, y_off) |
| 152 | + @c1x += x_off; @c2x += x_off |
| 153 | + @c1y += y_off; @c2y += y_off |
| 154 | + current_curve.set_control_points(*control_points) |
| 155 | + current_curve.x1 += x_off; current_curve.x2 += x_off |
| 156 | + current_curve.y1 += y_off; current_curve.y2 += y_off |
| 157 | +end |
| 158 | + |
| 159 | +def move_control_point(x_off, y_off) |
| 160 | + case @control || @end_point |
| 161 | + when :one then @c1x += x_off and @c1y += y_off |
| 162 | + when :two then @c2x += x_off and @c2y += y_off |
| 163 | + end |
| 164 | + current_curve.set_control_points(*control_points) |
| 165 | +end |
| 166 | + |
| 167 | +def move_end_point(x_off, y_off) |
| 168 | + c = current_curve |
| 169 | + case @end_point |
| 170 | + when :one then c.x1 += x_off and c.y1 += y_off |
| 171 | + when :two then c.x2 += x_off and c.y2 += y_off |
| 172 | + end |
| 173 | +end |
| 174 | + |
| 175 | +def compute_offsets |
| 176 | + return mouse_x - pmouse_x, mouse_y - pmouse_y |
| 177 | +end |
| 178 | + |
| 179 | +def draw_curves |
| 180 | + stroke 255 |
| 181 | + no_fill |
| 182 | + stroke_width 2 |
| 183 | + curves.each(&:draw) |
| 184 | +end |
| 185 | + |
| 186 | +def draw_current_control_points |
| 187 | + fill color(*REDDISH) |
| 188 | + no_stroke |
| 189 | + oval c1x, c1y, 5, 5 |
| 190 | + oval c2x, c2y, 5, 5 |
| 191 | +end |
| 192 | + |
| 193 | +def draw_control_tangent_lines |
| 194 | + c = current_curve |
| 195 | + stroke color(*REDDISH) |
| 196 | + stroke_width 1 |
| 197 | + line c1x, c1y, c.x1, c.y1 |
| 198 | + line c2x, c2y, c.x2, c.y2 |
| 199 | +end |
| 200 | + |
| 201 | +def settings |
| 202 | + size 300, 300 |
| 203 | +end |
0 commit comments