-
Notifications
You must be signed in to change notification settings - Fork 1
/
moon_filled_with_squares.js
145 lines (119 loc) · 4.65 KB
/
moon_filled_with_squares.js
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
// fill a moon/skewed donut with squares
const canvasSketch = require('canvas-sketch');
const penplot = require('./utils/penplot');
const utils = require('./utils/random');
const poly = require('./utils/poly');
let svgFile = new penplot.SvgFile();
// grid settings
let margin = 0.6;
let elementWidth = 2;
let elementHeight = 2;
let columns = 4;
let rows = 4;
const settings = {
dimensions: 'A3',
orientation: 'portrait',
pixelsPerInch: 300,
scaleToView: true,
units: 'cm',
};
//297, 420
let w = 29.7;
let h = 42;
const sketch = (context) => {
let drawingWidth = (columns * (elementWidth + margin)) - margin;
let drawingHeight = (rows * (elementHeight + margin)) - margin;
let marginLeft = (context.width - drawingWidth) / 2;
let marginTop = (context.height - drawingHeight) / 2;
// nr of circles -> 5 to 7
// center is on page
// radius = width/3.5 tot width/2+2
// inner center = displaced by 1 to 3 (or minus)
// inner radius = outer * 0.7 tot 0.8
let circles = [];
let innercircles = [];
let nrOfCircles = utils.getRandomInt(7, 5);
for (let r = 0; r < nrOfCircles; r++) {
circles[r] = {};
circles[r].center = {x: utils.getRandom(w), y: utils.getRandomInt(h)};
circles[r].r = utils.getRandom(Math.max(w/3.5, w/2+2), Math.min(w/3.5, w/2+2));
let displaceSignX = utils.getRandomIntInclusive(1) == 1 ? 1 : -1;
let displaceSignY = utils.getRandomIntInclusive(1) == 1 ? 1 : -1;
let displaceX = utils.getRandom(2.5,1);
let displaceY = utils.getRandom(2.5,1);
innercircles[r] = {};
innercircles[r].center = { x: circles[r].center.x + (displaceX * displaceSignX), y: circles[r].center.y + (displaceY * displaceSignY) }
innercircles[r].r = circles[r].r * utils.getRandom(0.85, 0.75);
}
return ({ context, width, height, units }) => {
svgFile = new penplot.SvgFile();
poly.init(context);
context.fillStyle = 'white';
context.fillRect(0, 0, width, height);
context.strokeStyle = 'black';
context.lineWidth = 0.02;
const drawElement = (origin) => {
let arc = utils.getRandom(Math.PI/2*3, Math.PI/2);
let rot = utils.getRandom(Math.PI);
context.beginPath();
context.arc(origin.x, origin.y, 0.1, rot, rot+arc);
context.stroke();
svgFile.addArc(origin.x, origin.y, 0.1, rot*Math.PI/180, (rot+arc)*Math.PI/180) // /Math.PI*180
}
const drawElement2 = (origin) => {
//console.log(origin);
let s = poly.createSquarePolygon(origin.x, origin.y, 0.2, 0.2);
let corner = utils.getRandomInt(3);
let i = utils.getRandomInt(1);
let pos = utils.getRandomInt(1);
let d = 0.03;
let offset = pos ? d : -d;
s[corner][i] = s[corner][i] + offset;
poly.drawPolygonOnCanvas(context, s);
svgFile.addLine(s, true);
}
// grid repeat starts here
let posX = marginLeft;
let posY = marginTop;
// circles = [];
// innercircles = [];
// circles.push({center: poly.point(width/3, height/3), r: width/2});
// innercircles.push({center: poly.point((width/3)+1.5, (height/3)+1.5), r: width/2*4/5});
// circles.push({center: poly.point(width/3*2, height/3*2), r: width/2});
// innercircles.push({center: poly.point((width/3*2)-1, (height/3*2)+0.8), r: width/2*3.5/5});
// circles.push({center: poly.point(width/2, height/2), r: width/2+2});
// innercircles.push({center: poly.point(width/2, height/2), r: width/2+2*3.5/5});
// circles.push({center: poly.point(width/2*1.5, height/4*3), r: width/2+2});
// innercircles.push({center: poly.point(width/2*1.5, height/4*3), r: width/2+2*3.5/5});
// circles.push({center: poly.point(width/10, height/2+4), r: width/3.5});
// innercircles.push({center: poly.point((width/10)+1.5, (height/2+4)-0.2), r: width/3.5*0.73});
for (let index = 0; index < 100000; index++) {
let x = utils.getRandom(width);
let y = utils.getRandom(height);
let point = poly.point(x,y);
//console.log(point);
for (let i = 0; i < circles.length; i++) {
const circle = circles[i];
const innercircle = innercircles[i];
if (poly.pointIsInCircle(point, circle.center, circle.r)) {
if (!poly.pointIsInCircle(point, innercircle.center, innercircle.r))
{ drawElement(point); }
}
}
}
return [
// Export PNG as first layer
context.canvas,
// Export SVG for pen plotter as second layer
{
data: svgFile.toSvg({
width,
height,
units
}),
extension: '.svg',
}
];
};
};
canvasSketch(sketch, settings);