-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathregularPolygons.jscad
68 lines (54 loc) · 1.83 KB
/
regularPolygons.jscad
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
// title : Regular polygons (non intersecting) generator
// author : Gilbert Duval
// license : MIT License
// revision : 0.001
// tags : polygon
// file : regularPolygons.jscad
function getParameterDefinitions() {
return [
{ type: 'group', caption: 'REGULAR POLYGONS (non intersecting)'},
{ name: 'mode', type: 'choice', caption:'Mode ?', values: ['demo','edit'], captions : ['View Demo','Edit Polygon']},
{ name: 'count', type: 'int', initial: 3, caption: 'Number of segments ?', min:3},
{ name: 'length', type: 'int', initial: 10, caption: 'Length ?' },
];
}
function simpleRotate(cx, cy, x, y, angle) {
// https://stackoverflow.com/questions/17410809/how-to-calculate-rotation-in-2d-in-javascript
// cx, cy : center of rotation - x,y : point to rotate, angle : rotation angle in degrees
var radians = (Math.PI / 180) * angle,
cos = Math.cos(radians),
sin = Math.sin(radians),
nx = (cos * (x - cx)) + (sin * (y - cy)) + cx,
ny = (cos * (y - cy)) - (sin * (x - cx)) + cy;
return [nx, ny];
}
function regularPolygon(edgesCount, edgeLength){
// http://www.georgehart.com/virtual-polyhedra/polygons.html
var angleD, vertices = [], i, x, y;
angleD = 360 / edgesCount;
vertices.push([0,0]);
for(i = 1; i < edgesCount; i++){
x = vertices[i-1][0];
y = vertices[i-1][1];
vertices.push(simpleRotate(x, y, x, y + edgeLength, angleD * i));
}
return polygon({points:vertices});
}
function main(params){
var r, i;
if(params.mode !== 'demo'){
r = regularPolygon(params.count, params.length);
} else {
r = [];
for(i = 3; i< 9; i++){
r.push(regularPolygon(i,(9-i)*2).translate([(i-5)*15,9-i]));
}
r.push(
difference(
regularPolygon(8,10).translate([0,-30]),
regularPolygon(4,10).rotateZ(45).translate([5,-34])
)
);
}
return r;
}