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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 5 deletions
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

0 commit comments

Comments
 (0)