Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1a86dcb

Browse files
committed
py5 conversion
1 parent 13d9da8 commit 1a86dcb

File tree

2 files changed

+80
-70
lines changed

2 files changed

+80
-70
lines changed

‎box_with_rectangular_holes/box_with_rectangular_holes.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,29 @@ def draw():
2323
if modes[0] >= 0:
2424
fill(255)
2525
stroke(0)
26-
pushMatrix()
26+
push_matrix()
2727
translate(0, 0, 200)
28-
rotateX(HALF_PI / 2)
28+
rotate_x(HALF_PI / 2)
2929
frame_box(w, h, d, thick)
30-
popMatrix()
30+
pop_matrix()
31+
3132
if modes[0] <= 0:
3233
unfolded_frame_box(w, h, d, thick)
3334

34-
def mousePressed():
35+
if is_key_pressed:
36+
k = key_code if key == CODED else key
37+
for i, (plus, minus) in enumerate(DIMENSION_KEYS):
38+
if k == plus:
39+
dimensions[i] += 1
40+
elif k == minus:
41+
dimensions[i] -= 1
42+
43+
def mouse_pressed():
3544
modes[:] = modes[1:] + [modes[0]]
3645

37-
def keyPressed():
46+
def key_pressed():
3847
if key == 's':
39-
saveFrame("a###.png")
48+
save_frame("a###.png")
4049
if key == ' ':
4150
dimensions[:] = [250, 150, 100, 30]
4251

43-
k = key_code if key == CODED else key
44-
for i, (plus, minus) in enumerate(DIMENSION_KEYS):
45-
if k == plus:
46-
dimensions[i] += 1
47-
elif k == minus:
48-
dimensions[i] -= 1

‎box_with_rectangular_holes/frame_box.py

Lines changed: 65 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,52 @@
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, thick):
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
if thick > 0 and mw - thick > 0 and mh - thick > 0:
3639
mw -= thick
3740
mh -= thick
38-
beginContour() # counterclockwise hole
39-
vertex(-mw, -mh)
40-
vertex(-mw, +mh)
41-
vertex(+mw, +mh)
42-
vertex(+mw, -mh)
43-
endContour()
44-
endShape(CLOSE)
45-
popMatrix()
41+
py5.begin_contour() # counterclockwise hole
42+
py5.vertex(-mw, -mh)
43+
py5.vertex(-mw, +mh)
44+
py5.vertex(+mw, +mh)
45+
py5.vertex(+mw, -mh)
46+
py5.end_contour()
47+
py5.end_shape(py5.CLOSE)
48+
py5.pop_matrix()
49+
4650

4751
def unfolded_frame_box(w, h, d, thick=0, draw_main=True):
4852
mw, mh, md = w / 2., h / 2., d / 2.
@@ -53,60 +57,63 @@ def unfolded_frame_box(w, h, d, thick=0, draw_main=True):
5357
unfolded_face(-mw - md, -mh, d, h, "acna", thick, draw_main)
5458
unfolded_face(mw + md, -mh, d, h, "ncaa", thick, draw_main)
5559

60+
5661
def unfolded_face(x, y, w, h, edge_types, thick=0, draw_main=True):
5762
e0, e1, e2, e3 = edge_types
5863
mw, mh = w / 2., h / 2.
59-
pushMatrix()
60-
translate(x, y)
64+
py5.push_matrix()
65+
py5.translate(x, y)
6166
if draw_main:
6267
edge(-mw, +mh, -mw, -mh, e0)
6368
edge(-mw, -mh, +mw, -mh, e1)
6469
edge(+mw, -mh, +mw, +mh, e2)
6570
edge(+mw, +mh, -mw, +mh, e3)
6671
if thick > 0 and mw - thick > 0 and mh - thick > 0:
6772
unfolded_face(0, 0, w - thick * 2, h - thick * 2, "cccc")
68-
popMatrix()
73+
py5.pop_matrix()
74+
6975

7076
def edge(x0, y0, x1, y1, edge_type):
7177
if edge_type == "n": # no edge is drawn
7278
return
7379
elif edge_type == "c": # cut stroke selected
74-
stroke(CUT_STROKE)
80+
py5.stroke(CUT_STROKE)
7581
else:
76-
stroke(FOLD_STROKE) # fold stroke selected for "v" and "a"
77-
line(x0, y0, x1, y1) # line drawn here
82+
py5.stroke(FOLD_STROKE) # fold stroke selected for "v" and "a"
83+
py5.line(x0, y0, x1, y1) # line drawn here
7884
if edge_type == "a": # tab (note a fold-stroke line was already drawn)
79-
stroke(CUT_STROKE)
80-
noFill()
85+
py5.stroke(CUT_STROKE)
86+
py5.no_fill()
8187
glue_tab((x0, y0), (x1, y1), 10)
8288

83-
def glue_tab(p1, p2, tab_w, cut_ang=QUARTER_PI / 3):
89+
90+
def glue_tab(p1, p2, tab_w, cut_ang=py5.QUARTER_PI / 3):
8491
"""
8592
draws a trapezoidal or triangular glue tab along edge defined by p1 and p2,
8693
with width tab_w and cut angle a
8794
"""
88-
al = atan2(p1[0] - p2[0], p1[1] - p2[1])
89-
a1 = al + cut_ang + PI
95+
al = py5.atan2(p1[0] - p2[0], p1[1] - p2[1])
96+
a1 = al + cut_ang + py5.PI
9097
a2 = al - cut_ang
9198
# calculate cut_len to get the right tab width
92-
cut_len = tab_w / sin(cut_ang)
93-
f1 = (p1[0] + cut_len * sin(a1),
94-
p1[1] + cut_len * cos(a1))
95-
f2 = (p2[0] + cut_len * sin(a2),
96-
p2[1] + cut_len * cos(a2))
97-
edge_len = dist(p1[0], p1[1], p2[0], p2[1])
99+
cut_len = tab_w / py5.sin(cut_ang)
100+
f1 = (p1[0] + cut_len * py5.sin(a1),
101+
p1[1] + cut_len * py5.cos(a1))
102+
f2 = (p2[0] + cut_len * py5.sin(a2),
103+
p2[1] + cut_len * py5.cos(a2))
104+
edge_len = py5.dist(p1[0], p1[1], p2[0], p2[1])
98105

99-
if edge_len > 2 * cut_len * cos(cut_ang): # 'normal' trapezoidal tab
100-
beginShape()
101-
vertex(*p1) # vertex(p1[0], p1[1])
102-
vertex(*f1)
103-
vertex(*f2)
104-
vertex(*p2)
105-
endShape()
106+
if edge_len > 2 * cut_len * py5.cos(cut_ang): # 'normal' trapezoidal tab
107+
py5.begin_shape()
108+
py5.vertex(*p1) # vertex(p1[0], p1[1])
109+
py5.vertex(*f1)
110+
py5.vertex(*f2)
111+
py5.vertex(*p2)
112+
py5.end_shape()
106113
else: # short triangular tab
107114
fm = ((f1[0] + f2[0]) / 2, (f1[1] + f2[1]) / 2)
108-
beginShape()
109-
vertex(*p1)
110-
vertex(*fm) # middle way of f1 and f2
111-
vertex(*p2)
112-
endShape()
115+
py5.begin_shape()
116+
py5.vertex(*p1)
117+
py5.vertex(*fm) # middle way of f1 and f2
118+
py5.vertex(*p2)
119+
py5.end_shape()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /