-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
68 lines (58 loc) · 1.64 KB
/
util.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
// utility functions
function lerp(A, B, t){
return A + (B - A) * t;
}
function getIntersection(A, B, C, D){
let tTop = (A.y - C.y) * (D.x - C.x) - (A.x - C.x) * (D.y - C.y)
let uTop = (A.x - B.x) * (C.y - A.y) - (A.y - B.y) * (C.x - A.x)
let bottom = (B.x - A.x) * (D.y - C.y) - (B.y - A.y) * (D.x - C.x)
if(0 != bottom){
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
}
}
function polyIntersect(poly1, poly2){
for(let i = 0 ; i < poly1.length ; ++i){
for(let j = 0 ; j < poly2.length ; ++j){
if(getIntersection(
poly1[i],
poly1[(i+1)%poly1.length],
poly2[j],
poly2[(j+1)%poly2.length]
)){
return true
}
}
}
return false
}
function getRGBA(val){
const alpha = Math.abs(val)
const R = val < 0 ? 0 : 255
const G = R
const B = val > 0 ? 0 : 255
return "rgba("+R+","+G+","+B+","+alpha+")"
}
function writeText(ctx, text, x, y, size = 10){
ctx.beginPath()
ctx.font = size + "px Arial";
ctx.fillStyle = "#0a0a0a"
ctx.strokeStyle = "#0a0a0a"
ctx.fillText(text, x, y);
ctx.fill()
ctx.stroke()
}
function save(bestCar){
localStorage.setItem("bestBrain", JSON.stringify(bestCar.brain))
}
function discard(){
localStorage.removeItem("bestBrain")
}