Skip to content

Commit 2500b8b

Browse files
author
monkstone
committed
still doing tidy up
1 parent b0bbe09 commit 2500b8b

File tree

4 files changed

+37
-34
lines changed

4 files changed

+37
-34
lines changed

samples/contributed/drawolver.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# with a 'one_of_each' method
1010

1111
load_library :vecmath
12-
import 'vecmath'
1312
attr_reader :drawing_mode, :points, :rot_x, :rot_y, :vertices
1413

1514
def setup

samples/contributed/gravity.rb

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
def setup
66
size 600, 500
77
@particles = []
8-
@grabbed = nil
8+
@grabbed = nil
99
background 0
1010
smooth 4
1111
stroke_weight 4
@@ -17,7 +17,7 @@ def draw
1717
no_stroke
1818
fill 0, 60
1919
rect 0, 0, width, height
20-
particles.each { |p| p.collect_force; p.move; p.render }
20+
particles.each(&:run)
2121
end
2222

2323
def mouse_pressed
@@ -35,17 +35,17 @@ def particle_grab
3535
end
3636

3737
class Particle
38-
GRAVITY = 1.0
38+
GRAVITY = 1.0
3939
attr_reader :x0, :y0, :x1, :y1, :diameter, :mass_amount
40-
40+
4141
def initialize(x, y, mass)
4242
@x0, @y0, @x1, @y1 = x, y, x, y
4343
@x_speed, @y_speed = 0, 0
4444
@x_accel, @y_accel = 0, 0
4545
@mass_amount = mass
4646
@diameter = sqrt(mass_amount) * 20
4747
end
48-
48+
4949
def collect_force
5050
@x_accel, @y_accel = 0, 0
5151
@min_dist = 1000
@@ -61,23 +61,29 @@ def collect_force
6161
end
6262
end
6363
end
64-
64+
6565
def move
6666
@x_speed, @y_speed = 0, 0 if grabbed?
6767
@x_speed += @x_accel
6868
@y_speed += @y_accel
6969
@x1, @y1 = x0 + @x_speed, y0 + @y_speed
7070
end
71-
71+
7272
def grabbed?
7373
$app.grabbed == self
7474
end
75-
75+
76+
def run
77+
collect_force
78+
move
79+
render
80+
end
81+
7682
def render
7783
no_stroke
7884
grabbed? ? render_grabbed : render_free
7985
end
80-
86+
8187
def render_free
8288
charge_col = 1000.0 / @min_dist / 50.0
8389
tot_col_1 = 100 + charge_col * 6
@@ -90,7 +96,7 @@ def render_free
9096
ellipse x1, y1, diameter, diameter
9197
@x0, @y0 = x1, y1
9298
end
93-
99+
94100
def render_grabbed
95101
fill 150, 150, 255, 100
96102
ellipse mouse_x, mouse_y, diameter + 8, diameter + 8
@@ -99,8 +105,8 @@ def render_grabbed
99105
ellipse mouse_x, mouse_y, diameter, diameter
100106
@x0, @y0 = mouse_x, mouse_y
101107
end
102-
108+
103109
def angle_of(x1, y1, x2, y2)
104110
Math::PI - atan2(y1 - y2, x1 - x2)
105-
end
111+
end
106112
end

samples/contributed/mandelbrot.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Mandelbrot Set example
22
# by Jordan Scales (http://jordanscales.com)
3-
# Modified to use map1d (instead of map), and somewhat
3+
# Modified to use map1d (instead of map), and somewhat
44
# optimized (update_pixels instead of set, and hypot for abs)
55
# default size 900x600
66
# no need to loop
@@ -15,7 +15,7 @@ def setup
1515
def draw
1616
(0...900).each do |x|
1717
(0...600).each do |y|
18-
c = Complex.new(map1d(x, (0...900), (-3..1.5)), map1d(y, (0...600), (-1.5..1.5)))
18+
c = Complex.new(map1d(x, (0...900), (-3..1.5)), map1d(y, (0...600), (-1.5..1.5)))
1919
# mandel will return 0 to 20 (20 is strong)
2020
# map this to 0, 255 (and flip it)
2121
pixels[x + y * 900] = color(255 - map1d(mandel(c, 20), (0..20), (0..255)).to_i)
@@ -29,7 +29,7 @@ def draw
2929
def mandel(z, max = 10)
3030
score = 0
3131
c = z.clone
32-
while score < max do
32+
while score < max
3333
# z = z^2 + c
3434
z.square
3535
z.add c
@@ -40,12 +40,11 @@ def mandel(z, max = 10)
4040
end
4141

4242
# rolled my own Complex class
43-
# stripped of all functionality, except for what I needed (abs, square, add, to_s)
44-
#
45-
# Using this class, runs in ~12.5s on my MacBook Air
46-
# compared to ~22s using ruby's Complex struct
43+
# including only the functionality I need (abs, square, add, to_s)
44+
#
45+
# Using this class, runs in ~12.5s on my MacBook Air
46+
# compared to ~22s using ruby's Complex struct
4747
class Complex
48-
4948
attr_accessor :real, :imag
5049

5150
def initialize(real, imag)
@@ -73,6 +72,6 @@ def abs
7372
end
7473

7574
def to_s
76-
format("(%d+%di)", real, imag)
75+
format('(%d+%di)', real, imag)
7776
end
7877
end

samples/contributed/polyhedrons.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
###
22
# Polyhedrons after sketch by Chinchbug on openprocessing.org
33
###
4-
54
Vert = Struct.new(:x, :y, :z) do
65
def dist_sq(v)
76
(x - v.x) * (x - v.x) + (y - v.y) * (y - v.y) + (z - v.z) * (z - v.z)
@@ -16,7 +15,7 @@ def self.permutations(x, y, z)
1615
PHI_SQ = PHI * PHI
1716
PHI_CU = PHI * PHI * PHI
1817
ROOT2 = sqrt(2)
19-
TYPE = "Type Archimedian\nFaces: %d "
18+
TYPE = "Type Archimedian\nFaces: "
2019

2120
attr_reader :verts, :curr_id, :scayl, :ang, :spd, :name, :notes, :off_x, :off_y
2221
attr_reader :off_z, :len_edge
@@ -164,70 +163,70 @@ def create_poly(id)
164163
@scayl = 46
165164
when 6
166165
@name = 'Cuboctahedron:'
167-
@notes = "#{TYPE}triangles, 6 squares\nVertices 12\nEdges 24" % 8
166+
@notes = format("%s %d triangles, 6 squares\nVertices 12\nEdges 24", TYPE, 8)
168167
permutations(1, 0, 1)
169168
@len_edge = ROOT2
170169
@scayl = 170
171170
when 7
172171
@name = 'Truncated Cube:'
173-
@notes = "#{TYPE}triangles, 6 octogons\nVertices 24\nEdges 36" % 8
172+
@notes = format("%s %d triangles, 6 octogons\nVertices 24\nEdges 36", TYPE, 8)
174173
permutations(ROOT2 - 1, 1, 1)
175174
@len_edge = 2 * (ROOT2 - 1)
176175
@scayl = 155
177176
when 8
178177
@name = 'Truncated Octahedron:'
179-
@notes = "#{TYPE}squares, 8 hexagons\nVertices 24\nEdges 36" % 6
178+
@notes = format("%s %d squares, 8 hexagons\nVertices 24\nEdges 36", TYPE, 6)
180179
permutations(0, 1, 2)
181180
permutations(2, 1, 0)
182181
@len_edge = ROOT2
183182
@scayl = 100
184183
when 9
185184
@name = 'Rhombicuboctahedron:'
186-
@notes = "#{TYPE}triangles, 18 squares\nVertices 24\nEdges 48" % 8
185+
@notes = format("%s %d triangles, 18 squares\nVertices 24\nEdges 48", TYPE, 8)
187186
permutations(ROOT2 + 1, 1, 1)
188187
@len_edge = 2
189188
@scayl = 80
190189
when 10
191190
@name = 'Truncated Cuboctahedron:'
192-
@notes = "#{TYPE}squares, 8 hexagons, 6 octogons\nVertices 48\nEdges 72" % 12
191+
@notes = format("%s %d squares, 8 hexagons, 6 octogons\nVertices 48\nEdges 72", TYPE, 12)
193192
permutations(ROOT2 + 1, 2 * ROOT2 + 1, 1)
194193
permutations(ROOT2 + 1, 1, 2 * ROOT2 + 1)
195194
@len_edge = 2
196195
@scayl = 50
197196
when 11
198197
@name = 'Icosidodecahedron:'
199-
@notes = "#{TYPE}triangles, 12 pentagons\nVertices 30\nEdges 60" % 20
198+
@notes = format("%s %d triangles, 12 pentagons\nVertices 30\nEdges 60", TYPE, 20)
200199
permutations(0, 0, 2 * PHI)
201200
permutations(1, PHI, PHI_SQ)
202201
@len_edge = 2
203202
@scayl = 70
204203
when 12
205204
@name = 'Truncated Dodecahedron:'
206-
@notes = "#{TYPE}triangles, 12 decagons\nVertices 60\nEdges 90" % 20
205+
@notes = format("%s %d triangles, 12 decagons\nVertices 60\nEdges 90", TYPE, 20)
207206
permutations(0, 1 / PHI, PHI + 2)
208207
permutations(1 / PHI, PHI, 2 * PHI)
209208
permutations(PHI, 2, PHI_SQ)
210209
@len_edge = 2 * (PHI - 1)
211210
@scayl = 60
212211
when 13
213212
@name = 'Truncated Icosahedron:'
214-
@notes = "#{TYPE}pentagons, 20 hexagons\nVertices 60\nEdges 90" % 12
213+
@notes = format("%s %d pentagons, 20 hexagons\nVertices 60\nEdges 90", TYPE, 12)
215214
permutations(0, 1, 3 * PHI)
216215
permutations(2, 2 * PHI + 1, PHI)
217216
permutations(1, PHI + 2, 2 * PHI)
218217
@len_edge = 2
219218
@scayl = 45
220219
when 14
221220
@name = 'Small Rhombicosidodecahedron:'
222-
@notes = "#{TYPE}triangles, 30 squares, 12 pentagons\nVertices 60\nEdges 120" %20
221+
@notes = format("%s %d triangles, 30 squares, 12 pentagons\nVertices 60\nEdges 120", TYPE, 20)
223222
permutations(1, 1, PHI_CU)
224223
permutations(PHI_SQ, PHI, 2 * PHI)
225224
permutations(PHI + 2, 0, PHI_SQ)
226225
@len_edge = 2
227226
@scayl = 50
228227
when 15
229228
@name = 'Great Rhombicosidodecahedron:'
230-
@notes = "#{TYPE}squares, 20 hexagons, 12 decagons\nVertices 120\nEdges 180" %30
229+
@notes = format("%s %d squares, 20 hexagons, 12 decagons\nVertices 120\nEdges 180", TYPE, 30)
231230
permutations(1 / PHI, 1 / PHI, PHI + 3)
232231
permutations(2 / PHI, PHI, 2 * PHI + 1)
233232
permutations(1 / PHI, PHI_SQ, 3 * PHI - 1)

0 commit comments

Comments
 (0)