-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
61 lines (54 loc) · 1.67 KB
/
utils.js
File metadata and controls
61 lines (54 loc) · 1.67 KB
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
// Linear Interpolation
function lerp(A,B,percentage){
return A + (B-A)*percentage;
}
// Get the intersection point of lines
// using Line Segment Intersection
// Ref: https://www.youtube.com/watch?v=5FkOO1Wwb8w&ab_channel=EngineerNick
function getIntersection(A,B,C,D){
const tTop = (D.x-C.x)*(A.y-C.y) - (D.y-C.y)*(A.x-C.x);
const uTop = (C.y-A.y)*(A.x-B.x) - (C.x-A.x)*(A.y-B.y);
const bottom = (D.y-C.y)*(B.x-A.x) - (D.x-C.x)*(B.y-A.y);
if(bottom != 0){
const t = tTop/bottom;
const u = uTop/bottom;
if(t>=0 && t<=1 && u>=0 && u<=1){
return {
x:lerp(A.x,B.x,t),
y:lerp(A.y,B.y,t),
offset:t
}
}
}
return null;
}
// Check intersection of polygon with obstacles
function polysIntersect(poly1,poly2){
for(let i=0; i<poly1.length; i++){
for(let j=0; j<poly2.length; j++){
// Forming segment lines from point to point
const touch= getIntersection(
poly1[i],
poly1[(i+1)%poly1.length],
poly2[j],
poly2[(j+1)%poly2.length]
);
if(touch){
return true;
}
}
}
return false;
}
//// Cofigurate NetworkCanvas Stroke Color based on weights
function getRGBA(value){
// Higher opacity for weights closer to 1 or -1
const alpha=Math.abs(value);
// Higher Red saturation for weights closer to 1
const R = value < 0 ? 0 : 255;
// Color Blending
const G = 120;
// Higher Blue saturation for weights closer to -1
const B = value > 0 ? 0 : 255;
return `rgba(${R},${G},${B},${alpha})`;
}