Repository navigation
Expand file tree
/
Copy pathtruchet_fig_19.scad
More file actions
165 lines (135 loc) · 4.45 KB
/
Copy pathtruchet_fig_19.scad
File metadata and controls
165 lines (135 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Tiling Patterns of Truchet, Fig 19 */
/* [Seed] */
_Seed = 13;
/* [Square Size] */
_SquareSize = 10;
/* [Square Height] */
_SquareHeight = 2.0;
/* [CircleMode] */
_CircleMode = "Circle"; // [Circle, Ring]
/* Circle Height] */
_CircleHeight = 0.4;
/* [Count X] */
_SquareCountX = 10;
/* [Count Y] */
_SquareCountY = 10;
/* [Gap] */
_SquareGap = 0.5;
/* [Ring Width] */
_RingWidth = 1.2;
/* [Extruders] */
// [Square Extruder]
_SquareExtruder = 1;
// [Circle Extruder]
_CircleExtruder = 2;
// [Extruder to render]
_WhichExtruder = "All"; // ["All", 1, 2, 3, 4, 5]
// Map a value of Extruder to an OpenSCAD color
function ExtruderColor(Extruder) =
(Extruder == 1 ) ? "red" :
(Extruder == 2 ) ? "green" :
(Extruder == 3 ) ? "blue" :
(Extruder == 4 ) ? "pink" :
(Extruder == 5 ) ? "yellow" :
"purple" ;
// If _WhichExtruder is "All" or is not "All" and matches the requested extruder, render
// the child nodes.
module Extruder(DoExtruder)
{
color(ExtruderColor(DoExtruder))
{
if (_WhichExtruder == "All" || DoExtruder == _WhichExtruder)
{
children();
}
}
}
module RenderCircleOrRing(CircleMode, SquareSize, CircleHeight, RingWidth)
{
if (CircleMode == "Circle")
{
circle(SquareSize / 2, $fn=99);
}
if (CircleMode == "Ring")
{
difference()
{
// Outer ring
circle(SquareSize / 2 + RingWidth / 2, $fn=99);
// Inner ring
circle(SquareSize / 2 - RingWidth / 2, $fn=99);
}
}
}
module RenderSquare(SquareExtruder, CircleExtruder, SquareSize, SquareHeight, CircleMode, CircleHeight, RingWidth)
{
union()
{
Extruder(SquareExtruder)
{
cube([SquareSize, SquareSize, SquareHeight], center=false);
}
translate([0, 0, SquareHeight])
{
intersection()
{
// Clip circle or rings at edges of square
cube([SquareSize, SquareSize, 99], center=false);
Extruder(CircleExtruder)
{
linear_extrude(CircleHeight)
{
union()
{
RenderCircleOrRing(CircleMode, SquareSize, CircleHeight, RingWidth);
translate([SquareSize, SquareSize, 0])
{
RenderCircleOrRing(CircleMode, SquareSize, CircleHeight, RingWidth);
}
}
}
}
}
}
}
}
module RenderRotatedSquare(SquareExtruder, CircleExtruder, SquareSize, SquareHeight, CircleMode, CircleHeight, RingWidth, Rotated)
{
if (Rotated)
{
translate([SquareSize / 2, SquareSize / 2, 0])
{
rotate([0, 0, 90])
{
translate([-SquareSize / 2, -SquareSize / 2, 0])
{
RenderSquare(SquareExtruder, CircleExtruder, SquareSize, SquareHeight, CircleMode, CircleHeight, RingWidth);
}
}
}
}
else
{
RenderSquare(SquareExtruder, CircleExtruder, SquareSize, SquareHeight, CircleMode, CircleHeight, RingWidth);
}
}
module main(Seed, SquareCountX, SquareCountY, SquareExtruder, CircleExtruder, SquareSize, CircleMode, SquareHeight, CircleHeight, RingWidth, SquareGap)
{
// Generate list of random numbers to set which squares are rendered vertically
// and which ones are renderer horizontally:
Verticals = rands(0, 1.001, SquareCountX * SquareCountY, Seed);
for (X = [0 : SquareCountX - 1])
{
for (Y = [0 : SquareCountY - 1])
{
PointX = X * (SquareSize + SquareGap);
PointY = Y * (SquareSize + SquareGap);
translate([PointX, PointY, 0])
{
Vertical = (Verticals[SquareCountY * X + Y] < 0.5) ? true : false;
RenderRotatedSquare(SquareExtruder, CircleExtruder, SquareSize, SquareHeight, CircleMode, CircleHeight, RingWidth, Vertical);
}
}
}
}
main(_Seed, _SquareCountX, _SquareCountY, _SquareExtruder, _CircleExtruder, _SquareSize, _CircleMode, _SquareHeight, _CircleHeight, _RingWidth, _SquareGap);