-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmath.js
More file actions
92 lines (73 loc) · 1.88 KB
/
math.js
File metadata and controls
92 lines (73 loc) · 1.88 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
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
import test from 'ava'
import litecanvas from '../src/index.js'
import * as sinon from 'sinon'
/** @type {LitecanvasInstance} */
let local
test.before(() => {
sinon.stub(console) // silent console
local = litecanvas({
global: false,
})
local.listen('init', () => local.pause())
})
test.after(() => {
local.quit()
})
test('PI', async (t) => {
t.is(local.PI, Math.PI)
})
test('TWO_PI', async (t) => {
t.is(local.TWO_PI, Math.PI * 2)
})
test('HALF_PI', async (t) => {
t.is(local.HALF_PI, Math.PI / 2)
})
test('lerp', async (t) => {
t.is(local.lerp(10, 20, 0), 10)
t.is(local.lerp(10, 20, 0.5), 15)
t.is(local.lerp(10, 20, 1), 20)
t.is(local.lerp(10, 20, 2), 30)
})
test('deg2rad', async (t) => {
t.is(local.deg2rad(180), local.PI)
t.is(local.deg2rad(360), local.TWO_PI)
})
test('rad2deg', async (t) => {
t.is(local.rad2deg(local.PI), 180)
t.is(local.rad2deg(local.HALF_PI), 90)
})
test('clamp', async (t) => {
t.is(local.clamp(-10, 0, 100), 0)
t.is(local.clamp(50, 0, 100), 50)
t.is(local.clamp(999999, 0, 100), 100)
})
test('wrap', async (t) => {
t.is(local.wrap(5, 0, 10), 5)
t.is(local.wrap(-1, 0, 10), 9)
t.is(local.wrap(11, 0, 10), 1)
})
test('map', async (t) => {
// without contrains (default behavior)
t.is(local.map(150, 0, 100, 0, 1), 1.5)
// with contrains
t.is(local.map(150, 0, 100, 0, 1, true), 1)
})
test('norm', async (t) => {
// without contrains
t.is(local.norm(50, 0, 100), 0.5)
// with contrains
t.is(local.norm(150, 0, 100), 1.5)
})
test('round', async (t) => {
const n = 9.87654321
// without precision
t.is(local.round(n), 10)
// with precision
t.is(local.round(n, 2), 9.88)
t.is(local.round(n, 5), 9.87654)
})
test('dist', async (t) => {
const expected = 100
const actual = local.dist(0, 0, 0, 100)
t.is(actual, expected)
})