Skip to content

Commit 4ff6195

Browse files
committed
pdf library example
1 parent 01e4cd6 commit 4ff6195

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

data_path/complex_3D.rb

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#!/usr/bin/env jruby -v -W2
2+
#
3+
# Geometry
4+
# by Marius Watz.
5+
#
6+
7+
require 'propane'
8+
9+
class Complex3D < Propane::App
10+
load_library :pdf
11+
include_package 'processing.pdf'
12+
attr_reader :num, :pt, :style, :dosave
13+
14+
def setup
15+
sketch_title 'Complex 3 D'
16+
background(255)
17+
@dosave = false
18+
@num = 150
19+
@pt = []
20+
@style = []
21+
# Set up arc shapes
22+
index = 0
23+
(0...num).each do |i|
24+
pt << rand(TAU) # Random X axis rotation
25+
pt << rand(TAU) # Random Y axis rotation
26+
pt << rand(60..80) # Short to quarter-circle arcs
27+
pt[pt.length - 1] = rand(8..27) * 10 if rand(100) > 90
28+
pt << rand(2..50) * 5 # Radius. Space them out nicely
29+
pt << rand(4..32) # Width of band
30+
pt[pt.length - 1] = rand(40..60) if (rand(100) > 90) # Width of band
31+
pt << rand(0.005..0.0334)# Speed of rotation
32+
# get colors
33+
prob = rand(100)
34+
case prob
35+
when (0..30)
36+
style[i * 2] = color_blended(rand, 255,0,100, 255,0,0, 210)
37+
when (30..70)
38+
style[i * 2] = color_blended(rand, 0,153,255, 170,225,255, 210)
39+
when (70..90)
40+
style[i * 2] = color_blended(rand, 200,255,0, 150,255,0, 210)
41+
else
42+
style[i * 2] = color(255,255,255, 220)
43+
end
44+
case prob
45+
when (0..50)
46+
style[i * 2] = color_blended(rand, 200,255,0, 50,120,0, 210)
47+
when (50..90)
48+
style[i * 2] = color_blended(rand, 255,100,0, 255,255,0, 210)
49+
else
50+
style[i * 2] = color(255, 255, 255, 220)
51+
style[i * 2 + 1] = rand(100) % 3
52+
end
53+
end
54+
end
55+
56+
def draw
57+
if dosave
58+
# set up PGraphicsPDF for use with beginRaw
59+
pdf = begin_raw(PDF, data_path('pdf_complex_out.pdf'))
60+
# set default Illustrator stroke styles and paint background rect.
61+
pdf.stroke_join(MITER)
62+
pdf.stroke_cap(SQUARE)
63+
pdf.fill(0)
64+
pdf.no_stroke
65+
pdf.rect(0, 0, width, height)
66+
end
67+
background(0)
68+
index = 0
69+
translate(width / 2, height / 2, 0)
70+
rotate_x(PI / 6)
71+
rotate_y(PI / 6)
72+
(0 ... num).each do |i|
73+
push_matrix
74+
rotate_x(pt[index])
75+
rotate_y(pt[index + 1])
76+
index += 2
77+
case (style[i * 2 + 1])
78+
when 0
79+
stroke(style[i * 2])
80+
no_fill
81+
stroke_weight(1)
82+
arc_line(0, 0, pt[index], pt[index + 1], pt[index + 2])
83+
index += 3
84+
when 1
85+
fill(style[i * 2])
86+
no_stroke
87+
arc_line_bars(0, 0, pt[index], pt[index + 1],pt[index + 2])
88+
index += 3
89+
else
90+
fill(style[i * 2])
91+
no_stroke
92+
arc(0, 0, pt[index], pt[index + 1], pt[index + 2])
93+
index += 3
94+
end
95+
# increase rotation
96+
pt[index - 5] += pt[index] / 10
97+
pt[index - 4] += pt[index] / 20
98+
index += 1
99+
pop_matrix
100+
end
101+
if dosave
102+
end_raw
103+
@dosave=false
104+
end
105+
end
106+
107+
# Get blend of two colors
108+
def color_blended(fract, r, g, b, r2, g2, b2, a)
109+
r2 = (r2 - r)
110+
g2 = (g2 - g)
111+
b2 = (b2 - b)
112+
color(r + r2 * fract, g + g2 * fract, b + b2 * fract, a)
113+
end
114+
115+
# Draw arc line
116+
def arc_line(x, y, deg, rad, w)
117+
a = (deg < 360)? deg : 0
118+
numlines = w / 2
119+
(0...numlines).each do
120+
begin_shape
121+
(0...a).each do |i|
122+
vertex(DegLut.cos(i) * rad + x, DegLut.sin(i) * rad + y)
123+
end
124+
end_shape
125+
rad += 2
126+
end
127+
end
128+
129+
# Draw arc line with bars
130+
def arc_line_bars(x, y, deg, rad, w)
131+
a=(deg < 360)? deg / 16 : 0
132+
begin_shape(QUADS)
133+
(0...a).step(4) do |i|
134+
vertex(DegLut.cos(i) * rad + x,DegLut.sin(i) * (rad) + y)
135+
vertex(DegLut.cos(i) * (rad + w) + x,DegLut.sin(i) * (rad + w) + y)
136+
vertex(DegLut.cos((i + 2)) * (rad + w) + x, DegLut.sin((i + 2)) * (rad + w) + y)
137+
vertex(DegLut.cos((i + 2)) * rad + x, DegLut.sin((i + 2)) * rad + y)
138+
end
139+
end_shape
140+
end
141+
142+
# Draw solid arc
143+
def arc(x, y, deg, rad, w)
144+
a = (deg < 360)? deg : 0
145+
begin_shape(QUAD_STRIP)
146+
(0...a).each do |i|
147+
vertex(DegLut.cos(i) * rad + x, DegLut.sin(i) * rad + y)
148+
vertex(DegLut.cos(i) * (rad + w) + x, DegLut.sin(i) * (rad + w) + y)
149+
end
150+
end_shape
151+
end
152+
153+
def mouse_pressed
154+
@dosave = true
155+
end
156+
157+
def settings
158+
size(1024, 768, P3D)
159+
end
160+
end
161+
162+
Complex3D.new

0 commit comments

Comments
 (0)