Skip to content

Commit b0bbe09

Browse files
author
monkstone
committed
yet more tidy up
1 parent 7cfd7a3 commit b0bbe09

File tree

4 files changed

+73
-91
lines changed

4 files changed

+73
-91
lines changed

samples/contributed/bezier_playground.rb

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,47 @@ def self.overlaps(x, y, point_x, point_y)
1010
class Curve
1111
include Olap, Processing::Proxy
1212
attr_accessor :x1, :y1, :c1x, :c1y, :c2x, :c2y, :x2, :y2
13-
13+
1414
def initialize
1515
@x1, @y1, @x2, @y2 = X1, Y1, X2, Y2
1616
set_control_points(X1 + 30, Y1, X2 - 30, Y2)
1717
end
18-
18+
1919
def contains(x, y)
2020
return :one if Olap.overlaps(x1, y1, x, y)
2121
return :two if Olap.overlaps(x2, y2, x, y)
2222
end
23-
23+
2424
def all_points
2525
return x1, y1, c1x, c1y, c2x, c2y, x2, y2
2626
end
27-
27+
2828
def control_points
2929
return c1x, c1y, c2x, c2y
3030
end
31-
31+
3232
def set_control_points(*points)
3333
@c1x, @c1y, @c2x, @c2y = *points
3434
end
35-
35+
3636
def draw
3737
bezier(*all_points)
3838
oval x1, y1, 3, 3
3939
oval x2, y2, 3, 3
4040
end
41-
41+
4242
def print_equation(id)
43-
p = all_points.map { |pt| pt.to_i }
44-
puts ""
45-
puts "*** line ##{id} ***"
46-
puts "x = (1-t)^3 #{p[0]} + 3(1-t)^2 t#{p[2]} + 3(1-t)t^2 #{p[4]} + t^3 #{p[6]}"
47-
puts "y = -1 * ((1-t)^3 #{p[1]} + 3(1-t)^2 t#{p[3]} + 3(1-t)t^2 #{p[5]} + t^3 #{p[7]})"
48-
puts ""
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 ''
4951
end
5052
end
5153

52-
5354
attr_accessor :curves, :c1x, :c1y, :c2x, :c2y
5455
attr_reader :panel, :hide
5556

@@ -69,10 +70,21 @@ def setup
6970
c.button :new_curve
7071
c.button :print_equations
7172
@panel = c
72-
end
73+
end
7374
generate_curve
7475
end
7576

77+
def draw
78+
unless hide
79+
panel.visible = visible
80+
@hide = true
81+
end
82+
background 50
83+
draw_control_tangent_lines
84+
draw_curves
85+
draw_current_control_points
86+
end
87+
7688
def print_equations
7789
curves.each_with_index { |c, i| c.print_equation(i + 1) }
7890
end
@@ -114,15 +126,14 @@ def mouse_pressed
114126
@end_point = curve.contains(mouse_x, mouse_y) if curve
115127
end
116128

117-
118129
def mouse_released
119130
@control, @end_point = nil, nil
120131
@hide = false
121132
end
122133

123134
def mouse_dragged
124135
offs = compute_offsets
125-
return if offs.map { |o| o.abs }.max > 100
136+
return if offs.map(&:abs).max > 100
126137
return move_control_point(*offs) if @control
127138
return move_end_point(*offs) && move_control_point(*offs) if @end_point
128139
move_current_curve(*offs)
@@ -136,7 +147,6 @@ def switch_curve_if_endpoint_clicked
136147
@current = curves.index(become)
137148
end
138149

139-
140150
def move_current_curve(x_off, y_off)
141151
@c1x += x_off; @c2x += x_off
142152
@c1y += y_off; @c2y += y_off
@@ -145,7 +155,6 @@ def move_current_curve(x_off, y_off)
145155
current_curve.y1 += y_off; current_curve.y2 += y_off
146156
end
147157

148-
149158
def move_control_point(x_off, y_off)
150159
case @control || @end_point
151160
when :one then @c1x += x_off and @c1y += y_off
@@ -154,7 +163,6 @@ def move_control_point(x_off, y_off)
154163
current_curve.set_control_points(*control_points)
155164
end
156165

157-
158166
def move_end_point(x_off, y_off)
159167
c = current_curve
160168
case @end_point
@@ -163,16 +171,15 @@ def move_end_point(x_off, y_off)
163171
end
164172
end
165173

166-
167174
def compute_offsets
168175
return mouse_x - pmouse_x, mouse_y - pmouse_y
169176
end
170177

171-
def draw_curves
178+
def draw_curves
172179
stroke 255
173180
no_fill
174181
stroke_width 2
175-
curves.each { |curve| curve.draw }
182+
curves.each(&:draw)
176183
end
177184

178185
def draw_current_control_points
@@ -189,16 +196,3 @@ def draw_control_tangent_lines
189196
line c1x, c1y, c.x1, c.y1
190197
line c2x, c2y, c.x2, c.y2
191198
end
192-
193-
def draw
194-
if (!hide)
195-
panel.visible = self.visible
196-
@hide = true
197-
end
198-
background 50
199-
draw_control_tangent_lines
200-
draw_curves
201-
draw_current_control_points
202-
end
203-
204-

samples/contributed/drawolver.rb

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Drawolver: draw 2D & revolve 3D
22

33
# Example to show how to use the VecMath library.
4-
# ruby replacement for PVector. Also features
5-
# the use each_cons, possibly a rare use for this
4+
# ruby replacement for PVector. Also features
5+
# the use each_cons, possibly a rare use for this
66
# ruby Enumerable method?
77
# 2010-03-22 - fjenett (last revised by monkstone 2014-03-21)
88
# now uses 'zip' and 'each', in place of a custom Array object
@@ -12,43 +12,41 @@
1212
import 'vecmath'
1313
attr_reader :drawing_mode, :points, :rot_x, :rot_y, :vertices
1414

15-
def setup
15+
def setup
1616
size 1024, 768, P3D
17-
frame_rate 30
17+
frame_rate 30
1818
reset_scene
1919
end
2020

21-
def draw
22-
background 0
23-
if (!drawing_mode)
24-
translate(width/2, height/2)
21+
def draw
22+
background 0
23+
unless drawing_mode
24+
translate(width / 2, height / 2)
2525
rotate_x rot_x
2626
rotate_y rot_y
2727
@rot_x += 0.01
2828
@rot_y += 0.02
29-
translate(-width/2, -height/2)
30-
end
29+
translate(-width / 2, -height / 2)
30+
end
3131
no_fill
3232
stroke 255
33-
points.each_cons(2) { |ps, pe| line ps.x, ps.y, pe.x, pe.y}
34-
35-
if (!drawing_mode)
36-
stroke 125
37-
fill 120
38-
lights
39-
ambient_light 120, 120, 120
40-
vertices.each_cons(2) do |r1, r2|
41-
begin_shape(TRIANGLE_STRIP)
42-
r1.zip(r2).each do |v1, v2|
43-
vertex v1.x, v1.y, v1.z
44-
vertex v2.x, v2.y, v2.z
45-
end
46-
end_shape
33+
points.each_cons(2) { |ps, pe| line ps.x, ps.y, pe.x, pe.y }
34+
return if drawing_mode
35+
stroke 125
36+
fill 120
37+
lights
38+
ambient_light 120, 120, 120
39+
vertices.each_cons(2) do |r1, r2|
40+
begin_shape(TRIANGLE_STRIP)
41+
r1.zip(r2).each do |v1, v2|
42+
vertex v1.x, v1.y, v1.z
43+
vertex v2.x, v2.y, v2.z
4744
end
48-
end
45+
end_shape
46+
end
4947
end
5048

51-
def reset_scene
49+
def reset_scene
5250
@drawing_mode = true
5351
@points = []
5452
@rot_x = 0.0
@@ -69,28 +67,24 @@ def mouse_released
6967
recalculate_shape
7068
end
7169

72-
def recalculate_shape
70+
def recalculate_shape
7371
@vertices = []
74-
points.each_cons(2) do |ps, pe|
72+
points.each_cons(2) do |ps, _pe|
7573
b = points.last - points.first
7674
# len = b.mag
77-
b.normalize!
78-
a = ps - points.first
79-
dot = a.dot b
80-
b = b * dot
81-
normal = points.first + b
75+
b.normalize!
76+
a = ps - points.first
77+
dot = a.dot b
78+
b *= dot
79+
normal = points.first + b
8280
c = ps - normal
83-
# nlen = c.mag
84-
vertices << []
85-
(0..TAU).step(PI/15) do |ang|
81+
# nlen = c.mag
82+
vertices << []
83+
(0..TAU).step(PI / 15) do |ang|
8684
e = normal + c * cos(ang)
87-
e.z = c.mag * sin(ang)
85+
e.z = c.mag * sin(ang)
8886
vertices.last << e
8987
end
9088
end
9189
@drawing_mode = false
9290
end
93-
94-
95-
96-

samples/contributed/fern.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# The Fern Fractal
22
# by Luis Correia
33

4-
attr_reader :boundary
4+
attr_reader :bnds
55

66
def setup
77
size 500, 500
8-
@boundary = Boundary.new(0, width )
8+
@bnds = Boundary.new(0, width)
99
no_loop
10-
puts "Be patient. This takes about 10 seconds to render."
10+
puts 'Be patient. This takes about 10 seconds to render.'
1111
end
1212

1313
def draw
@@ -34,18 +34,18 @@ def draw
3434
end
3535
i = height - (y * 45).to_i
3636
j = width / 2 + (x * 45).to_i
37-
pixels[i * height + j] += 2_560 if (boundary.include?(i) && boundary.include?(j))
37+
pixels[i * height + j] += 2_560 if bnds.include?(i) && bnds.include?(j)
3838
x0, y0 = x, y
3939
end
4040
update_pixels
4141
end
4242

43-
# Abstract boundary checking to this
43+
# Abstract bnds checking to this
4444
# lightweight class
4545
#
4646

4747
Boundary = Struct.new(:lower, :upper) do
48-
def include? x
48+
def include?(x)
4949
(lower...upper).cover? x
5050
end
5151
end

samples/contributed/orbit.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,21 @@ def setup
1515
def draw
1616
background 255
1717
translate 225, 225
18-
1918
text_font d_font
2019
ellipse 0, 0, 10, 10
2120
text 'sun', 10, 0
22-
2321
3.times do |i|
2422
push_matrix
25-
2623
rotate frame_count / -180.0 * PI + i * PI / -1.5
2724
line 0, 0, 120, 0
28-
2925
translate 120, 0
3026
ellipse 0, 0, 10, 10
3127
text_font d_font, 22
3228
text 'planet', 10, 0
33-
34-
rotate frame_count / -30.0 * PI
29+
rotate frame_count / -30.0 * PI
3530
line 0, 0, 30, 0
3631
text_font d_font, 15
3732
text 'moon', 32, 0
38-
3933
pop_matrix
4034
end
41-
end
35+
end

0 commit comments

Comments
 (0)