Skip to content

Commit 5ddd8f2

Browse files
author
monkstone
committed
mainly replace constrain with 'clip to range method'
1 parent 5074dd2 commit 5ddd8f2

File tree

14 files changed

+270
-301
lines changed

14 files changed

+270
-301
lines changed
Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
21
# Sprite (Teddy)
3-
# by James Patterson.
4-
#
5-
# Demonstrates loading and displaying a transparent GIF image.
6-
2+
# by James Patterson.
3+
#
4+
# Demonstrates loading and displaying a transparent GIF image.
5+
# Also ruby-processings clip to range method (cf processing constrain)
6+
attr_reader :drag, :teddy, :xpos, :ypos
77

88
def setup
99
size 200, 200
10-
@teddy = load_image "teddy.gif"
11-
@xpos, @ypos = width/2, height/2
12-
@drag = 30.0
10+
@teddy = load_image 'teddy.gif'
11+
@xpos, @ypos = width / 2, height / 2
12+
@drag = 30.0
1313
frame_rate 60
1414
end
1515

1616
def draw
17-
background 102
18-
difx = mouse_x - @xpos - @teddy.width/2
17+
background 102
18+
difx = mouse_x - xpos - teddy.width / 2
1919
if difx.abs > 1.0
20-
@xpos += difx/@drag
21-
@xpos = constrain( @xpos, 0, width-@teddy.width/2 )
20+
@xpos += difx / drag
21+
@xpos = (0..width - teddy.width / 2).clip(xpos)
2222
end
23-
24-
dify = mouse_y - @ypos - @teddy.height/2
23+
dify = mouse_y - ypos - teddy.height / 2
2524
if dify.abs > 1.0
26-
@ypos += dify/@drag
27-
@ypos = constrain( @ypos, 0, height-@teddy.height/2 )
25+
@ypos += dify / drag
26+
@ypos = (0..height - teddy.height / 2).clip(ypos)
2827
end
29-
30-
image @teddy, @xpos, @ypos
28+
image teddy, xpos, ypos
3129
end
32-
Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
1-
# Zoom.
2-
#
1+
# Zoom.
2+
#
33
# Move the cursor over the image to alter its position. Click and press
44
# the mouse to zoom and set the density of the matrix by typing numbers 1-5.
5-
# This program displays a series of lines with their heights corresponding to
6-
# a color value read from an image.
5+
# This program displays a series of lines with their heights corresponding to
6+
# a color value read from an image.
7+
# Uses ruby-processings clip to range method (cf processing constrain)
8+
attr_reader :sval, :img, :img_pixels, :nmx, :nmy, :res
79

8-
def setup
9-
size 640, 360, P3D
10-
no_fill
11-
stroke 255
12-
@nmx = 0.0
13-
@nmy = 0.0
14-
@sval = 1.0
15-
@res = 5
16-
@img = load_image "ystone08.jpg"
17-
@img_pixels = []
18-
(0...@img.height).each do |y|
19-
@img_pixels << []
20-
(0...@img.width).each do |x|
21-
@img_pixels.last << @img.get(y, x)
22-
end
23-
end
10+
def setup
11+
size 640, 360, P3D
12+
no_fill
13+
stroke 255
14+
@nmx = 0.0
15+
@nmy = 0.0
16+
@sval = 1.0
17+
@res = 5
18+
@img = load_image 'ystone08.jpg'
19+
@img_pixels = []
20+
(0...img.height).each do |y|
21+
img_pixels << []
22+
(0...@img.width).each do |x|
23+
img_pixels.last << img.get(y, x)
24+
end
25+
end
2426
end
2527

26-
def draw
27-
background 0
28-
@nmx += (mouse_x - @nmx) / 20
29-
@nmy += (mouse_y - @nmy) / 20
30-
if mouse_pressed?
31-
@sval += 0.005
32-
else
33-
@sval -= 0.01
34-
end
35-
@sval = constrain @sval, 1.0, 2.5
36-
translate width/2 + @nmx * @sval - 100, height/2 + @nmy * @sval - 200, -50
37-
scale @sval
38-
rotate_z PI/9 - @sval + 1
39-
rotate_x PI/@sval/8 - 0.125
40-
rotate_y @sval/8 - 0.125
41-
translate -width/2, -height/2
42-
(0...@img.height).step(@res) do |y|
43-
(0...@img.width).step(@res) do |x|
44-
rr = red @img_pixels[y][x]
45-
gg = green @img_pixels[y][x]
46-
bb = blue @img_pixels[y][x]
47-
tt = rr + gg + bb
48-
stroke rr, gg, gg
49-
line y, x, tt/10 - 20, y, x, tt/10
50-
end
28+
def draw
29+
background 0
30+
@nmx += (mouse_x - nmx) / 20
31+
@nmy += (mouse_y - nmy) / 20
32+
if mouse_pressed?
33+
@sval += 0.005
34+
else
35+
@sval -= 0.01
36+
end
37+
@sval = (1.0..2.5).clip sval
38+
translate width / 2 + nmx * sval - 100, height / 2 + nmy * sval - 200, -50
39+
scale sval
40+
rotate_z PI / 9 - sval + 1
41+
rotate_x PI / sval / 8 - 0.125
42+
rotate_y sval / 8 - 0.125
43+
translate(-width / 2, -height / 2)
44+
(0...img.height).step(res) do |y|
45+
(0...img.width).step(res) do |x|
46+
rr = red(img_pixels[y][x])
47+
gg = green(img_pixels[y][x])
48+
bb = blue(img_pixels[y][x])
49+
tt = rr + gg + bb
50+
stroke rr, gg, gg
51+
line y, x, tt / 10 - 20, y, x, tt / 10
5152
end
52-
53+
end
5354
end
5455

55-
def key_pressed
56-
k = key.to_i
57-
@res = k if k > 0 && k < 6
56+
def key_pressed
57+
k = key.to_i
58+
@res = k if (1..6).include? k
5859
end
59-
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
2-
# Move the mouse across the screen to move the circle.
3-
# The program constrains the circle to its box.
4-
1+
# Move the mouse across the screen to move the circle.
2+
# The program constrains the circle to its box.
3+
# You can still use processing constrain if you wish, but
4+
# since it is implemented using ruby-processing 'clip to range'
5+
# method we prefer that here...
6+
attr_reader :inner, :easing, :edge, :ellipse_size, :mx, :my
57

68
def setup
79
size 640, 360
@@ -12,22 +14,17 @@ def setup
1214
@easing = 0.05
1315
@ellipse_size = 24.0
1416
@edge = 100
15-
@inner = @edge + @ellipse_size
17+
@inner = edge + ellipse_size
1618
end
1719

1820
def draw
1921
background 51
20-
21-
@mx += (mouse_x - @mx) * @easing if (mouse_x - @mx).abs > 0.1
22-
@my += (mouse_y - @my) * @easing if (mouse_y - @my).abs > 0.1
23-
24-
distance = @ellipse_size * 2
25-
@mx = constrain @mx, @inner, (width - @inner)
26-
@my = constrain @my, @inner, (height - @inner)
27-
22+
@mx += (mouse_x - mx) * easing if (mouse_x - mx).abs > 0.1
23+
@my += (mouse_y - my) * easing if (mouse_y - my).abs > 0.1
24+
@mx = (inner..(width - inner)).clip mx
25+
@my = (inner..(height - inner)).clip my
2826
fill 76
29-
rect @edge, @edge, width - @edge, height - @edge
30-
27+
rect edge, edge, width - edge, height - edge
3128
fill 255
32-
ellipse @mx, @my, @ellipse_size, @ellipse_size
29+
ellipse mx, my, ellipse_size, ellipse_size
3330
end

samples/processing_app/topics/advanced_data/counting_words.rb

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,25 @@ def draw
4747
# the Hash using the word String as the key
4848
concordance[s] = Word.new(s)
4949
end
50-
5150
# x and y will be used to locate each word
5251
x = 0
5352
y = height - 10
54-
5553
# Look at each word
5654
concordance.values.each do |w|
5755
# Only display words that appear 3 times
5856
if w.count > 3 # access word count
5957
# The size is the count
60-
fsize = constrain(w.count, 0, 100)
58+
fsize = (0..100).clip w.count
6159
text_size(fsize)
6260
text(w.word, x, y)
6361
# Move along the x-axis
6462
x += text_width(w.word) + 1
6563
end
6664
# If x gets to the end, move y
6765
# If y == 0 we are done
68-
if y == 0
69-
no_loop
70-
else
71-
if x > width
72-
x = 0
73-
y = (y < 0) ? 0 : y - 100
74-
end
75-
end
66+
no_loop if y == 0
67+
next unless x >= width
68+
x = 0
69+
y = (y < 0) ? 0 : y - 100
7670
end
7771
end

samples/processing_app/topics/advanced_data/library/word/word.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def increment_franken
3939
# The more often it appears, the faster it falls
4040
def move
4141
@speed = map1d(total_count, (5..25), (0.1..0.4))
42-
@speed = constrain(speed, 0, 10.0)
42+
@speed = (0..10.0).clip speed
4343
@position[Y] += speed
4444
@position[Y] = -height if position[Y] > height * 2
4545
end
@@ -53,7 +53,7 @@ def display
5353
end
5454
# Its size is also tied to number of occurences
5555
fs = map1d(total_count, (5..25), (2..24.0))
56-
fs = constrain(fs, 2, 48)
56+
fs = (2..48).clip fs
5757
text_size(fs)
5858
text_align(CENTER)
5959
text(word, position[X], position[Y])

samples/processing_app/topics/cellular_automata/game_of_life.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ def draw
5555
if pause && mouse_pressed?
5656
# # Map and avoid out of bound errors
5757
over_x = (map1d(mouse_x, (0..width), (0..row))).to_i
58-
over_x = constrain(over_x, 0, row - 1)
58+
over_x = (0..row - 1).clip over_x
5959
over_y = (map1d(mouse_y, (0..height), (0..column))).to_i
60-
over_y = constrain(over_y, 0, column - 1)
60+
over_y = (0..column - 1).clip over_y
6161
# Check against cells in buffer
6262
if cells_buffer[over_x][over_y] # Cell is alive
6363
cells[over_x][over_y] = DEAD # Kill

0 commit comments

Comments
 (0)