Skip to content

Commit 44113ad

Browse files
committed
py5 conversion
1 parent 0b8868e commit 44113ad

File tree

9 files changed

+292
-266
lines changed

9 files changed

+292
-266
lines changed

box_with_circular_holes/box_with_circular_holes.pyde renamed to box_with_circular_holes/box_with_circular_holes.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
from frame_box import frame_box, unfolded_frame_box
1010

11+
DIMENSION_KEYS = (('a', 'd'),
12+
('w', 's'),
13+
(LEFT, RIGHT),
14+
(UP, DOWN))
1115
dimensions = [250, 150, 100, 30]
1216
modes = [-1, 0, 1] # click mouse to switch modes
1317

@@ -21,29 +25,25 @@ def draw():
2125
if modes[0] >= 0:
2226
fill(255)
2327
stroke(0)
24-
pushMatrix()
28+
push_matrix()
2529
translate(0, 0, 200)
26-
rotateX(HALF_PI / 2)
30+
rotate_x(HALF_PI / 2)
2731
frame_box(w, h, d, thick)
28-
popMatrix()
32+
pop_matrix()
2933
if modes[0] <= 0:
3034
unfolded_frame_box(w, h, d, thick)
3135

32-
def mousePressed():
36+
def mouse_pressed():
3337
modes[:] = modes[1:] + [modes[0]]
3438

35-
def keyPressed():
39+
def key_pressed():
3640
if key == 'p':
37-
saveFrame("a###.png")
41+
save_frame("a###.png")
3842
if key == ' ':
3943
dimensions[:] = [250, 150, 100, 30]
4044

41-
DIMENSION_KEYS = (('a', 'd', key),
42-
('w', 's', key),
43-
(LEFT, RIGHT, keyCode),
44-
(UP, DOWN, keyCode))
45-
46-
for i, (plus, minus, k) in enumerate(DIMENSION_KEYS):
45+
k = key_code if key == CODED else key
46+
for i, (plus, minus) in enumerate(DIMENSION_KEYS):
4747
if k == plus:
4848
dimensions[i] += 1
4949
elif k == minus:

box_with_circular_holes/frame_box.py

+73-65
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
1-
CUT_STROKE, FOLD_STROKE = color(255, 0, 0), color(0, 0, 255)
1+
import py5
2+
3+
CUT_STROKE, FOLD_STROKE = py5.color(255, 0, 0), py5.color(0, 0, 255)
24

35
def frame_box(w, h, d, thick=0):
46
""" draw the 3D version of the box with rectangular holes """
57
mw, mh, md = w / 2., h / 2., d / 2.
6-
translate(0, 0, -md) # base
8+
py5.translate(0, 0, -md) # base
79
face(0, 0, w, h, thick)
8-
translate(0, 0, d) # top
10+
py5.translate(0, 0, d) # top
911
face(0, 0, w, h, thick)
10-
translate(0, 0, -md) # back to 0
11-
rotateY(HALF_PI)
12-
translate(0, 0, -mw) # left side
12+
py5.translate(0, 0, -md) # back to 0
13+
py5.rotate_y(py5.HALF_PI)
14+
py5.translate(0, 0, -mw) # left side
1315
face(0, 0, d, h, thick)
14-
translate(0, 0, w) # right side
16+
py5.translate(0, 0, w) # right side
1517
face(0, 0, d, h, thick)
16-
translate(0, 0, -mw) # back to middle
17-
rotateY(-HALF_PI) # back to 0 rotation
18-
rotateX(HALF_PI)
19-
translate(0, 0, -mh) # lateral e
18+
py5.translate(0, 0, -mw) # back to middle
19+
py5.rotate_y(-py5.HALF_PI) # back to 0 rotation
20+
py5.rotate_x(py5.HALF_PI)
21+
py5.translate(0, 0, -mh) # lateral e
2022
face(0, 0, w, d, thick)
21-
translate(0, 0, h) # lateral d
23+
py5.translate(0, 0, h) # lateral d
2224
face(0, 0, w, d, thick)
23-
translate(0, 0, -mw) # reset translate
24-
rotateX(-HALF_PI) # reset rotate
25+
py5.translate(0, 0, -mw) # reset translate
26+
py5.rotate_x(-py5.HALF_PI) # reset rotate
27+
2528

2629
def face(x, y, w, h, e):
2730
mw, mh = w / 2., h / 2.
28-
pushMatrix()
29-
translate(x, y)
30-
beginShape()
31-
vertex(-mw, -mh)
32-
vertex(+mw, -mh)
33-
vertex(+mw, +mh)
34-
vertex(-mw, +mh)
31+
py5.push_matrix()
32+
py5.translate(x, y)
33+
py5.begin_shape()
34+
py5.vertex(-mw, -mh)
35+
py5.vertex(+mw, -mh)
36+
py5.vertex(+mw, +mh)
37+
py5.vertex(-mw, +mh)
3538
hole(mw, mh, e)
36-
endShape(CLOSE)
37-
popMatrix()
39+
py5.end_shape(py5.CLOSE)
40+
py5.pop_matrix()
41+
3842

3943
def hole(mw, mh, e):
40-
if e > 0 and mw - e > 0 and mh - e > 0:
41-
beginContour()
42-
np = 24
43-
for i in range(np):
44-
ang = TWO_PI / np * i
45-
x = sin(ang) * e
46-
y = cos(ang) * e
47-
vertex(x, y)
48-
endContour()
49-
44+
if e > 0 and mw - e > 0 and mh - e > 0:
45+
py5.begin_contour()
46+
np = 24
47+
for i in range(np):
48+
ang = py5.TWO_PI / np * i
49+
x = py5.sin(ang) * e
50+
y = py5.cos(ang) * e
51+
py5.vertex(x, y)
52+
py5.end_contour()
5053

5154

5255
def unfolded_frame_box(w, h, d, thick=0, draw_main=True):
@@ -58,61 +61,66 @@ def unfolded_frame_box(w, h, d, thick=0, draw_main=True):
5861
unfolded_face(-mw - md, -mh, d, h, "acna", thick, draw_main)
5962
unfolded_face(mw + md, -mh, d, h, "ncaa", thick, draw_main)
6063

64+
6165
def unfolded_face(x, y, w, h, edge_types, thick=0, draw_main=True):
6266
e0, e1, e2, e3 = edge_types
6367
mw, mh = w / 2., h / 2.
64-
pushMatrix()
65-
translate(x, y)
68+
py5.push_matrix()
69+
py5.translate(x, y)
6670
if draw_main:
6771
edge(-mw, +mh, -mw, -mh, e0)
6872
edge(-mw, -mh, +mw, -mh, e1)
6973
edge(+mw, -mh, +mw, +mh, e2)
7074
edge(+mw, +mh, -mw, +mh, e3)
7175
if thick > 0 and mw - thick > 0 and mh - thick > 0:
72-
stroke(CUT_STROKE)
73-
circle(0, 0, thick * 2)
74-
popMatrix()
76+
py5.stroke(CUT_STROKE)
77+
py5.circle(0, 0, thick * 2)
78+
py5.pop_matrix()
79+
7580

7681
def edge(x0, y0, x1, y1, edge_type):
7782
if edge_type == "n": # no edge is drawn
7883
return
7984
elif edge_type == "c": # cut stroke selected
80-
stroke(CUT_STROKE)
85+
py5.stroke(CUT_STROKE)
8186
else:
82-
stroke(FOLD_STROKE) # fold stroke selected for "v" and "a"
83-
line(x0, y0, x1, y1) # line drawn here
87+
py5.stroke(FOLD_STROKE) # fold stroke selected for "v" and "a"
88+
py5.line(x0, y0, x1, y1) # line drawn here
8489
if edge_type == "a": # tab (note a fold-stroke line was already drawn)
85-
stroke(CUT_STROKE)
86-
noFill()
90+
py5.stroke(CUT_STROKE)
91+
py5.no_fill()
8792
glue_tab((x0, y0), (x1, y1), 10)
8893

89-
def glue_tab(p1, p2, tab_w, cut_ang=QUARTER_PI / 3):
94+
95+
def glue_tab(p1, p2, tab_w, cut_ang=py5.QUARTER_PI / 3):
9096
"""
9197
draws a trapezoidal or triangular glue tab along edge defined by p1 and p2,
9298
with width tab_w and cut angle a
9399
"""
94-
al = atan2(p1[0] - p2[0], p1[1] - p2[1])
95-
a1 = al + cut_ang + PI
100+
al = py5.atan2(p1[0] - p2[0], p1[1] - p2[1])
101+
a1 = al + cut_ang + py5.PI
96102
a2 = al - cut_ang
97103
# calculate cut_len to get the right tab width
98-
cut_len = tab_w / sin(cut_ang)
99-
f1 = (p1[0] + cut_len * sin(a1),
100-
p1[1] + cut_len * cos(a1))
101-
f2 = (p2[0] + cut_len * sin(a2),
102-
p2[1] + cut_len * cos(a2))
103-
edge_len = dist(p1[0], p1[1], p2[0], p2[1])
104-
105-
if edge_len > 2 * cut_len * cos(cut_ang): # 'normal' trapezoidal tab
106-
beginShape()
107-
vertex(*p1) # vertex(p1[0], p1[1])
108-
vertex(*f1)
109-
vertex(*f2)
110-
vertex(*p2)
111-
endShape()
104+
cut_len = tab_w / py5.sin(cut_ang)
105+
f1 = (p1[0] + cut_len * py5.sin(a1),
106+
p1[1] + cut_len * py5.cos(a1))
107+
f2 = (p2[0] + cut_len * py5.sin(a2),
108+
p2[1] + cut_len * py5.cos(a2))
109+
edge_len = py5.dist(p1[0], p1[1], p2[0], p2[1])
110+
111+
if edge_len > 2 * cut_len * py5.cos(cut_ang): # 'normal' trapezoidal tab
112+
py5.begin_shape()
113+
py5.vertex(*p1) # vertex(p1[0], p1[1])
114+
py5.vertex(*f1)
115+
py5.vertex(*f2)
116+
py5.vertex(*p2)
117+
py5.end_shape()
112118
else: # short triangular tab
113119
fm = ((f1[0] + f2[0]) / 2, (f1[1] + f2[1]) / 2)
114-
beginShape()
115-
vertex(*p1)
116-
vertex(*fm) # middle way of f1 and f2
117-
vertex(*p2)
118-
endShape()
120+
py5.begin_shape()
121+
py5.vertex(*p1)
122+
py5.vertex(*fm) # middle way of f1 and f2
123+
py5.vertex(*p2)
124+
py5.end_shape()
125+
126+

box_with_rectangular_holes/box_with_rectangular_holes.pyde renamed to box_with_rectangular_holes/box_with_rectangular_holes.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
from frame_box import frame_box, unfolded_frame_box
88

9-
dimensions = [250, 150, 100, 30]
9+
DIMENSION_KEYS = (('a', 'd'),
10+
('w', 's'),
11+
(LEFT, RIGHT),
12+
(UP, DOWN))
1013
modes = [-1, 0, 1] # click mouse to switch modes
14+
dimensions = [250, 150, 100, 30] # initial dimensions, a list to be mutated
1115

1216
def setup():
1317
size(600, 600, P3D)
@@ -36,12 +40,8 @@ def keyPressed():
3640
if key == ' ':
3741
dimensions[:] = [250, 150, 100, 30]
3842

39-
DIMENSION_KEYS = (('a', 'd', key),
40-
('w', 's', key),
41-
(LEFT, RIGHT, keyCode),
42-
(UP, DOWN, keyCode))
43-
44-
for i, (plus, minus, k) in enumerate(DIMENSION_KEYS):
43+
k = key_code if key == CODED else key
44+
for i, (plus, minus) in enumerate(DIMENSION_KEYS):
4545
if k == plus:
4646
dimensions[i] += 1
4747
elif k == minus:

extruded_simple_poly/extruded_simple_poly.pyde renamed to extruded_simple_poly/extruded_simple_poly.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def draw():
2525
fill(255)
2626
stroke(0)
2727
f.draw_3D(-QUARTER_PI)
28-
noFill()
28+
no_fill()
2929
stroke(255, 0, 0)
3030
translate(200, 0)
3131
f.draw_2D()

0 commit comments

Comments
 (0)