-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpolygon.scad
More file actions
142 lines (114 loc) · 3.46 KB
/
polygon.scad
File metadata and controls
142 lines (114 loc) · 3.46 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
// Polygons with internal connections
//
// Render a polygon with N (3 or more) nodes, with an
// optional center node. Then connect each of the nodes
// node to the center nodes and to the NodeConnectivity
// nearest neighbor nodes.
/* [Polygon] */
// [Number of points]
_N = 6;
// [Radius]
_Radius = 100.0;
// [Center Node]
_CenterNode = false;
/* Node */
// [Node Radius]
_NodeRadius = 5.0;
// [Node Thickness]
_NodeThickness = 1.0;
// [Node Connectivity]
_NodeConnectivity = 1;
/* Edge */
// [Edge Width]
_EdgeWidth = 2.0;
// [Inner Edge Gap]
_InnerEdgeGap = 4.0;
// [Outer Edge Gap]
_OuterEdgeGap = 1.5;
// [Edge Thickness]
_EdgeThickness = 1.0;
module Polygon(Radius, N, CenterNode, NodeRadius, NodeThickness, NodeConnectivity, EdgeWidth, EdgeThickness, OuterEdgeGap, InnerEdgeGap)
{
X = [for (Theta = [0 : 360 / N : 360]) Radius * cos(Theta)];
Y = [for (Theta = [0 : 360 / N : 360]) Radius * sin(Theta)];
NodeCount = len(X) - 1;
// Render nodes
for (i = [0 : NodeCount - 1])
{
translate([X[i], Y[i], 0])
{
linear_extrude(NodeThickness)
{
echo(NodeRadius);
circle(NodeRadius);
}
}
}
// Render center node
if (CenterNode)
{
linear_extrude(NodeThickness)
{
circle(NodeRadius);
}
}
// Render edge to connect center node to each node
if (CenterNode)
{
for (i = [0 : NodeCount - 1])
{
Edge(0, 0, X[i], Y[i], EdgeThickness, EdgeWidth, InnerEdgeGap, OuterEdgeGap);
}
}
// Render edges to connect each node to all others
for (i = [0 : NodeCount - 1])
{
for (j = [i + 1 : i + NodeConnectivity])
{
if (i != j)
{
jj = (j >= 0) ? (j % NodeCount) : (j + NodeCount);
Edge(X[i], Y[i], X[jj], Y[jj], EdgeThickness, EdgeWidth, InnerEdgeGap, OuterEdgeGap);
}
}
}
}
module Edge(X0, Y0, X1, Y1, EdgeThickness, EdgeWidth, InnerEdgeGap, OuterEdgeGap)
{
// Compute distance between points
DX = X1 - X0;
DY = Y1 - Y0;
H = sqrt(DX * DX + DY * DY);
// Compute midpoint of [X0,Y0] - [X1, Y1]
XMid = X0 + (X1 - X0) / 2;
YMid = Y0 + (Y1 - Y0) / 2;
// For debugging, plot the midpoint
translate([XMid, YMid, 0])
{
color("red")
{
linear_extrude(EdgeThickness)
{
circle(r=2);
}
}
}
// Compute angle between points
A = atan2(DY, DX);
// TODO This needs work to compute midpoint that respects InnerEdgeGap and OuterEdgeGap
translate([XMid, YMid, 0])
{
rotate(A)
{
linear_extrude(EdgeThickness)
{
square([H - (InnerEdgeGap * 2), EdgeWidth], center=true);
}
}
}
}
module main(Radius, N, CenterNode, NodeRadius, NodeThickness, NodeConnectivity, EdgeWidth, EdgeThickness, InnerEdgeGap, OuterEdgeGap)
{
Polygon(Radius, N, CenterNode, NodeRadius, NodeThickness, NodeConnectivity, EdgeWidth, EdgeThickness, InnerEdgeGap, OuterEdgeGap);
}
main(_Radius, _N, _CenterNode, _NodeRadius, _NodeThickness, _NodeConnectivity, _EdgeWidth, _EdgeThickness, _InnerEdgeGap, _OuterEdgeGap);