Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
loeasy68
GitHub Repository: loeasy68/loeasy68.github.io
Path: blob/main/website/GAUSS/js/utils.js
2941 views
1
var polyTranslate = function(p, trX, trY)
2
{
3
var points = [];
4
5
for(var i = 0; i < p.points.length; i++)
6
points[i] = { x : p.points[i].x + trX, y : p.points[i].y + trY };
7
return points;
8
};
9
10
11
var cropImage = function(poly)
12
{
13
var maskColor = 'rgba(255, 0, 255, 255)';
14
var maskColorRGBA = [255, 0, 255, 255];
15
16
var pixels = { data : null, imageWidth : null};
17
18
var isMaskColor = function(pixelData)
19
{
20
return (pixelData[0] == maskColorRGBA[0] &&
21
pixelData[1] == maskColorRGBA[1] &&
22
pixelData[2] == maskColorRGBA[2] &&
23
pixelData[3] == maskColorRGBA[3]);
24
};
25
26
var minX, minY, maxX, maxY;
27
28
minX = maxX = poly.points[0].x;
29
minY = maxY = poly.points[0].y;
30
31
for(var i = 1; i < poly.points.length; i++)
32
{
33
var p = poly.points[i];
34
if(p.x < minX)
35
minX = p.x;
36
if(p.x > maxX)
37
maxX = p.x;
38
if(p.y < minY)
39
minY = p.y;
40
if(p.y > maxY)
41
maxY = p.y;
42
}
43
var tmpCanvas = document.createElement('canvas');
44
var tmpContext = tmpCanvas.getContext('2d');
45
tmpCanvas.width = maxX - minX;
46
tmpCanvas.height = maxY - minY;
47
tmpContext.clearRect(0, 0, tmpCanvas.width, tmpCanvas.height);
48
tmpContext.fillStyle = 'white';
49
tmpContext.fillRect(0, 0, tmpCanvas.width, tmpCanvas.height);
50
var newPoly = owl.deepCopy(poly);
51
newPoly.points = polyTranslate(newPoly, -minX, -minY);
52
drawPolygon(newPoly, maskColor, maskColor, tmpContext);
53
54
pixels.data = tmpContext.getImageData(0, 0, tmpCanvas.width, tmpCanvas.height).data;
55
pixels.imageWidth = tmpCanvas.width;
56
var tmp = canvas.state;
57
canvas.state = 'idle';
58
updateCanvas(editorCurrentRoom, 'room');
59
var cutImage = ctx.getImageData(minX, minY, tmpCanvas.width, tmpCanvas.height);
60
canvas.state = tmp;
61
62
63
64
var findPixelsIndex = function(i, j) { return (j * tmpCanvas.width + i) * 4; };
65
for(var i = 0; i < tmpCanvas.width; i++)
66
for(var j = 0; j < tmpCanvas.height; j++)
67
{
68
var idx = findPixelsIndex(i, j);
69
var pixelColor = [pixels.data[idx], pixels.data[idx + 1], pixels.data[idx + 2], pixels.data[idx + 3]];
70
if (isMaskColor(pixelColor) == false)
71
{
72
cutImage.data[idx] = 0;
73
cutImage.data[idx + 1] = 0;
74
cutImage.data[idx + 2] = 0;
75
cutImage.data[idx + 3] = 0;
76
}
77
}
78
tmpContext.putImageData(cutImage, 0, 0);
79
80
return {src: tmpCanvas.toDataURL(), width: maxX - minX, height: maxY - minY, pos : { x : minX, y : minY }};
81
};
82
83
//+ Jonas Raoni Soares Silva
84
//@ http://jsfromhell.com/math/is-point-in-poly [rev. #0]
85
var isPointInPoly = function (poly, pt)
86
{
87
for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
88
((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
89
&& (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
90
&& (c = !c);
91
return c;
92
};
93
94
var qSort = function(a, orderingFunction) // Quicksort for walkbehinds ordering
95
{
96
if (a.length == 0) return [];
97
98
var left = [], right = [], pivot = a[0];
99
100
for (var i = 1; i < a.length; i++)
101
{
102
orderingFunction(a[i], pivot) === true ? left.push(a[i]) : right.push(a[i]);
103
}
104
105
return qSort(left, orderingFunction).concat(pivot, qSort(right, orderingFunction));
106
};
107
108
var orderWalkBehinds = function(wb1, wb2)
109
{
110
return wb1.centralPerspectiveWalkBehind < wb2.centralPerspectiveWalkBehind;
111
};
112
113
var orderPanels = function(panel1_id, panel2_id)
114
{
115
return panel1_id < panel2_id;
116
};
117
118
var setCanvasResolution = function(canvas, w, h)
119
{
120
$(canvas).css({ 'width' : w, 'height' : h});
121
scaleFactor.x = w / canvas.width;
122
scaleFactor.y = h / canvas.height;
123
};
124
125
var getDistanceBetweenPoints = function(p1, p2)
126
{
127
try
128
{
129
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
130
}
131
catch (exception)
132
{
133
return Infinity;
134
}
135
};
136
137
/**
138
** See: http://jsfromhell.com/math/dot-line-length
139
**
140
** Distance from a point to a line or segment.
141
**
142
** @param {number} x point's x coord
143
** @param {number} y point's y coord
144
** @param {number} x0 x coord of the line's A point
145
** @param {number} y0 y coord of the line's A point
146
** @param {number} x1 x coord of the line's B point
147
** @param {number} y1 y coord of the line's B point
148
** @param {boolean} overLine specifies if the distance should respect the limits
149
** of the segment (overLine = true) or if it should consider the segment as an
150
** infinite line (overLine = false), if false returns the distance from the point to
151
** the line, otherwise the distance from the point to the segment.
152
**/
153
var dotLineLength = function(x, y, x0, y0, x1, y1, o) {
154
function lineLength(x, y, x0, y0){
155
return Math.sqrt((x -= x0) * x + (y -= y0) * y);
156
}
157
if(o && !(o = function(x, y, x0, y0, x1, y1){
158
if(!(x1 - x0)) return {x: x0, y: y};
159
else if(!(y1 - y0)) return {x: x, y: y0};
160
var left, tg = -1 / ((y1 - y0) / (x1 - x0));
161
return {x: left = (x1 * (x * tg - y + y0) + x0 * (x * - tg + y - y1)) / (tg * (x1 - x0) + y0 - y1), y: tg * left - tg * x + y};
162
}(x, y, x0, y0, x1, y1), o.x >= Math.min(x0, x1) && o.x <= Math.max(x0, x1) && o.y >= Math.min(y0, y1) && o.y <= Math.max(y0, y1))){
163
var l1 = lineLength(x, y, x0, y0), l2 = lineLength(x, y, x1, y1);
164
return l1 > l2 ? l2 : l1;
165
}
166
else {
167
var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;
168
return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b);
169
}
170
};
171
172
function wrapText(context, text, x, y, maxWidth, lineHeight, fillColor)
173
{
174
var cars = text.split("\n");
175
context.fillStyle = fillColor;
176
177
for (var ii = 0; ii < cars.length; ii++) {
178
179
var line = "";
180
var words = cars[ii].split(" ");
181
var correction = 0;
182
for (var n = 0; n < words.length; n++) {
183
var testLine = line + words[n] + " ";
184
var metrics = context.measureText(testLine);
185
var testWidth = metrics.width;
186
187
if (testWidth > maxWidth) {
188
metrics = context.measureText(line);
189
context.lineWidth = 5;
190
var tmp = x - metrics.width / 2;
191
if(tmp < 0)
192
{
193
correction = -tmp;
194
tmp = 0;
195
}
196
context.strokeText(line, tmp, y);
197
context.lineWidth = 3;
198
context.fillText(line, tmp, y);
199
line = words[n] + " ";
200
y += lineHeight;
201
}
202
else {
203
line = testLine;
204
}
205
}
206
207
var metrics = context.measureText(line);
208
context.lineWidth = 5;
209
var tmp = x - metrics.width / 2;
210
if(tmp < 0)
211
{
212
correction = 0;
213
tmp = 0;
214
}
215
context.strokeText(line, tmp + correction, y);
216
context.lineWidth = 3;
217
context.fillText(line, tmp + correction, y);
218
y += lineHeight;
219
}
220
};
221
222
var keys = function(obj)
223
{
224
var ks = [];
225
for(var i in obj)
226
ks.push(i);
227
return ks;
228
};
229
230
var DEBUG_drawWalkBoxes = function()
231
{
232
for(var key in testCurrentRoom.walkBoxes)
233
{
234
var wb = testCurrentRoom.walkBoxes[key];
235
if(wb.visible)
236
drawPolygon(wb.polygon, '', 'green', gameCtx, false);
237
}
238
};
239
240
var getVector = function(p1, p2)
241
{
242
return new Point(p2.x - p1.x, p2.y - p1.y);
243
};
244
245
var normalizeVector = function(v)
246
{
247
var norm = Math.sqrt(Math.pow(v.x, 2) + Math.pow(v.y, 2));
248
v.x /= norm;
249
v.y /= norm;
250
return v;
251
};
252
253
var drawStraightLine = function(p1, p2)
254
{
255
var v = normalizeVector(getVector(p1, p2));
256
var p = new Point(p1.x, p1.y);
257
var speed = 3;
258
v.x *= speed;
259
v.y *= speed;
260
261
ctx.fillRect(p1.x - 5, p1.y - 5, 10, 10);
262
ctx.fillRect(p2.x - 5, p2.y - 5, 10, 10);
263
while(p.x !== p2.x || p.y !== p2.y)
264
{
265
ctx.fillRect(p.x - 1, p.y - 1, 2, 2);
266
if((p.x > p2.x && p.x + v.x < p2.x) || (p.x < p2.x && p.x + v.x > p2.x))
267
p.x = p2.x;
268
else
269
p.x += v.x;
270
if((p.y > p2.y && p.y + v.y < p2.y) || (p.y < p2.y && p.y + v.y > p2.y))
271
p.y = p2.y;
272
else
273
p.y += v.y;
274
}
275
};
276
277
var getDistanceFromPoints = function(p1, p2)
278
{
279
return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2))
280
};
281
282
var checkLineIntersection = function(edge1, edge2)
283
{
284
var denominator, a, b, numerator1, numerator2;
285
var result = { x: null, y: null, inLines: false};
286
287
var line1StartX = edge1[0].x;
288
var line1EndX = edge1[1].x;
289
var line1StartY = edge1[0].y;
290
var line1EndY = edge1[1].y;
291
var line2StartX = edge2[0].x;
292
var line2EndX = edge2[1].x;
293
var line2StartY = edge2[0].y;
294
var line2EndY = edge2[1].y;
295
denominator = ((line2EndY - line2StartY) * (line1EndX - line1StartX)) - ((line2EndX - line2StartX) * (line1EndY - line1StartY));
296
if (denominator == 0) {
297
return result;
298
}
299
a = line1StartY - line2StartY;
300
b = line1StartX - line2StartX;
301
numerator1 = ((line2EndX - line2StartX) * a) - ((line2EndY - line2StartY) * b);
302
numerator2 = ((line1EndX - line1StartX) * a) - ((line1EndY - line1StartY) * b);
303
a = numerator1 / denominator;
304
b = numerator2 / denominator;
305
306
// if we cast these lines infinitely in both directions, they intersect here:
307
result.x = line1StartX + (a * (line1EndX - line1StartX));
308
result.y = line1StartY + (a * (line1EndY - line1StartY));
309
310
if ((a > 0 && a < 1) && (b > 0 && b < 1))
311
result.inLines = true;
312
313
return result;
314
};
315
316
var getNextPointInLine = function(p1, p2, speed)
317
{
318
var v = normalizeVector(getVector(p1, p2));
319
var p = new Point(p1.x, p1.y);
320
321
if(Math.abs(p1.x - p2.x) < 1 && Math.abs(p1.y - p2.y) < 1)
322
return p2;
323
v.x *= speed;
324
v.y *= speed;
325
326
if((p.x > p2.x && p.x + v.x < p2.x) || (p.x < p2.x && p.x + v.x > p2.x))
327
p.x = p2.x;
328
else
329
p.x += v.x;
330
if((p.y > p2.y && p.y + v.y < p2.y) || (p.y < p2.y && p.y + v.y > p2.y))
331
p.y = p2.y;
332
else
333
p.y += v.y;
334
335
return p;
336
337
};
338
339
var getLineSlope = function(p1, p2)
340
{
341
return (p1.y - p2.y) / (p1.x - p2.x);
342
};
343
344
var downloadCroppedImage = function (filename, text)
345
{
346
// atob to base64_decode the data-URI
347
var image_data = atob(text.split(',')[1]);
348
// Use typed arrays to convert the binary data to a Blob
349
var arraybuffer = new ArrayBuffer(image_data.length);
350
var view = new Uint8Array(arraybuffer);
351
for (var i=0; i<image_data.length; i++)
352
{
353
view[i] = image_data.charCodeAt(i) & 0xff;
354
}
355
var blob = new Blob([arraybuffer], {type : 'image/png'});
356
var pom = document.createElement('a');
357
//pom.setAttribute('href', 'data:application/json;charset=utf-8,' + encodeURIComponent(text));
358
pom.setAttribute('href', URL.createObjectURL(blob));
359
pom.setAttribute('download', filename);
360
pom.click();
361
};
362