Skip to content

Commit d5228d6

Browse files
author
monkstone
committed
start checking examples for java idioms
1 parent d724337 commit d5228d6

8 files changed

Lines changed: 132 additions & 103 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Translated Sketches
2+
array.rb
3+
### More rubyified version
4+
ruby_array.rb
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
1-
# * An array is a list of data. Each piece of data in an array
2-
# * is identified by an index number representing its position in
3-
# * the array. Arrays are zero based, which means that the first
4-
# * element in the array is [0], the second element is [1], and so on.
5-
# * In this example, an array named "coswav" is created and
6-
# * filled with the cosine values. This data is displayed three
7-
# * separate ways on the screen.
8-
9-
1+
# An array is a list of data. Each piece of data in an array
2+
# is identified by an index number representing its position in
3+
# the array. Arrays are zero based, which means that the first
4+
# element in the array is [0], the second element is [1], and so on.
5+
# In this example, an array named "coswav" is created and
6+
# filled with the cosine values. This data is displayed three
7+
# separate ways on the screen.
108

119
def setup
1210
size 640, 360
1311
coswave = []
14-
1512
0.upto(width) do |i|
1613
amount = map i, 0, width, 0, PI
1714
coswave[i] = cos(amount).abs
1815
end
19-
2016
0.upto(width) do |i|
2117
stroke(coswave[i] * 255)
2218
line(i, 0, i, height / 3)
2319
end
24-
2520
0.upto(width) do |i|
2621
stroke(coswave[i] * 255 / 4)
2722
line(i, height / 3, i, height / 3 * 2)
2823
end
29-
3024
0.upto(width) do |i|
3125
stroke(255 - coswave[i] * 255)
3226
line(i, height / 3 * 2, i, height)
3327
end
34-
3528
end
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
# * Demonstrates the syntax for creating a two-dimensional (2D) array.
2-
# * Values in a 2D array are accessed through two index values.
3-
# * 2D arrays are useful for storing images. In this example, each dot
4-
# * is colored in relation to its distance from the center of the image.
1+
# Demonstrates the syntax for creating a two-dimensional (2D) array.
2+
# Values in a 2D array are accessed through two index values.
3+
# 2D arrays are useful for storing images. In this example, each dot
4+
# is colored in relation to its distance from the center of the image.
55

66
def setup
77
size 640, 360
8-
distances = Array.new( width ) { Array.new( height ) } # [width][height]
9-
stroke_weight 2
10-
max_distance = dist( width/2, height/2, width, height )
11-
8+
distances = Array.new(width) { Array.new(height) } # [width][height]
9+
stroke_weight 2
10+
max_distance = dist(width / 2, height / 2, width, height)
1211
width.times do |x|
1312
height.times do |y|
14-
distance = dist( width/2, height/2, x, y )
13+
distance = dist(width / 2, height / 2, x, y)
1514
distances[x][y] = distance / max_distance * 255
1615
end
1716
end
18-
19-
background 0
17+
background 0
2018
x = 0
2119
while x < distances.length
22-
y = 0
20+
y = 0
2321
while y < distances[x].length
2422
stroke distances[x][y]
2523
point x, y
@@ -28,5 +26,3 @@ def setup
2826
x += 2
2927
end
3028
end
31-
32-
Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# * Demonstrates the syntax for creating an array of custom objects.
2-
3-
1+
# Demonstrates the syntax for creating an array of custom objects.
42

53
UNIT = 40
64
attr_reader :mods
@@ -9,54 +7,49 @@ def setup
97
size 640, 360
108
wide_count = width / UNIT
119
height_count = height / UNIT
12-
@mods = []
10+
@mods = []
1311
wide_count.times do |i|
1412
height_count.times do |j|
15-
mods << CustomObject.new(j * UNIT, i * UNIT, UNIT/2, UNIT/2, rand(0.05..0.8))
13+
mods << CustomObject.new(j * UNIT, i * UNIT, UNIT / 2, UNIT / 2, rand(0.05..0.8))
1614
end
17-
end
15+
end
1816
no_stroke
1917
end
2018

2119
def draw
22-
background 0
20+
background 0
2321
mods.each do |mod|
2422
mod.update
2523
mod.draw
2624
end
2725
end
2826

2927
# the custom object
30-
3128
class CustomObject
3229
include Processing::Proxy
33-
attr_reader :x, :y, :mx, :my, :size
30+
attr_reader :x, :y, :xdir, :ydir, :mx, :my, :size, :speed
3431
def initialize(mx, my, x, y, speed)
35-
@mx, @my = my, mx # This is backwards because the Processing example is backwards.
32+
@mx, @my = my, mx # because the Processing example is backwards.
3633
@x, @y = x.to_i, y.to_i
3734
@xdir, @ydir = 1, 1
3835
@speed = speed
3936
@size = UNIT
4037
end
41-
38+
4239
def update
43-
@x += @speed * @xdir
44-
unless (0..size).cover? x
40+
@x += speed * xdir
41+
unless (0..size).cover? x
4542
@xdir *= -1
46-
@x += @xdir
47-
@y += @ydir
48-
end
49-
if @y >= @size || x <= 0
50-
@ydir *= -1
51-
@y += @ydir
43+
@x += xdir
44+
@y += ydir
5245
end
46+
return unless y >= size || x <= 0
47+
@ydir *= -1
48+
@y += ydir
5349
end
54-
50+
5551
def draw
5652
fill(255)
5753
ellipse(mx + x, my + y, 6, 6)
5854
end
59-
6055
end
61-
62-
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# An array is a list of data. Each piece of data in an array
2+
# In ruby it is quite usual to initialize an array with a map, here we
3+
# also use ruby-processing map1d (in place of vanilla processing map)
4+
# Note how much more compact ruby-processing version is...
5+
6+
def setup
7+
size 640, 360
8+
coswave = (0..width).map { |i| cos(map1d(i, (0..width), (0..PI))).abs }
9+
coswave.each_with_index do |value, i|
10+
stroke(value * 255)
11+
line(i, 0, i, height / 3)
12+
stroke(value * 255 / 4)
13+
line(i, height / 3, i, height / 3 * 2)
14+
stroke(255 - value * 255)
15+
line(i, height / 3 * 2, i, height)
16+
end
17+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Demonstrates the syntax for creating a two-dimensional (2D) array.
2+
# Values in a 2D array are accessed through two index values.
3+
# 2D arrays are useful for storing images. In this example, each dot
4+
# is colored in relation to its distance from the center of the image.
5+
6+
def setup
7+
size 640, 360
8+
distances = Array.new(width) { Array.new(height) } # [width][height]
9+
stroke_weight 2
10+
max_distance = dist(width / 2, height / 2, width, height)
11+
width.times do |x|
12+
height.times do |y|
13+
distance = dist(width / 2, height / 2, x, y)
14+
distances[x][y] = distance / max_distance * 255
15+
end
16+
end
17+
background 0
18+
row = []
19+
(0...width).step(2) do |x|
20+
row = distances[x]
21+
(0...height).step(2) do |y|
22+
stroke row[y]
23+
point x, y
24+
end
25+
end
26+
end
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
require 'forwardable'
12
load_libraries :icosahedron, :vecmath
23

34
def setup
45
size 640, 360, P3D
5-
@ico1 = Icosahedron.new 75.0
6-
@ico2 = Icosahedron.new 75.0
7-
@ico3 = Icosahedron.new 75.0
6+
@ico1 = Icosahedron.new self, 75.0
7+
@ico2 = Icosahedron.new self, 75.0
8+
@ico3 = Icosahedron.new self, 75.0
89
end
910

1011
def draw
1112
background 0
1213
lights
13-
translate width/2, height/2
14+
translate(width / 2, height / 2)
1415
push_matrix
15-
translate -width/3.5, 0
16+
translate(-width / 3.5, 0)
1617
rotate_x frame_count * PI / 185
1718
rotate_y frame_count * PI / -200
1819
stroke 170, 0, 0
@@ -27,13 +28,11 @@ def draw
2728
@ico2.draw
2829
pop_matrix
2930
push_matrix
30-
translate width/3.5, 0
31+
translate(width / 3.5, 0)
3132
rotate_x frame_count * PI / -200
3233
rotate_y frame_count * PI / 200
3334
no_stroke
3435
fill 0, 0, 185
3536
@ico3.draw
3637
pop_matrix
3738
end
38-
39-
Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,61 @@
1+
# Example of replacing Processing::Proxy with extend Forwardable
2+
# And using Vec3D :to_vertex function
13
class Icosahedron
2-
include Processing::Proxy # mixin Processing::Proxy
3-
attr_reader :r
4-
5-
def initialize(radius)
4+
extend Forwardable # replacing Processing::Proxy mixin
5+
def_delegators(:@app, :vertex, :begin_shape, :end_shape)
6+
attr_reader :r, :app, :renderer
7+
8+
def initialize(app, radius)
69
@r = radius
10+
@app = app
11+
@renderer = AppRender.new(app)
712
end
8-
13+
914
##
1015
# Draw an icosahedron defined by a radius r.
1116
#
1217
def draw
13-
# Calculate the vertex data for an icosahedron inscribed by a sphere radius 'r'.
18+
# Calculate vertex data for an icosahedron inscribed by a sphere radius 'r'.
1419
# Use 4 Golden Ratio rectangles as the basis.
1520
phi = (1.0 + Math.sqrt(5.0)) / 2.0
1621
h = r / Math.sqrt(1.0 + phi * phi)
1722
v =
18-
[
19-
Vec3D.new(0, -h, h * phi), Vec3D.new(0, -h, -h * phi), Vec3D.new(0, h, -h * phi), Vec3D.new(0, h, h * phi),
20-
Vec3D.new(h, -h * phi, 0), Vec3D.new(h, h * phi, 0), Vec3D.new(-h, h * phi, 0), Vec3D.new(-h, -h * phi, 0),
21-
Vec3D.new(-h * phi, 0, h), Vec3D.new(-h * phi, 0, -h), Vec3D.new(h * phi, 0, -h), Vec3D.new(h * phi, 0, h)
23+
[
24+
Vec3D.new(0, -h, h * phi), Vec3D.new(0, -h, -h * phi),
25+
Vec3D.new(0, h, -h * phi), Vec3D.new(0, h, h * phi),
26+
Vec3D.new(h, -h * phi, 0), Vec3D.new(h, h * phi, 0),
27+
Vec3D.new(-h, h * phi, 0), Vec3D.new(-h, -h * phi, 0),
28+
Vec3D.new(-h * phi, 0, h), Vec3D.new(-h * phi, 0, -h),
29+
Vec3D.new(h * phi, 0, -h), Vec3D.new(h * phi, 0, h)
2230
]
23-
24-
begin_shape(TRIANGLES)
25-
26-
draw_triangle(v[0], v[7],v[4])
27-
draw_triangle(v[0], v[4], v[11])
28-
draw_triangle(v[0], v[11], v[3])
29-
draw_triangle(v[0], v[3], v[8])
30-
draw_triangle(v[0], v[8], v[7])
31-
32-
draw_triangle(v[1], v[4], v[7])
33-
draw_triangle(v[1], v[10], v[4])
34-
draw_triangle(v[10], v[11], v[4])
35-
draw_triangle(v[11], v[5], v[10])
36-
draw_triangle(v[5], v[3], v[11])
37-
draw_triangle(v[3], v[6], v[5])
38-
draw_triangle(v[6], v[8], v[3])
39-
draw_triangle(v[8], v[9], v[6])
40-
draw_triangle(v[9], v[7], v[8])
41-
draw_triangle(v[7], v[1], v[9])
42-
43-
draw_triangle(v[2], v[1], v[9])
44-
draw_triangle(v[2], v[10], v[1])
45-
draw_triangle(v[2], v[5], v[10])
46-
draw_triangle(v[2], v[6], v[5])
47-
draw_triangle(v[2], v[9], v[6])
48-
31+
32+
begin_shape(Java::ProcessingCore::PConstants::TRIANGLES)
33+
draw_triangle(v[0], v[7], v[4])
34+
draw_triangle(v[0], v[4], v[11])
35+
draw_triangle(v[0], v[11], v[3])
36+
draw_triangle(v[0], v[3], v[8])
37+
draw_triangle(v[0], v[8], v[7])
38+
draw_triangle(v[1], v[4], v[7])
39+
draw_triangle(v[1], v[10], v[4])
40+
draw_triangle(v[10], v[11], v[4])
41+
draw_triangle(v[11], v[5], v[10])
42+
draw_triangle(v[5], v[3], v[11])
43+
draw_triangle(v[3], v[6], v[5])
44+
draw_triangle(v[6], v[8], v[3])
45+
draw_triangle(v[8], v[9], v[6])
46+
draw_triangle(v[9], v[7], v[8])
47+
draw_triangle(v[7], v[1], v[9])
48+
draw_triangle(v[2], v[1], v[9])
49+
draw_triangle(v[2], v[10], v[1])
50+
draw_triangle(v[2], v[5], v[10])
51+
draw_triangle(v[2], v[6], v[5])
52+
draw_triangle(v[2], v[9], v[6])
4953
end_shape
5054
end
51-
55+
5256
def draw_triangle(p1, p2, p3)
53-
54-
vertex(p1.x, p1.y, p1.z)
55-
vertex(p2.x, p2.y, p2.z)
56-
vertex(p3.x, p3.y, p3.z)
57-
57+
p1.to_vertex(renderer)
58+
p2.to_vertex(renderer)
59+
p3.to_vertex(renderer)
5860
end
59-
60-
end
61+
end

0 commit comments

Comments
 (0)