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 , e ):
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
hole (mw , mh , e )
36
- endShape (CLOSE )
37
- popMatrix ()
39
+ py5 .end_shape (py5 .CLOSE )
40
+ py5 .pop_matrix ()
41
+
38
42
39
43
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 ()
50
53
51
54
52
55
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):
58
61
unfolded_face (- mw - md , - mh , d , h , "acna" , thick , draw_main )
59
62
unfolded_face (mw + md , - mh , d , h , "ncaa" , thick , draw_main )
60
63
64
+
61
65
def unfolded_face (x , y , w , h , edge_types , thick = 0 , draw_main = True ):
62
66
e0 , e1 , e2 , e3 = edge_types
63
67
mw , mh = w / 2. , h / 2.
64
- pushMatrix ()
65
- translate (x , y )
68
+ py5 . push_matrix ()
69
+ py5 . translate (x , y )
66
70
if draw_main :
67
71
edge (- mw , + mh , - mw , - mh , e0 )
68
72
edge (- mw , - mh , + mw , - mh , e1 )
69
73
edge (+ mw , - mh , + mw , + mh , e2 )
70
74
edge (+ mw , + mh , - mw , + mh , e3 )
71
75
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
+
75
80
76
81
def edge (x0 , y0 , x1 , y1 , edge_type ):
77
82
if edge_type == "n" : # no edge is drawn
78
83
return
79
84
elif edge_type == "c" : # cut stroke selected
80
- stroke (CUT_STROKE )
85
+ py5 . stroke (CUT_STROKE )
81
86
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
84
89
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 ()
87
92
glue_tab ((x0 , y0 ), (x1 , y1 ), 10 )
88
93
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 ):
90
96
"""
91
97
draws a trapezoidal or triangular glue tab along edge defined by p1 and p2,
92
98
with width tab_w and cut angle a
93
99
"""
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
96
102
a2 = al - cut_ang
97
103
# 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 ()
112
118
else : # short triangular tab
113
119
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
+
0 commit comments