Only just found @jeremydouglass rosetta code examples, it would be nice exercise for people to have a go at translating for JRubyArt. I had a go at the Animation example:-
# Animation
# https://rosettacode.org/wiki/Animation#Processing
# Processing 3.4
# 2019-08 Jeremy Douglass
# 2020-02 translated to JRubyArt Martin Prout
# Task:
#
# Create a window containing the string "Hello World! " (the trailing space
# is significant). Make the text appear to be rotating right by periodically
# removing one txt from the end of the string and attaching it to the front.
# When the user clicks on the (windowed) text, it should reverse its direction.
attr_reader :txt, :dir
def setup
sketch_title 'Animation'
@txt = 'Hello, world! '
@dir = true
end
def settings
size(120, 100)
end
def draw
background(128)
text(txt, 10, height / 2)
return unless (frame_count % 10).zero?
txt_array = txt.split(//)
txt_array = dir ? txt_array.rotate(1) : txt_array.rotate(txt.length - 1)
@txt = txt_array.join('')
puts(txt)
end
def mouse_released
@dir = !dir
end
As always there is often more ways to do it in ruby, but this is broadly similar to vanilla processing and somewhat different from Shoes
version.
2 Likes
A more bare-bones version based on ruby string index (like Shoes version), and relying on default width / height values:-
attr_reader :txt, :dir
def setup
@txt = 'Hello, world! '
@dir = true
end
def draw
background(128)
text(txt, 10, height / 2)
return unless (frame_count % 10).zero?
@txt = dir ? txt[-1] + txt[0..-2] : txt[1..-1] + txt[0]
puts(txt)
end
def mouse_released
@dir = !dir
end
2 Likes
These are great, @monkstone.
If we were to begin collecting JRubyArt examples somewhere, where (ideally) would you like to see them live? A separate github repo?
Another opti0n, they could actually be in a “ruby” path or branch of the Java example set github as well, of course – they wouldn’t load ini PDE because it doesn’t have a JRubyArt mode, but they could either be left out of the contributions manager release or just included in a way that the Java example set would ignore them…
I’m actually not sure whether PDE Contributions manager Examples can handle parallel examples for multiple PDE modes – so, the same issue for e.g. python mode instead of ruby.
1 Like
I hadn’t actually given it much thought, I’ve got an examples repository already. But something like @shiffman Nature of Code examples would be cool. If it were simply a language thing I guess Rosetta Code has it covered except for gui and say Vectors, where they are already implemented.
# The four operations to be implemented are:
#
# Vector + Vector addition
# Vector - Vector subtraction
# Vector * scalar multiplication
# Vector / scalar division
# A vector class, Vec2D, is a JRubyArt built-in. It expresses an x, y
# vector from the origin. A vector may return its components,
# magnitude, and heading, and also includes '+', '-', '*', and
# '/' -- among other methods.
def setup
vec1 = Vec2D.new(5, 7)
vec2 = Vec2D.new(2, 3)
puts(vec1.x, vec1.y, vec1.mag, vec1.heading)
puts(vec1 + vec2)
puts(vec1 - vec2)
puts(vec1 * 11)
puts(vec1 / 2)
puts(vec1.inspect)
# Output:
# 5.0
# 7.0
# 8.602325267042627
# 0.9505468408120751
# Vec2D(x = 7.0000, y = 10.0000)
# Vec2D(x = 3.0000, y = 4.0000)
# Vec2D(x = 55.0000, y = 77.0000)
# Vec2D(x = 2.5000, y = 3.5000)
no_loop
end
1 Like
I have added this one to Rosetta Code for ruby (previously unsolved)
INCR = 0.1
attr_reader :x, :theta
def setup
sketch_title 'Archimedian Spiral'
@theta = 0
@x = 0
background(255)
translate(width / 2.0, height / 2.0)
begin_shape
while x < width / 2
@x = theta * cos(theta / PI)
curve_vertex(x, theta * sin(theta / PI))
@theta += INCR
end
end_shape
end
def settings
size(300, 300)
end
3 Likes