-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror-arranger.js
185 lines (139 loc) · 4.63 KB
/
mirror-arranger.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
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
"use strict";
import fs from 'fs-extra';
import path from 'path';
import colors from 'colors';
import * as color_convert from './color-convert.js';
import * as image_loader from './image-loader.js';
import vector from './vector.js';
import {stipple} from './stipple.js';
export function convert(settings, image) {
const width = image.width;
const height = image.height;
const input_context = image.getContext('2d');
const input_data = input_context.getImageData(0, 0, width, height);
let colors = color_convert.imageDataToARGBObjectArray(input_data.data);
let pixels = colors.map( (color, i) => {
let x = i % width;
let y = Math.floor(i / width);
return {color, x, y};
})
.filter(pixel => {
return pixel.color.r == 255 && pixel.color.g == 255 && pixel.color.b == 255;
})
return {pixels, image_size: {width, height}};
}
export function circle(settings, mirrors) {
const width = 100;
const height = 100;
const pixels = Array
.from({length: mirrors})
.map( (_, i, a) => (i / a.length) * Math.PI * 2)
.map( angle => {
return {
x: width / 2 + Math.cos(angle) * width / 2,
y: height / 2 + Math.sin(angle) * height / 2,
color: {r:0, g: 0, b: 0, a: 255},
}
});
return {pixels, image_size: {width, height}};
}
export function spiral(settings, mirrors, turns) {
const width = 100;
const height = 100;
const pixels = Array
.from({length: mirrors})
.map( (_, i, a) => {
return {
angle: (i / a.length) * Math.PI * 2 * turns,
t: i / a.length,
}
})
.map( state => {
return {
x: width / 2 + Math.cos(state.angle) * width / 2 * state.t,
y: height / 2 + Math.sin(state.angle) * height / 2 * state.t,
color: {r:0, g: 0, b: 0, a: 255},
}
});
return {pixels, image_size: {width, height}};
}
export function equidistantSpiral(settings) {
var config = settings.input.arrangement.equidistant_spiral;
const width = config.width;
const height = config.height;
let centerX = width/2;
let centerY = height/2;
let radius = width / 2 - 4;
let coils = config.coils;
var thetaMax = coils * 2 * Math.PI;
var awayStep = radius / thetaMax;
var chord = config.chord;
var pixels = [];
for (var theta = chord / awayStep; theta <= thetaMax; ) {
var away = awayStep * theta;
theta += chord / away;
pixels.push({
x: centerX + Math.cos ( theta ) * away,
y: centerY + Math.sin ( theta ) * away,
color: {r:0, g: 0, b: 0, a: 255},
});
}
return {pixels, image_size: {width, height}};
}
export async function stippling(settings) {
let image = await image_loader.readImage(settings.input.arrangement.stippling.path);
await image_loader.writeImage(path.join(settings.output.path, 'arrangement.png'), image);
const input_context = image.getContext('2d');
const input_data = input_context.getImageData(0, 0, image.width, image.height);
let image_size = Math.max(image.width, image.height);
const point_count = settings.input.arrangement.stippling.points;
const iterations = [settings.input.arrangement.stippling.iterations];
let stipled_points = stipple(input_data.data, image.width, image.height, point_count, iterations, settings.input.arrangement.stippling.invert)
.points
.reduce( (list, current, index) => {
if (index % 2 == 0) {
return list.concat([{x: current, y: null}]);
}
list[list.length-1].y = current;
return list;
}, []);
const size = settings.input.arrangement.stippling.imaginary_size;
let scaled_stipled_points = stipled_points.map(point => {
return {
x: point.x / image_size * size,
y: point.y / image_size * size,
color: {r:255, g:255, b: 255, a: 255},
}
});
return {
pixels: scaled_stipled_points,
image_size: {
width: size,
height: size
}
};
}
export function concentricCircles(settings) {
const width = 100;
const height = 100;
let centerX = width/2;
let centerY = height/2;
let radius = width / 2 - 4;
var chord = 2;
var circles = Math.round(radius / chord);
var pixels = [];
for (var circle = 0; circle < circles; circle++) {
var r = circle * radius / circles;
var circomference = r * 2 * Math.PI;
var mirrors_in_circle = Math.floor(circomference / chord);
for (var i = 0; i < mirrors_in_circle; i++) {
var angle = Math.PI * 2 / mirrors_in_circle * i;
pixels.push({
x: centerX + Math.cos( angle ) * r,
y: centerY + Math.sin( angle ) * r,
color: {r:0, g: 0, b: 0, a: 255},
});
}
}
return {pixels, image_size: {width, height}};
}