add math utilities

This commit is contained in:
Quentin Leonetti 2019-07-14 05:14:53 +02:00
parent 216a077799
commit f05fa86dbb
2 changed files with 40 additions and 2 deletions

View File

@ -82,6 +82,20 @@ function Library (ronin) {
return arr return arr
} }
this.range = (start, end, step = 1) => {
let arr = []
if (step > 0) {
for(let i = start; i <= end ; i+= step) {
arr.push(i)
}
} else {
for(let i = start; i >= end ; i+= step) {
arr.push(i)
}
}
return arr
}
// Shapes // Shapes
this.pos = (x, y, t = 'pos') => { this.pos = (x, y, t = 'pos') => {
@ -179,6 +193,22 @@ function Library (ronin) {
return Math.round(val / step) * step return Math.round(val / step) * step
} }
this.min = Math.min
this.max = Math.max
this.ceil = Math.ceil
this.floor = Math.floor
this.sin = Math.sin
this.cos = Math.cos
this.PI = Math.PI
this.TWO_PI = Math.PI
// Generics // Generics
this.echo = (...args) => { this.echo = (...args) => {
@ -192,11 +222,16 @@ function Library (ronin) {
} }
this.test = (name, a, b) => { this.test = (name, a, b) => {
if (a !== b) { if (Array.isArray(a)) {
// TODO: make testing more solid
a = a.toString()
b = b.toString()
}
if (a != b) {
console.warn('failed ' + name, a, b) console.warn('failed ' + name, a, b)
} else { } else {
console.log('passed ' + name, a, b) console.log('passed ' + name, a, b)
} }
return a === b return a == b
} }
} }

View File

@ -23,4 +23,7 @@
(test "first" (first ("a" "b" "c")) "a") (test "first" (first ("a" "b" "c")) "a")
(test "last" (last ("a" "b" "c")) "c") (test "last" (last ("a" "b" "c")) "c")
(test "range simple" (range 0 4) (0 1 2 3 4))
(test "range with step" (range 0 4 2) (0 2 4))
(test "range with negative step" (range 0 -4 -1) (0 -1 -2 -3 -4))
) )