Skip to content

Commit b405b7b

Browse files
committed
replace processing constrain with clamp
1 parent b1f7ed5 commit b405b7b

File tree

17 files changed

+78
-249
lines changed

17 files changed

+78
-249
lines changed

advanced_data/counting_words.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def draw
5656
# Only display words that appear 3 times
5757
if w.count > 3 # access word count
5858
# The size is the count
59-
fsize = constrain(w.count, 0, 100)
59+
fsize = w.count.clamp(4, 100)
6060
text_size(fsize)
6161
text(w.word, x, y)
6262
# Move along the x-axis

advanced_data/library/word/word.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def increment_franken
4040
# The more often it appears, the faster it falls
4141
def move
4242
@speed = map1d(total_count, (5..25), (0.1..0.4))
43-
@speed = constrain(speed, 0, 10.0)
43+
@speed = speed.clamp(0, 10.0)
4444
@position[Y] += speed
4545
@position[Y] = -height if position[Y] > height * 2
4646
end
@@ -54,7 +54,7 @@ def display
5454
end
5555
# Its size is also tied to number of occurences
5656
fs = map1d(total_count, (5..25), (2..24.0))
57-
fs = constrain(fs, 2, 48)
57+
fs = fs.clamp(2, 48)
5858
text_size(fs)
5959
text_align(CENTER)
6060
text(word, position[X], position[Y])

basics/image/colored_extrusion.rb

+9-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# the mouse to zoom and set the density of the matrix by typing numbers 1-5.
66
# This program displays a series of lines with their heights corresponding to
77
# a color value read from an image.
8-
ColoredExtrusion < Processing::App
8+
class ColoredExtrusion < Processing::App
99
def setup
1010
sketch_title 'Colored Extrusion'
1111
no_fill
@@ -33,22 +33,20 @@ def draw
3333
else
3434
@sval -= 0.01
3535
end
36-
@sval = constrain @sval, 1.0, 2.5
36+
@sval = @sval.clamp(1.0, 2.5)
3737
translate width / 2 + @nmx * @sval - 100, height / 2 + @nmy * @sval - 200, -50
3838
scale @sval
3939
rotate_z PI / 9 - @sval + 1
4040
rotate_x PI / @sval / 8 - 0.125
4141
rotate_y @sval / 8 - 0.125
4242
translate -width / 2, -height / 2
43-
(0...@img.height).step(@res) do |y|
44-
(0...@img.width).step(@res) do |x|
45-
rr = red @img_pixels[y][x]
46-
gg = green @img_pixels[y][x]
47-
bb = blue @img_pixels[y][x]
48-
tt = rr + gg + bb
49-
stroke rr, gg, gg
50-
line y, x, tt / 10 - 20, y, x, tt / 10
51-
end
43+
grid(@img.width, @img.height, @res, @res) do |x, y|
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
5250
end
5351
end
5452

basics/image/sprite.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ def draw
1717
difx = mouse_x - @xpos - @teddy.width / 2
1818
if difx.abs > 1.0
1919
@xpos += difx / @drag
20-
@xpos = constrain(@xpos, 0, width - @teddy.width / 2)
20+
@xpos = @xpos.clamp(0, width - @teddy.width / 2)
2121
end
2222
dify = mouse_y - @ypos - @teddy.height / 2
2323
if dify.abs > 1.0
2424
@ypos += dify/@drag
25-
@ypos = constrain(@ypos, 0, height - @teddy.height / 2)
25+
@ypos = @ypos.clamp(0, height - @teddy.height / 2)
2626
end
2727
image @teddy, @xpos, @ypos
2828
end

basics/modules/library/euler_ball/euler_ball.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ def bounds_collision
3838
end
3939
end
4040

41-
# Convenient boundary class, we only include MathTool module for constrain
41+
# Convenient boundary class
4242
class Bounds
43-
include MathTool
4443
attr_reader :low, :high, :inside
4544
def initialize(lower:, upper:)
4645
@low = lower
@@ -51,6 +50,6 @@ def initialize(lower:, upper:)
5150
# Returns the current position or the limit, sets the `inside` flag
5251
def position(val)
5352
@inside = (low..high).cover? val
54-
constrain(val, low, high)
53+
val.clamp(low, high)
5554
end
5655
end

basics/objects/polymorphism.rb

+25-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
require 'picrate'
2-
require_relative 'shapes/square'
3-
require_relative 'shapes/circle.rb'
42

5-
class Polymorphism < Processing::App
63
# Learning Processing
74
# Daniel Shiffman
85
# http://www.learningprocessing.com
@@ -13,30 +10,31 @@ class Polymorphism < Processing::App
1310
# this is a PiCrate port. Introducing the hook method, keyword args and the
1411
# post_initialization hook for flexible inheritance. Important we only
1512
# really need to know 'run' method. Note initialization of color can be
16-
# specified for Circle,defaults to color(rand(255), 100) see shapes/circle.rb.
17-
18-
attr_reader :shps
19-
20-
def setup
21-
sketch_title 'Polymorphism'
22-
@shps = []
23-
30.times do
24-
if rand < 0.5
25-
shps << Circle.new(x: 320, y: 180, r: 32, c: color(rand(255), 100))
26-
else
27-
shps << Square.new(x: 320, y: 180, r: 32)
28-
end
29-
end
30-
end
31-
32-
def draw
33-
background(255)
34-
shps.each(&:run)
35-
end
36-
37-
def settings
38-
size(480, 270)
39-
end
13+
# specified for Circle,defaults to color(rand(255), 100) see shapes library.
14+
class Polymorphism < Processing::App
15+
load_library :shapes
16+
attr_reader :shps
17+
18+
def setup
19+
sketch_title 'Polymorphism'
20+
@shps = []
21+
30.times do
22+
if rand < 0.5
23+
shps << Circle.new(x: 320, y: 180, r: 32, c: color(rand(255), 100))
24+
else
25+
shps << Square.new(x: 320, y: 180, r: 32)
26+
end
27+
end
28+
end
29+
30+
def draw
31+
background(255)
32+
shps.each(&:run)
33+
end
34+
35+
def settings
36+
size(480, 270)
37+
end
4038
end
4139

4240
Polymorphism.new

basics/objects/polymorphism2.rb

+26-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
require 'picrate'
2-
require_relative 'shapes/square'
3-
require_relative 'shapes/circle2.rb'
42

5-
class Polymorphism2 < Processing::App
63
# Learning Processing
74
# Daniel Shiffman
85
# http://www.learningprocessing.com
@@ -13,30 +10,32 @@ class Polymorphism2 < Processing::App
1310
# this is a PiCrate port. Introducing the hook method,
1411
# keyword args and the post_initialization hook for flexible inheritance.
1512
# Important change we only really need to know run method initialization of
16-
# color is also really irrelevant save showing how to use hook.
17-
18-
attr_reader :shps
19-
20-
def setup
21-
sketch_title 'Polymorphism'
22-
@shps = []
23-
30.times do
24-
if rand < 0.5
25-
shps << Circle.new(x: 320, y: 180, r: 32)
26-
else
27-
shps << Square.new(x: 320, y: 180, r: 32)
28-
end
29-
end
30-
end
31-
32-
def draw
33-
background(255)
34-
shps.each(&:run)
35-
end
36-
37-
def settings
38-
size(480, 270)
39-
end
13+
# color is also really irrelevant save showing how to use hook
14+
class Polymorphism2 < Processing::App
15+
load_library :shapes
16+
17+
attr_reader :shps
18+
19+
def setup
20+
sketch_title 'Polymorphism'
21+
@shps = []
22+
30.times do
23+
if rand < 0.5
24+
shps << Circle2.new(x: 320, y: 180, r: 32)
25+
else
26+
shps << Square.new(x: 320, y: 180, r: 32)
27+
end
28+
end
29+
end
30+
31+
def draw
32+
background(255)
33+
shps.each(&:run)
34+
end
35+
36+
def settings
37+
size(480, 270)
38+
end
4039
end
4140

4241
Polymorphism2.new

basics/objects/shapes/circle.rb

-42
This file was deleted.

basics/objects/shapes/circle2.rb

-50
This file was deleted.

basics/objects/shapes/shape.rb

-37
This file was deleted.

basics/objects/shapes/square.rb

-27
This file was deleted.

0 commit comments

Comments
 (0)