-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolygon.scad
243 lines (205 loc) · 5.4 KB
/
Polygon.scad
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//============================================================
// OpenSCAD
// Regular Convex Polygon Library
// Last Modified : 2015/10/28
//============================================================
/*
Definition
N = Number of Side N >=3
A = apothem
radius of inside circle
R = circumradius
Radius of outside circle
S = Side
Lenght of a side
Build polygon with the Apothem :
N and A is known
Need to calculate S then R
use $fn
Build Polygon with Circumradius
N and R known
use $fn
Build Polygon with Side
N and S Known
Need to calculate R and optionaly A
use $fn
TO DO
Control result
For more information :
http://en.wikipedia.org/wiki/Regular_polygon
*/
//------------------------------------------------------------
//
//------------------------------------------------------------
Demo();
//------------------------------------------------------------
// Demo
//------------------------------------------------------------
module Demo()
{
inch = 25.4; // 25.4mm
// A = apothem - it's a RADIUS !
// For a bolt of 4 inches spécify A=2*inch
//Polygon(N=7,A=2*inch,h=0); // 4 inches radius Polygon
//Polygon(N=7,A=49.8365,h=0);
//Trigon(A=50,h=10); // N=3
//Dodecagon(A=50,h=0); // N=12
//rotate([0,0,360/7/2])
//Nonagon(R=55.3144,h=10,debug=true);
//Polygon(N=6,A=60,h=0,debug=false);
//Polygon(N=6,A=60,h=5,debug=true);
Enneagon(A=50);
}
//------------------------------------------------------------
// Polygon :
//------------------------------------------------------------
module Polygon(N=3,A=0,R=0,S=0,h=0,debug=false)
{
N = ( N < 3 ? 3 : N );
angle = 360/N;
angleA = angle/2;
if (debug)
{
echo("N = ", N);
echo("A = ", A);
echo("S = ", S);
echo("R = ", R);
}
if ( A > 0 ) // if A assign R and S
{
S = 2 * A * tan(angleA); // assign
//R = (S/2) / sin(angleA); // no assign ???
R = ( A * tan(angleA) ) / sin(angleA); // asign
_Build_Polygon(N=N,R=R,h=h);
if (debug)
{
echo("aN = ", N);
echo("aA = ", A);
echo("aS = ", S);
echo("aR = ", R);
#cylinder(r=A,h=h+1,center=true,$fn=150);
%cylinder(r=R,h=h+1,center=true,$fn=150);
}
}
else
{
if ( S > 0 ) // if S assign R and A
{
R = (S/2) / sin(angleA); // assign
A = S / ( 2 * tan(angleA) ); // assign
_Build_Polygon(N=N,R=R,h=h);
if (debug)
{
echo("sN = ", N);
echo("sA = ", A);
echo("sS = ", S);
echo("sR = ", R);
#cylinder(r=A,h=h+1,center=true,$fn=150);
%cylinder(r=R,h=h+1,center=true,$fn=150);
}
}
else
{
if ( R == 0 ) // default
{
S = 2 * R * sin(angleA); // no assign ???
A = S / ( 2 * tan(angleA) ); // no assign ???
_Build_Polygon(N=N,h=h);
if (debug)
{
echo("0N = ", N);
echo("0A = ", A);
echo("0S = ", S);
echo("0R = ", R);
#cylinder(r=A,h=h+1,center=true,$fn=150);
%cylinder(r=R,h=h+1,center=true,$fn=150);
}
}
else // build with R
{
S = 2 * R * sin(angleA);
A = S / ( 2 * tan(angleA) ); // no assign ???
//A = R * ( sin(angleA) / tan(angleA) ) // no assign ???
_Build_Polygon(N=N,R=R,h=h);
if (debug)
{
echo("rN = ", N);
echo("rA = ", A);
echo("rS = ", S);
echo("rR = ", R);
%cylinder(r=R,h=h+1,center=true,$fn=150);
}
}
}
}
if (debug)
{
echo("fN = ", N);
echo("fA = ", A);
echo("fS = ", S);
echo("fR = ", R);
}
}
//------------------------------------------------------------
// Build
//------------------------------------------------------------
module _Build_Polygon(N=3,R=1,h=0)
{
if (h > 0)
{
// 3D primitive h>0
cylinder(r=R,h=h,$fn=N,center=true);
}
else
{
// 2D primitive h=0
circle(r=R,$fn=N,center=true);
}
}
//------------------------------------------------------------
// Building lots from N=3, N=N+1
// http://en.wikipedia.org/wiki/Polygon
//------------------------------------------------------------
// 3 sides
module Trigon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=3,A=A,R=R,S=S,h=h,debug=debug);}
module Triangle(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=3,A=A,R=R,S=S,h=h,debug=debug);}
// 4 sides
module Tetragon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=4,A=A,R=R,S=S,h=h,debug=debug);}
module Quadrilateral(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=4,A=A,R=R,S=S,h=h,debug=debug);}
module Quadrangle(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=4,A=A,R=R,S=S,h=h,debug=debug);}
// 5 sides
module Pentagon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=5,A=A,R=R,S=S,h=h,debug=debug);}
// 6 sides
module Hexagon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=6,A=A,R=R,S=S,h=h,debug=debug);}
// 7 sides
module Heptagon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=7,A=A,R=R,S=S,h=h,debug=debug);}
// 8 sides
module Octagon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=8,A=A,R=R,S=S,h=h,debug=debug);}
// 9 sides
module Enneagon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=9,A=A,R=R,S=S,h=h,debug=debug);}
module Nonagon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=9,A=A,R=R,S=S,h=h,debug=debug);}
// 10 sides
module Decagon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=10,A=A,R=R,S=S,h=h,debug=debug);}
// 11 sides
module Handecagon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=11,A=A,R=R,S=S,h=h,debug=debug);}
// 12 sides
module Dodecagon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=12,A=A,R=R,S=S,h=h,debug=debug);}
// ...
// 100 sides
module Hectogon(A=0,R=0,S=0,h=0,debug=false)
{Polygon(N=100,A=A,R=R,S=S,h=h,debug=debug);}
//==EOF=======================================================