Skip to content

Commit

Permalink
Replace constrain (processing) with clamp (ruby-2.4+)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Aug 5, 2020
1 parent 60f1977 commit 15714cd
Show file tree
Hide file tree
Showing 25 changed files with 69 additions and 83 deletions.
4 changes: 2 additions & 2 deletions contributed/chladni.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ def key_pressed
end
end
@recompute = true
@m = constrain(m, 1, 20)
@n = constrain(n, 1, 20)
@m = m.clamp(1, 20)
@n = n.clamp(1, 20)
end
2 changes: 1 addition & 1 deletion contributed/library/particles/lib/attractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def calc_grav_force(p)
dir = loc - p.loc # Calculate direction of force
d = dir.mag # Distance between objects
# Limit the distance to eliminate "extreme" result of very close or very far objects
d = constrain(d, 5.0, 50.0)
d = d.clamp(5.0, 50.0)
# Normalize vector (distance doesn't matter here, we just want this vector for direction)
dir.normalize!
force = (gravity * mass * 1 / (d * d)) # Calculate gravitional force magnitude
Expand Down
5 changes: 1 addition & 4 deletions external_library/java/hemesh/slicer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
java_import 'wblut.processing.WB_Render3D'
java_import 'wblut.math.WB_Ease'




boolean ORTHOROT
boolean ORTHO

Expand Down Expand Up @@ -329,7 +326,7 @@ def getMesh(f, int counter)
transform=WB_Transform.new
do
if (p.parentToChild.nil?)
fracf=constrain((float)(p.level-f), 0.0, 1.0)
fracf=(p.level-f).clamp(0.0, 1.0)
if (counter<numFrames/2)
fracf=1.0-fracf
end
Expand Down
2 changes: 1 addition & 1 deletion processing_app/basics/image/colored_extrusion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def draw
else
@sval -= 0.01
end
@sval = constrain @sval, 1.0, 2.5
@sval = @sval.clamp(1.0, 2.5)
translate width / 2 + @nmx * @sval - 100, height / 2 + @nmy * @sval - 200, -50
scale @sval
rotate_z PI / 9 - @sval + 1
Expand Down
14 changes: 7 additions & 7 deletions processing_app/basics/image/sprite.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# Sprite (Teddy)
# by James Patterson.
#
# Demonstrates loading and displaying a transparent GIF image.
# by James Patterson.
#
# Demonstrates loading and displaying a transparent GIF image.

def setup
sketch_title 'Sprite'
@teddy = load_image(data_path('teddy.gif'))
@xpos, @ypos = width / 2, height / 2
@drag = 30.0
@drag = 30.0
frame_rate 60
end

def draw
background 102
background 102
difx = mouse_x - @xpos - @teddy.width / 2
if difx.abs > 1.0
@xpos += difx / @drag
@xpos = constrain(@xpos, 0, width - @teddy.width / 2)
@xpos = @xpos.clamp(0, width - @teddy.width / 2)
end
dify = mouse_y - @ypos - @teddy.height / 2
if dify.abs > 1.0
@ypos += dify/@drag
@ypos = constrain(@ypos, 0, height - @teddy.height / 2)
@ypos = @ypos.clamp(0, height - @teddy.height / 2)
end
image @teddy, @xpos, @ypos
end
Expand Down
4 changes: 2 additions & 2 deletions processing_app/basics/input/constrain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def draw
@mx += (mouse_x - @mx) * @easing if (mouse_x - @mx).abs > 0.1
@my += (mouse_y - @my) * @easing if (mouse_y - @my).abs > 0.1
distance = @ellipse_size * 2
@mx = constrain @mx, @inner, (width - @inner)
@my = constrain @my, @inner, (height - @inner)
@mx = @mx.clamp(@inner, width - @inner)
@my = @my.clamp(@inner, height - @inner)
fill 76
rect @edge, @edge, width - @edge, height - @edge
fill 255
Expand Down
5 changes: 2 additions & 3 deletions processing_app/basics/modules/euler_ball.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ def bounds_collision
end
end

# Convenient boundary class, we only include MathTool module for constrain
# Convenient boundary class
class Bounds
include Processing::MathTool
attr_reader :low, :high, :inside
def initialize(lower:, upper:)
@low = lower
Expand All @@ -51,6 +50,6 @@ def initialize(lower:, upper:)
# Returns the current position or the limit, sets the `inside` flag
def position(val)
@inside = (low..high).cover? val
constrain(val, low, high)
val.clamp(low, high)
end
end
2 changes: 1 addition & 1 deletion processing_app/basics/objects/shapes/circle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def jiggle
super
# The Circle jiggles both size and location.
@r += rand(-1..1.0)
@r = constrain(r, 0, 100)
@r = r.clamp(0, 100)
end

def display
Expand Down
2 changes: 1 addition & 1 deletion processing_app/basics/objects/shapes/circle2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def jiggle
super
# The Circle jiggles both size and location.
@r += rand(-1..1.0)
@r = constrain(r, 0, 100)
@r = r.clamp(0, 100)
end

# The change_color function is unique to the Circle class.
Expand Down
6 changes: 1 addition & 5 deletions processing_app/library/sound/noise/noise_spectrum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ def setup

def draw
# Only play one of the four noises, based on mouseY
next_noise = constrain(
map1d(mouseY, 0..height, 0..noises.length),
0,
noises.length - 1
)
next_noise = constrained_map(mouseY, 0..height, 0..noises.length) )
unless next_noise == current
noises[current].stop
@current = next_noise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ def setup

def draw
# Only play one of the four oscillators, based on mouseY
next_oscillator = constrain(
map1d(mouseY, 0..height, 0..count),
0,
count - 1
)
next_oscillator = constrained_map(mouseY, 0..height, 0..count)
unless next_oscillator == current
oscillators[current].stop
@current = next_oscillator
Expand Down
4 changes: 2 additions & 2 deletions processing_app/library/vecmath/vec2d/circles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def update
end

def check_edges # could be more sophisticated, eg reverse velocity
pos.x = constrain(pos.x, 0, width)
pos.y = constrain(pos.y, 0, height)
pos.x = pos.x.clamp(0, width)
pos.y = pos.y.clamp(0, height)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def render

def bounds_collision
if x_bound.exclude? pos.x
pos_old.x = constrain pos.x, x_bound.lower, x_bound.upper
pos_old.x = pos.x.clamp(x_bound.lower, x_bound.upper)
pos.x = (pos.x <= radius)? pos_old.x + push.x : pos_old.x - push.x
end
return unless y_bound.exclude? pos.y
pos_old.y = constrain pos.y, y_bound.lower, y_bound.upper
pos_old.y = pos.y.clamp(y_bound.lower, y_bound.upper)
pos.y = (pos.y <= radius)? pos_old.y + push.y : pos_old.y - push.y
end
end
Expand Down
40 changes: 20 additions & 20 deletions processing_app/library/vecmath/vec2d/seeking_neural.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@ def initialize(n, c)
@weights = Array.new(n) { rand(0..1.0) }
@c = c
end

# Function to train the Perceptron
# Weights are adjusted based on vehicle's error
def train(forces, error)
trained = @weights.zip(forces.map { |f| f.to_a }
.map { |a, b| (a * error.x + b * error.y) * @c })
.map { |w, c| constrain(w + c, 0, 1.0) }
.map { |w, c| (w + c).clamp(0, 1.0) }
@weights = trained
end

# Give me a steering result
def feedforward(forces)
# Sum all values
forces.zip(@weights).map { |a, b| a * b }.reduce(Vec2D.new, :+)
# forces.zip(@weights).map { |a, b| a * b }.reduce(:+)
end
end
end
# Seek
# Daniel Shiffman <http://www.shiffman.net>

class Vehicle
MAX_SPEED = 4
MAX_FORCE = 0.1

attr_reader :brain, :sz, :location, :targets, :desired

def initialize(n, x, y)
@brain = Perceptron.new(n, 0.001)
@acceleration = Vec2D.new
@velocity = Vec2D.new
@location = Vec2D.new(x, y)
@sz = 6.0
end

# Method to update location
def update(width, height)
# Update velocity
Expand All @@ -52,15 +52,15 @@ def update(width, height)
@location += @velocity
# Reset acceleration to 0 each cycle
@acceleration *= 0
@location.x = constrain(location.x, 0, width)
@location.y = constrain(location.y, 0, height)
@location.x = location.x.clamp(0, width)
@location.y = location.y.clamp(0, height)
end

def apply_force(force)
# We could add mass here if we want A = F / M
@acceleration += force
end

# Here is where the brain processes everything
def steer(targets, desired)
# Steer towards all targets
Expand All @@ -73,7 +73,7 @@ def steer(targets, desired)
error = desired - location
brain.train(forces, error)
end

# A method that calculates a steering force towards a target
# STEER = DESIRED MINUS VELOCITY
def seek(target)
Expand All @@ -86,8 +86,8 @@ def seek(target)
steer.set_mag(MAX_FORCE) { steer.mag > MAX_FORCE } # Limit to a maximum steering force
steer
end
def display

def display
# Draw a triangle rotated in the direction of velocity
theta = @velocity.heading + PI / 2
fill(175)
Expand All @@ -103,7 +103,7 @@ def display
end_shape(CLOSE)
pop_matrix
end
end
end
end

include SeekingNeural
Expand All @@ -114,9 +114,9 @@ def display
def setup
sketch_title 'Seeking Neural'
# The Vehicle's desired location
@desired = Vec2D.new(width / 2, height / 2)
@desired = Vec2D.new(width / 2, height / 2)
# Create a list of targets
make_targets
make_targets
# Create the Vehicle (it has to know about the number of targets
# in order to configure its brain)
@v = Vehicle.new(targets.size, rand(width), rand(height))
Expand All @@ -128,12 +128,12 @@ def make_targets
end

def draw
background(255)
background(255)
# Draw a circle to show the Vehicle's goal
stroke(0)
stroke_weight(2)
fill(0, 100)
ellipse(desired.x, desired.y, 36, 36)
ellipse(desired.x, desired.y, 36, 36)
# Draw the targets
targets.each do |target|
no_fill
Expand All @@ -142,7 +142,7 @@ def draw
ellipse(target.x, target.y, 16, 16)
line(target.x, target.y - 16, target.x, target.y + 16)
line(target.x - 16, target.y, target.x + 16, target.y)
end
end
# Update the Vehicle
v.steer(targets, desired)
v.update(width, height)
Expand Down
4 changes: 2 additions & 2 deletions processing_app/topics/advanced_data/counting_words.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(word)
@word = word
@count = 1
end

def increment
@count += 1
end
Expand Down Expand Up @@ -55,7 +55,7 @@ def draw
# Only display words that appear 3 times
if w.count > 3 # access word count
# The size is the count
fsize = constrain(w.count, 0, 100)
fsize = w.count.clamp(0, 100)
text_size(fsize)
text(w.word, x, y)
# Move along the x-axis
Expand Down
4 changes: 2 additions & 2 deletions processing_app/topics/advanced_data/library/word/word.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def increment_franken
# The more often it appears, the faster it falls
def move
@speed = map1d(total_count, (5..25), (0.1..0.4))
@speed = constrain(speed, 0, 10.0)
@speed = speed.clamp(0, 10.0)
@position[Y] += speed
@position[Y] = -height if position[Y] > height * 2
end
Expand All @@ -53,7 +53,7 @@ def display
end
# Its size is also tied to number of occurences
fs = map1d(total_count, (5..25), (2..24.0))
fs = constrain(fs, 2, 48)
fs = fs.clamp(2, 48)
text_size(fs)
text_align(CENTER)
text(word, position[X], position[Y])
Expand Down
4 changes: 2 additions & 2 deletions processing_app/topics/cellular_automata/game_of_life.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def draw
if pause && mouse_pressed?
# # Map and avoid out of bound errors
over_x = (map1d(mouse_x, (0..width), (0..row))).to_i
over_x = constrain(over_x, 0, row - 1)
over_x = over_x.clamp(0, row - 1)
over_y = (map1d(mouse_y, (0..height), (0..column))).to_i
over_y = constrain(over_y, 0, column - 1)
over_y = over_y.clamp(0, column - 1)
# Check against cells in buffer
if cells_buffer[over_x][over_y] # Cell is alive
cells[over_x][over_y] = DEAD # Kill
Expand Down
2 changes: 1 addition & 1 deletion processing_app/topics/gui/handles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def update
press_event
end
return unless press
@stretch = constrain(mouse_x - width / 2 - size / 2, 0, width / 2 - size - 1)
@stretch = (mouse_x - width / 2 - size / 2).clamp(0, width / 2 - size - 1)
end

def over_event
Expand Down
2 changes: 1 addition & 1 deletion processing_app/topics/gui/scrollbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def update
else
@locked = app.mouse_pressed? && over_event?
end
@newspos = constrain(mouse_x - wd / 2, spos_min, spos_max) if locked
@newspos = (mouse_x - wd / 2).clamp(spos_min, spos_max) if locked
@spos = spos + (newspos - spos) / loose if (newspos - spos).abs > 1
end

Expand Down
Loading

0 comments on commit 15714cd

Please sign in to comment.