Here is vanilla sketch that wasn't easy to translate to ruby, the more hacks there are, the harder it is to translate. However conversely it should make me a bit bit more sensitive to the same issue in reverse. The original sketch featured sincos look up tables which are a dubious benefit in java, especially when not optimised. It also featured a nasty ++ increment hack. The original sketch had quite lot of casting to int, I found there no casting required in ruby-processing, just a nifty built in deg to radian conversion using a patched Numeric class.
load_library 'pdf'
include_package 'processing.pdf'
attr_reader :num, :pt, :style, :dosave
def setup
size(1024, 768, P3D)
background(255)
@dosave = false
@num = 150
@pt = []
@style = []
index = 0
(0 ... num).each do |i|
pt.push(rand(TWO_PI))
pt.push(rand(TWO_PI))
pt.push(rand(60 .. 80))
if (rand(100)>90)
pt[pt.length - 1] = rand(8 .. 27) * 10
end
pt.push(rand(2 .. 50) * 5)
pt.push(rand(4 .. 32))
if (rand(100) > 90)
pt[pt.length - 1] = rand(40 .. 60)
end
pt.push(rand(0.005 .. 0.0334))
prob = rand(100)
if (prob < 30)
style[i*2] = color_blended(rand, 255,0,100, 255,0,0, 210)
elsif(prob < 70)
style[i*2] = color_blended(rand, 0,153,255, 170,225,255, 210)
elsif(prob<90)
style[i*2] = color_blended(rand, 200,255,0, 150,255,0, 210)
else
style[i*2] = color(255,255,255, 220)
end
if (prob < 50)
style[i*2] = color_blended(rand, 200,255,0, 50,120,0, 210)
elsif(prob < 90)
style[i*2] = color_blended(rand, 255,100,0, 255,255,0, 210)
else
style[i*2] = color(255, 255, 255, 220)
style[i*2+1] = rand(100) % 3
end
end
end
def draw
if(dosave)
pdf = begin_raw(PDF, "pdf_complex_out.pdf")
pdf.stroke_join(MITER)
pdf.stroke_cap(SQUARE)
pdf.fill(0)
pdf.no_stroke
pdf.rect(0,0, width,height)
end
background(0)
index = 0
translate(width/2, height/2, 0)
rotate_x(PI/6)
rotate_y(PI/6)
(0 ... num).each do |i|
push_matrix
rotate_x(pt[index])
rotate_y(pt[index + 1])
index += 2
if (style[i*2+1] == 0)
stroke(style[i*2])
no_fill
stroke_weight(1)
arc_line(0,0, pt[index],pt[index + 1],pt[index + 2])
index += 3
elsif (style[i*2+1] == 1)
fill(style[i*2])
no_stroke
arc_line_bars(0,0, pt[index],pt[index + 1],pt[index + 2])
index += 3
else
fill(style[i*2])
no_stroke
arc(0,0, pt[index],pt[index + 1],pt[index + 2])
index += 3
end
pt[index-5] += pt[index] / 10
pt[index-4] += pt[index] / 20
index += 1
pop_matrix
end
if (dosave)
end_raw
@dosave=false
end
end
def color_blended(fract, r, g, b, r2, g2, b2, a)
r2 = (r2 - r)
g2 = (g2 - g)
b2 = (b2 - b)
return color(r + r2 * fract, g + g2 * fract, b + b2 * fract, a)
end
def arc_line(x, y, deg, rad, w)
a=(deg < 360)? deg : 0
numlines = w/2
(0 ... numlines).each do
begin_shape
(0 ... a).each do |i|
vertex(cos(i.radians)*rad+x,sin(i.radians)*rad+y)
end
end_shape
rad += 2
end
end
def arc_line_bars(x, y, deg, rad, w)
a=(deg < 360)? deg / 4 : 0
begin_shape(QUADS)
(0 ... a).step(4) do |i|
vertex(cos(i.radians)*(rad)+x,sin(i.radians)*(rad)+y)
vertex(cos(i.radians)*(rad+w)+x,sin(i.radians)*(rad+w)+y)
vertex(cos((i + 2).radians)*(rad+w)+x,sin((i + 2).radians)*(rad+w)+y)
vertex(cos((i + 2).radians)*(rad)+x,sin((i + 2).radians)*(rad)+y)
end
end_shape
end
def arc(x,y,deg,rad,w)
a = (deg < 360)? deg : 0
begin_shape(QUAD_STRIP)
(0 ... a).each do |i|
vertex(cos(i.radians)*(rad)+x,sin(i.radians)*(rad)+y)
vertex(cos(i.radians)*(rad+w)+x,sin(i.radians)*(rad+w)+y)
end
end_shape
end
def mouse_pressed
@dosave = true
end
load_library 'pdf'
include_package 'processing.pdf'
attr_reader :num, :pt, :style, :dosave
def setup
size(1024, 768, P3D)
background(255)
@dosave = false
@num = 150
@pt = []
@style = []
index = 0
(0 ... num).each do |i|
pt.push(rand(TWO_PI))
pt.push(rand(TWO_PI))
pt.push(rand(60 .. 80))
if (rand(100)>90)
pt[pt.length - 1] = rand(8 .. 27) * 10
end
pt.push(rand(2 .. 50) * 5)
pt.push(rand(4 .. 32))
if (rand(100) > 90)
pt[pt.length - 1] = rand(40 .. 60)
end
pt.push(radians(rand(5 .. 30)) / 5 )
prob = rand(100)
if (prob < 30)
style[i*2] = color_blended(rand, 255,0,100, 255,0,0, 210)
elsif(prob < 70)
style[i*2] = color_blended(rand, 0,153,255, 170,225,255, 210)
elsif(prob<90)
style[i*2] = color_blended(rand, 200,255,0, 150,255,0, 210)
else
style[i*2] = color(255,255,255, 220)
end
if (prob < 50)
style[i*2] = color_blended(rand, 200,255,0, 50,120,0, 210)
elsif(prob < 90)
style[i*2] = color_blended(rand, 255,100,0, 255,255,0, 210)
else
style[i*2] = color(255, 255, 255, 220)
style[i*2+1] = rand(100).to_i % 3
end
end
end
def draw
if(dosave)
pdf = begin_raw(PDF, "pdf_complex_out.pdf")
pdf.stroke_join(MITER)
pdf.stroke_cap(SQUARE)
pdf.fill(0)
pdf.no_stroke
pdf.rect(0,0, width,height)
end
background(0)
index = 0
translate(width/2, height/2, 0)
rotate_x(PI/6)
rotate_y(PI/6)
(0 ... num).each do |i|
push_matrix
rotate_x(pt[index])
rotate_y(pt[index + 1])
index += 2
if (style[i*2+1] == 0)
stroke(style[i*2])
no_fill
stroke_weight(1)
arc_line(0,0, pt[index],pt[index + 1],pt[index + 2])
index += 3
elsif (style[i*2+1] == 1)
fill(style[i*2])
no_stroke
arc_line_bars(0,0, pt[index],pt[index + 1],pt[index + 2])
index += 3
else
fill(style[i*2])
no_stroke
arc(0,0, pt[index],pt[index + 1],pt[index + 2])
index += 3
end
pt[index-5] += pt[index] / 10
pt[index-4] += pt[index] / 20
index += 1
pop_matrix
end
if (dosave)
end_raw
@dosave=false
end
end
def color_blended(fract, r, g, b, r2, g2, b2, a)
r2 = (r2 - r)
g2 = (g2 - g)
b2 = (b2 - b)
return color(r + r2 * fract, g + g2 * fract, b + b2 * fract, a)
end
def arc_line(x, y, deg, rad, w)
a=(deg < 360)? deg : 0
numlines = w/2
(0 ... numlines).each do
begin_shape
(0 ... a).each do |i|
vertex(cos(radians(i))*rad+x,sin(radians(i))*rad+y)
end
end_shape
rad += 2
end
end
def arc_line_bars(x, y, deg, rad, w)
a=(deg < 360)? deg / 4 : 0
begin_shape(QUADS)
(0 ... a).step(4) do |i|
vertex(cos(radians(i))*(rad)+x,sin(radians(i))*(rad)+y)
vertex(cos(radians(i))*(rad+w)+x,sin(radians(i))*(rad+w)+y)
vertex(cos(radians(i + 2))*(rad+w)+x,sin(radians(i + 2))*(rad+w)+y)
vertex(cos(radians(i + 2))*(rad)+x,sin(radians(i + 2))*(rad)+y)
end
end_shape
end
def arc(x,y,deg,rad,w)
a = (deg < 360)? deg : 0
begin_shape(QUAD_STRIP)
(0 ... a).each do |i|
vertex(cos(radians(i))*(rad)+x,sin(radians(i))*(rad)+y)
vertex(cos(radians(i))*(rad+w)+x,sin(radians(i))*(rad+w)+y)
end
end_shape
end
def mouse_pressed
@dosave = true
end
No comments:
Post a Comment