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 )
2
4
3
5
def frame_box (w , h , d , thick = 0 ):
4
6
""" draw the 3D version of the box with rectangular holes """
5
7
mw , mh , md = w / 2. , h / 2. , d / 2.
6
- translate (0 , 0 , - md ) # base
8
+ py5 . translate (0 , 0 , - md ) # base
7
9
face (0 , 0 , w , h , thick )
8
- translate (0 , 0 , d ) # top
10
+ py5 . translate (0 , 0 , d ) # top
9
11
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
13
15
face (0 , 0 , d , h , thick )
14
- translate (0 , 0 , w ) # right side
16
+ py5 . translate (0 , 0 , w ) # right side
15
17
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
20
22
face (0 , 0 , w , d , thick )
21
- translate (0 , 0 , h ) # lateral d
23
+ py5 . translate (0 , 0 , h ) # lateral d
22
24
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
+
25
28
26
29
def face (x , y , w , h , thick ):
27
30
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 )
35
38
if thick > 0 and mw - thick > 0 and mh - thick > 0 :
36
39
mw -= thick
37
40
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
+
46
50
47
51
def unfolded_frame_box (w , h , d , thick = 0 , draw_main = True ):
48
52
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):
53
57
unfolded_face (- mw - md , - mh , d , h , "acna" , thick , draw_main )
54
58
unfolded_face (mw + md , - mh , d , h , "ncaa" , thick , draw_main )
55
59
60
+
56
61
def unfolded_face (x , y , w , h , edge_types , thick = 0 , draw_main = True ):
57
62
e0 , e1 , e2 , e3 = edge_types
58
63
mw , mh = w / 2. , h / 2.
59
- pushMatrix ()
60
- translate (x , y )
64
+ py5 . push_matrix ()
65
+ py5 . translate (x , y )
61
66
if draw_main :
62
67
edge (- mw , + mh , - mw , - mh , e0 )
63
68
edge (- mw , - mh , + mw , - mh , e1 )
64
69
edge (+ mw , - mh , + mw , + mh , e2 )
65
70
edge (+ mw , + mh , - mw , + mh , e3 )
66
71
if thick > 0 and mw - thick > 0 and mh - thick > 0 :
67
72
unfolded_face (0 , 0 , w - thick * 2 , h - thick * 2 , "cccc" )
68
- popMatrix ()
73
+ py5 .pop_matrix ()
74
+
69
75
70
76
def edge (x0 , y0 , x1 , y1 , edge_type ):
71
77
if edge_type == "n" : # no edge is drawn
72
78
return
73
79
elif edge_type == "c" : # cut stroke selected
74
- stroke (CUT_STROKE )
80
+ py5 . stroke (CUT_STROKE )
75
81
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
78
84
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 ()
81
87
glue_tab ((x0 , y0 ), (x1 , y1 ), 10 )
82
88
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 ):
84
91
"""
85
92
draws a trapezoidal or triangular glue tab along edge defined by p1 and p2,
86
93
with width tab_w and cut angle a
87
94
"""
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
90
97
a2 = al - cut_ang
91
98
# 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 ])
98
105
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 ()
106
113
else : # short triangular tab
107
114
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