Skip to content

Commit 3586d82

Browse files
author
monkstone
committed
mod
1 parent 8c50e2c commit 3586d82

File tree

4 files changed

+48
-155
lines changed

4 files changed

+48
-155
lines changed

samples/processing_app/basics/input/constrain.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Move the mouse across the screen to move the circle.
22
# 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...
3+
# Uses processing constrain implemented as a jruby extension
4+
65
attr_reader :inner, :easing, :edge, :ellipse_size, :mx, :my
76

87
def setup
@@ -21,8 +20,8 @@ def draw
2120
background 51
2221
@mx += (mouse_x - mx) * easing if (mouse_x - mx).abs > 0.1
2322
@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
23+
@mx = constrain(mx, inner, width - inner)
24+
@my = constrain(my, inner, height - inner)
2625
fill 76
2726
rect edge, edge, width - edge, height - edge
2827
fill 255

samples/processing_app/basics/input/mouse_1d.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def setup
1212
def draw
1313
background 0
1414
x_dist_blocks = width / 2
15-
block_size = map1d(mouse_x, (0..width), (10..x_dist_blocks - 10))
15+
# block_size = map1d(mouse_x, (0..width), (10..x_dist_blocks - 10))
16+
block_size = map(mouse_x, 0, width, 10, x_dist_blocks - 10)
1617
left_color = -0.002 * mouse_x / 2 + 0.06
1718
fill 0.0, left_color + 0.4, left_color + 0.6
1819
rect(width / 4, height / 2, block_size * 2, block_size * 2)

samples/processing_app/topics/gui/scrollbar.rb

Lines changed: 0 additions & 103 deletions
This file was deleted.

samples/processing_app/topics/motion/bouncy_bubbles.rb

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
def setup
1616
size(640, 360)
1717
@balls = []
18-
(0 ... NUM_BALLS).each do |i|
18+
(0...NUM_BALLS).each do |i|
1919
balls << Ball.new(rand(width), rand(height), rand(30..70), i, balls)
2020
end
2121
noStroke
@@ -24,17 +24,17 @@ def setup
2424

2525
def draw
2626
background(0)
27-
(0 ... NUM_BALLS).each do |i|
27+
(0...NUM_BALLS).each do |i|
2828
balls[i].collide
29-
balls[i].move
29+
balls[i].move(width, height)
3030
balls[i].display
3131
end
3232
end
3333

3434
class Ball
3535
attr_accessor :vx, :vy
36-
attr_reader :x, :y, :diameter, :others, :id, :width, :height
37-
36+
attr_reader :x, :y, :diameter, :others, :id
37+
3838
def initialize(xin, yin, din, idin, oin)
3939
@x = xin
4040
@y = yin
@@ -43,54 +43,50 @@ def initialize(xin, yin, din, idin, oin)
4343
@vx = 0
4444
@vy = 0
4545
@others = oin
46-
@width = $app.width
47-
@height = $app.height
4846
end
49-
47+
5048
def collide
51-
((id + 1) ... NUM_BALLS).each do |i|
49+
((id + 1)...NUM_BALLS).each do |i|
5250
dx = others[i].x - x
5351
dy = others[i].y - y
54-
sq_dist = dx*dx + dy*dy
55-
min_dist = (others[i].diameter/2 + diameter/2)
56-
sq_min_dist = min_dist * min_dist
57-
if (sq_dist < sq_min_dist)
58-
angle = atan2(dy, dx)
59-
target_x = x + cos(angle) * min_dist
60-
target_y = y + sin(angle) * min_dist
61-
ax = (target_x- others[i].x) * SPRING
62-
ay = (target_y - others[i].y) * SPRING
63-
@vx -= ax
64-
@vy -= ay
65-
others[i].vx += ax
66-
others[i].vy += ay
67-
end
68-
52+
distance = dist(others[i].x, others[i].y, x, y)
53+
min_dist = (others[i].diameter / 2 + diameter / 2)
54+
next unless distance < min_dist
55+
angle = atan2(dy, dx)
56+
target_x = x + cos(angle) * min_dist
57+
target_y = y + sin(angle) * min_dist
58+
ax = (target_x- others[i].x) * SPRING
59+
ay = (target_y - others[i].y) * SPRING
60+
@vx -= ax
61+
@vy -= ay
62+
others[i].vx += ax
63+
others[i].vy += ay
6964
end
70-
71-
def move
72-
@vy += GRAVITY
73-
@x += vx
74-
@y += vy
75-
if (x + diameter/2 > width)
76-
@x = width - diameter/2
77-
@vx *= FRICTION
78-
elsif (x - diameter/2 < 0)
79-
@x = diameter/2
80-
@vx *= FRICTION
81-
end
82-
83-
if (y + diameter/2 > height)
84-
@y = height - diameter/2
85-
@vy *= FRICTION
86-
elsif (y - diameter/2 < 0)
87-
@y = diameter/2
88-
@vy *= FRICTION
89-
end
65+
end
66+
67+
def move(width, height)
68+
@vy += GRAVITY
69+
@x += vx
70+
@y += vy
71+
if x + diameter / 2 > width
72+
@x = width - diameter / 2
73+
@vx *= FRICTION
74+
elsif x - diameter / 2 < 0
75+
@x = diameter / 2
76+
@vx *= FRICTION
9077
end
9178

92-
def display
93-
ellipse(x, y, diameter, diameter)
79+
if y + diameter / 2 > height
80+
@y = height - diameter / 2
81+
@vy *= FRICTION
82+
elsif y - diameter / 2 < 0
83+
@y = diameter / 2
84+
@vy *= FRICTION
9485
end
9586
end
87+
88+
def display
89+
ellipse(x, y, diameter, diameter)
90+
end
9691
end
92+

0 commit comments

Comments
 (0)