-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
245 lines (196 loc) · 5.51 KB
/
main.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
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
244
245
const point = (x, y) => ({
x,
y,
});
const line = (x1, y1, x2, y2) => ({
p1: point(x1, y1),
p2: point(x2, y2),
});
const rectLines = (x, y, w, h) => {
const [x1, y1] = [x, y];
const [x2, y2] = [x + w, y];
const [x3, y3] = [x, y + h];
const [x4, y4] = [x + w, y + h];
return [
line(x1, y1, x2, y2),
line(x1, y1, x3, y3),
line(x2, y2, x4, y4),
line(x3, y3, x4, y4),
];
};
const HEIGHT = 574;
const WIDTH = 1024;
const canvas = document.getElementById("canvas");
canvas.height = HEIGHT;
canvas.width = WIDTH;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "black";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
const boundaries = [
line(0, 0, WIDTH, 0),
line(0, 0, 0, HEIGHT),
line(0, HEIGHT, WIDTH, HEIGHT),
line(WIDTH, 0, WIDTH, HEIGHT),
];
const lines = [
...boundaries,
line(555, 222, 200, 400),
...rectLines(400, 400, 150, 90),
...rectLines(900, 100, 100, 400),
...rectLines(400, 50, 250, 40),
...[
line(50, 50, 350, 70),
line(50, 50, 200, 200),
line(200, 200, 350, 70)
]
];
function getAllPointsFromLines() {
return new Set(lines.map((l) => [l.p1, l.p2]).flat());
}
function getCorners() {
const seen = new Set();
const all = new Set();
for (let { p1, p2 } of lines.filter((l) => !boundaries.includes(l))) {
if (!seen.has(JSON.stringify(p1))) {
all.add(p1);
seen.add(JSON.stringify(p1));
}
if (!seen.has(JSON.stringify(p2))) {
all.add(p2);
seen.add(JSON.stringify(p2));
}
}
return all;
}
function distance(p1, p2) {
return Math.sqrt(
(p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)
);
}
function intersects(L1, L2) {
// Reference: https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line_segment
const x1 = L1.p1.x;
const y1 = L1.p1.y;
const x2 = L1.p2.x;
const y2 = L1.p2.y;
const x3 = L2.p1.x;
const y3 = L2.p1.y;
const x4 = L2.p2.x;
const y4 = L2.p2.y;
const t =
((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) /
((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
const u =
((x1 - x3) * (y1 - y2) - (y1 - y3) * (x1 - x2)) /
((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
if (t >= 0 && t <= 1 && u >= 0 && u <= 1) {
return point(x1 + t * (x2 - x1), y1 + t * (y2 - y1));
}
return null;
}
function findIntersectingPoints(line) {
const points = [];
for (let other of lines) {
const r = intersects(line, other);
if (r) {
points.push(r);
}
}
return points;
}
function renderAllLines() {
for (let line of lines) {
const { p1, p2 } = line;
ctx.strokeStyle = "red";
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.stroke();
}
}
function drawDot(x, y, color = "red") {
ctx.beginPath();
ctx.fillStyle = color;
ctx.arc(x, y, 5, 2 * Math.PI, 0, false);
ctx.fill();
}
function getAngle(x, y, ptn) {
const dx = ptn.x - x;
const dy = ptn.y - y;
return Math.atan2(dy, dx);
}
function sortByAngle(x, y, pts) {
pts.sort((p1, p2) => {
return getAngle(x, y, p1) - getAngle(x, y, p2);
});
}
renderAllLines();
function drawIlluminatedZone(lightPos, dots) {
const gradient = ctx.createRadialGradient(
lightPos.x, lightPos.y, 1, lightPos.x, lightPos.y, 400
);
// Add three color stops
gradient.addColorStop(0, "rgba(255, 255, 255, 1)");
gradient.addColorStop(1, "rgba(255, 255, 255, 0)");
ctx.fillStyle = gradient;
ctx.beginPath();
for (let i = 0; i < dots.length; i++) {
// this will wrap around
const [a, b] = [dots[i], dots[(i + 1)%dots.length]]
if (i == 0) {
ctx.moveTo(a.x, a.y);
} else {
ctx.lineTo(a.x, a.y);
}
ctx.lineTo(b.x, b.y);
}
ctx.fill();
}
canvas.addEventListener("mousemove", (e) => {
// get the mouse position
let x = e.pageX - canvas.offsetLeft;
let y = e.pageY - canvas.offsetTop;
ctx.clearRect(0, 0, WIDTH, HEIGHT);
ctx.fillStyle = "black";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.beginPath();
ctx.fillStyle = "black";
ctx.arc(x, y, 5, 2 * Math.PI, 0, false);
ctx.fill();
renderAllLines();
const rays = [];
const corners = getCorners();
for (let { x: a, y: b } of getAllPointsFromLines()) {
rays.push(line(x, y, a, b));
}
function getLineByAngle(x, y, angle) {
const k = 2 * WIDTH * Math.cos(angle);
const z = 2 * WIDTH * Math.sin(angle);
const a = x + k;
const b = y + z;
return line(x, y, a, b);
}
for (let corner of corners) {
let angle = getAngle(x, y, corner);
rays.push(getLineByAngle(x, y, angle));
rays.push(getLineByAngle(x, y, angle + 0.0001));
rays.push(getLineByAngle(x, y, angle - 0.0001));
}
// drawing each dot and line
const dots = [];
rays.forEach((aLine) => {
const pts = findIntersectingPoints(aLine);
pts.sort((a, b) => {
return distance(point(x, y), a) - distance(point(x, y), b);
});
if (pts.length > 0) {
let ptn = pts[0];
dots.push(ptn);
const { x: _x, y: _y } = ptn;
}
});
sortByAngle(x, y, dots);
const lightPosition = point(x, y);
dots.push(dots[0]);
drawIlluminatedZone(lightPosition, dots)
});