Skip to content

Commit 15714cd

Browse files
committed
Replace constrain (processing) with clamp (ruby-2.4+)
1 parent 60f1977 commit 15714cd

File tree

25 files changed

+69
-83
lines changed

25 files changed

+69
-83
lines changed

contributed/chladni.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ def key_pressed
6363
end
6464
end
6565
@recompute = true
66-
@m = constrain(m, 1, 20)
67-
@n = constrain(n, 1, 20)
66+
@m = m.clamp(1, 20)
67+
@n = n.clamp(1, 20)
6868
end

contributed/library/particles/lib/attractor.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def calc_grav_force(p)
2323
dir = loc - p.loc # Calculate direction of force
2424
d = dir.mag # Distance between objects
2525
# Limit the distance to eliminate "extreme" result of very close or very far objects
26-
d = constrain(d, 5.0, 50.0)
26+
d = d.clamp(5.0, 50.0)
2727
# Normalize vector (distance doesn't matter here, we just want this vector for direction)
2828
dir.normalize!
2929
force = (gravity * mass * 1 / (d * d)) # Calculate gravitional force magnitude

external_library/java/hemesh/slicer.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
java_import 'wblut.processing.WB_Render3D'
77
java_import 'wblut.math.WB_Ease'
88

9-
10-
11-
129
boolean ORTHOROT
1310
boolean ORTHO
1411

@@ -329,7 +326,7 @@ def getMesh(f, int counter)
329326
transform=WB_Transform.new
330327
do
331328
if (p.parentToChild.nil?)
332-
fracf=constrain((float)(p.level-f), 0.0, 1.0)
329+
fracf=(p.level-f).clamp(0.0, 1.0)
333330
if (counter<numFrames/2)
334331
fracf=1.0-fracf
335332
end

processing_app/basics/image/colored_extrusion.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def draw
3232
else
3333
@sval -= 0.01
3434
end
35-
@sval = constrain @sval, 1.0, 2.5
35+
@sval = @sval.clamp(1.0, 2.5)
3636
translate width / 2 + @nmx * @sval - 100, height / 2 + @nmy * @sval - 200, -50
3737
scale @sval
3838
rotate_z PI / 9 - @sval + 1

processing_app/basics/image/sprite.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
# Sprite (Teddy)
2-
# by James Patterson.
3-
#
4-
# Demonstrates loading and displaying a transparent GIF image.
2+
# by James Patterson.
3+
#
4+
# Demonstrates loading and displaying a transparent GIF image.
55

66
def setup
77
sketch_title 'Sprite'
88
@teddy = load_image(data_path('teddy.gif'))
99
@xpos, @ypos = width / 2, height / 2
10-
@drag = 30.0
10+
@drag = 30.0
1111
frame_rate 60
1212
end
1313

1414
def draw
15-
background 102
15+
background 102
1616
difx = mouse_x - @xpos - @teddy.width / 2
1717
if difx.abs > 1.0
1818
@xpos += difx / @drag
19-
@xpos = constrain(@xpos, 0, width - @teddy.width / 2)
19+
@xpos = @xpos.clamp(0, width - @teddy.width / 2)
2020
end
2121
dify = mouse_y - @ypos - @teddy.height / 2
2222
if dify.abs > 1.0
2323
@ypos += dify/@drag
24-
@ypos = constrain(@ypos, 0, height - @teddy.height / 2)
24+
@ypos = @ypos.clamp(0, height - @teddy.height / 2)
2525
end
2626
image @teddy, @xpos, @ypos
2727
end

processing_app/basics/input/constrain.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def draw
1818
@mx += (mouse_x - @mx) * @easing if (mouse_x - @mx).abs > 0.1
1919
@my += (mouse_y - @my) * @easing if (mouse_y - @my).abs > 0.1
2020
distance = @ellipse_size * 2
21-
@mx = constrain @mx, @inner, (width - @inner)
22-
@my = constrain @my, @inner, (height - @inner)
21+
@mx = @mx.clamp(@inner, width - @inner)
22+
@my = @my.clamp(@inner, height - @inner)
2323
fill 76
2424
rect @edge, @edge, width - @edge, height - @edge
2525
fill 255

processing_app/basics/modules/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 Processing::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

processing_app/basics/objects/shapes/circle.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def jiggle
2525
super
2626
# The Circle jiggles both size and location.
2727
@r += rand(-1..1.0)
28-
@r = constrain(r, 0, 100)
28+
@r = r.clamp(0, 100)
2929
end
3030

3131
def display

processing_app/basics/objects/shapes/circle2.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def jiggle
2525
super
2626
# The Circle jiggles both size and location.
2727
@r += rand(-1..1.0)
28-
@r = constrain(r, 0, 100)
28+
@r = r.clamp(0, 100)
2929
end
3030

3131
# The change_color function is unique to the Circle class.

processing_app/library/sound/noise/noise_spectrum.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ def setup
2828

2929
def draw
3030
# Only play one of the four noises, based on mouseY
31-
next_noise = constrain(
32-
map1d(mouseY, 0..height, 0..noises.length),
33-
0,
34-
noises.length - 1
35-
)
31+
next_noise = constrained_map(mouseY, 0..height, 0..noises.length) )
3632
unless next_noise == current
3733
noises[current].stop
3834
@current = next_noise

processing_app/library/sound/oscillators/oscillator_spectrum.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ def setup
3636

3737
def draw
3838
# Only play one of the four oscillators, based on mouseY
39-
next_oscillator = constrain(
40-
map1d(mouseY, 0..height, 0..count),
41-
0,
42-
count - 1
43-
)
39+
next_oscillator = constrained_map(mouseY, 0..height, 0..count)
4440
unless next_oscillator == current
4541
oscillators[current].stop
4642
@current = next_oscillator

processing_app/library/vecmath/vec2d/circles.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def update
107107
end
108108

109109
def check_edges # could be more sophisticated, eg reverse velocity
110-
pos.x = constrain(pos.x, 0, width)
111-
pos.y = constrain(pos.y, 0, height)
110+
pos.x = pos.x.clamp(0, width)
111+
pos.y = pos.y.clamp(0, height)
112112
end
113113
end

processing_app/library/vecmath/vec2d/library/verlet_chain/lib/verlet_ball.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ def render
3939

4040
def bounds_collision
4141
if x_bound.exclude? pos.x
42-
pos_old.x = constrain pos.x, x_bound.lower, x_bound.upper
42+
pos_old.x = pos.x.clamp(x_bound.lower, x_bound.upper)
4343
pos.x = (pos.x <= radius)? pos_old.x + push.x : pos_old.x - push.x
4444
end
4545
return unless y_bound.exclude? pos.y
46-
pos_old.y = constrain pos.y, y_bound.lower, y_bound.upper
46+
pos_old.y = pos.y.clamp(y_bound.lower, y_bound.upper)
4747
pos.y = (pos.y <= radius)? pos_old.y + push.y : pos_old.y - push.y
4848
end
4949
end

processing_app/library/vecmath/vec2d/seeking_neural.rb

+20-20
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,40 @@ def initialize(n, c)
99
@weights = Array.new(n) { rand(0..1.0) }
1010
@c = c
1111
end
12-
12+
1313
# Function to train the Perceptron
1414
# Weights are adjusted based on vehicle's error
1515
def train(forces, error)
1616
trained = @weights.zip(forces.map { |f| f.to_a }
1717
.map { |a, b| (a * error.x + b * error.y) * @c })
18-
.map { |w, c| constrain(w + c, 0, 1.0) }
18+
.map { |w, c| (w + c).clamp(0, 1.0) }
1919
@weights = trained
2020
end
21-
21+
2222
# Give me a steering result
2323
def feedforward(forces)
2424
# Sum all values
2525
forces.zip(@weights).map { |a, b| a * b }.reduce(Vec2D.new, :+)
2626
# forces.zip(@weights).map { |a, b| a * b }.reduce(:+)
2727
end
28-
end
28+
end
2929
# Seek
3030
# Daniel Shiffman <http://www.shiffman.net>
31-
31+
3232
class Vehicle
3333
MAX_SPEED = 4
3434
MAX_FORCE = 0.1
3535

3636
attr_reader :brain, :sz, :location, :targets, :desired
37-
37+
3838
def initialize(n, x, y)
3939
@brain = Perceptron.new(n, 0.001)
4040
@acceleration = Vec2D.new
4141
@velocity = Vec2D.new
4242
@location = Vec2D.new(x, y)
4343
@sz = 6.0
4444
end
45-
45+
4646
# Method to update location
4747
def update(width, height)
4848
# Update velocity
@@ -52,15 +52,15 @@ def update(width, height)
5252
@location += @velocity
5353
# Reset acceleration to 0 each cycle
5454
@acceleration *= 0
55-
@location.x = constrain(location.x, 0, width)
56-
@location.y = constrain(location.y, 0, height)
55+
@location.x = location.x.clamp(0, width)
56+
@location.y = location.y.clamp(0, height)
5757
end
58-
58+
5959
def apply_force(force)
6060
# We could add mass here if we want A = F / M
6161
@acceleration += force
6262
end
63-
63+
6464
# Here is where the brain processes everything
6565
def steer(targets, desired)
6666
# Steer towards all targets
@@ -73,7 +73,7 @@ def steer(targets, desired)
7373
error = desired - location
7474
brain.train(forces, error)
7575
end
76-
76+
7777
# A method that calculates a steering force towards a target
7878
# STEER = DESIRED MINUS VELOCITY
7979
def seek(target)
@@ -86,8 +86,8 @@ def seek(target)
8686
steer.set_mag(MAX_FORCE) { steer.mag > MAX_FORCE } # Limit to a maximum steering force
8787
steer
8888
end
89-
90-
def display
89+
90+
def display
9191
# Draw a triangle rotated in the direction of velocity
9292
theta = @velocity.heading + PI / 2
9393
fill(175)
@@ -103,7 +103,7 @@ def display
103103
end_shape(CLOSE)
104104
pop_matrix
105105
end
106-
end
106+
end
107107
end
108108

109109
include SeekingNeural
@@ -114,9 +114,9 @@ def display
114114
def setup
115115
sketch_title 'Seeking Neural'
116116
# The Vehicle's desired location
117-
@desired = Vec2D.new(width / 2, height / 2)
117+
@desired = Vec2D.new(width / 2, height / 2)
118118
# Create a list of targets
119-
make_targets
119+
make_targets
120120
# Create the Vehicle (it has to know about the number of targets
121121
# in order to configure its brain)
122122
@v = Vehicle.new(targets.size, rand(width), rand(height))
@@ -128,12 +128,12 @@ def make_targets
128128
end
129129

130130
def draw
131-
background(255)
131+
background(255)
132132
# Draw a circle to show the Vehicle's goal
133133
stroke(0)
134134
stroke_weight(2)
135135
fill(0, 100)
136-
ellipse(desired.x, desired.y, 36, 36)
136+
ellipse(desired.x, desired.y, 36, 36)
137137
# Draw the targets
138138
targets.each do |target|
139139
no_fill
@@ -142,7 +142,7 @@ def draw
142142
ellipse(target.x, target.y, 16, 16)
143143
line(target.x, target.y - 16, target.x, target.y + 16)
144144
line(target.x - 16, target.y, target.x + 16, target.y)
145-
end
145+
end
146146
# Update the Vehicle
147147
v.steer(targets, desired)
148148
v.update(width, height)

processing_app/topics/advanced_data/counting_words.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def initialize(word)
66
@word = word
77
@count = 1
88
end
9-
9+
1010
def increment
1111
@count += 1
1212
end
@@ -55,7 +55,7 @@ def draw
5555
# Only display words that appear 3 times
5656
if w.count > 3 # access word count
5757
# The size is the count
58-
fsize = constrain(w.count, 0, 100)
58+
fsize = w.count.clamp(0, 100)
5959
text_size(fsize)
6060
text(w.word, x, y)
6161
# Move along the x-axis

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

+2-2
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 = speed.clamp(0, 10.0)
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 = fs.clamp(2, 48)
5757
text_size(fs)
5858
text_align(CENTER)
5959
text(word, position[X], position[Y])

processing_app/topics/cellular_automata/game_of_life.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def draw
5252
if pause && mouse_pressed?
5353
# # Map and avoid out of bound errors
5454
over_x = (map1d(mouse_x, (0..width), (0..row))).to_i
55-
over_x = constrain(over_x, 0, row - 1)
55+
over_x = over_x.clamp(0, row - 1)
5656
over_y = (map1d(mouse_y, (0..height), (0..column))).to_i
57-
over_y = constrain(over_y, 0, column - 1)
57+
over_y = over_y.clamp(0, column - 1)
5858
# Check against cells in buffer
5959
if cells_buffer[over_x][over_y] # Cell is alive
6060
cells[over_x][over_y] = DEAD # Kill

processing_app/topics/gui/handles.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def update
5858
press_event
5959
end
6060
return unless press
61-
@stretch = constrain(mouse_x - width / 2 - size / 2, 0, width / 2 - size - 1)
61+
@stretch = (mouse_x - width / 2 - size / 2).clamp(0, width / 2 - size - 1)
6262
end
6363

6464
def over_event

processing_app/topics/gui/scrollbar.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def update
6666
else
6767
@locked = app.mouse_pressed? && over_event?
6868
end
69-
@newspos = constrain(mouse_x - wd / 2, spos_min, spos_max) if locked
69+
@newspos = (mouse_x - wd / 2).clamp(spos_min, spos_max) if locked
7070
@spos = spos + (newspos - spos) / loose if (newspos - spos).abs > 1
7171
end
7272

0 commit comments

Comments
 (0)