-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmath.scm
More file actions
94 lines (76 loc) · 2.27 KB
/
math.scm
File metadata and controls
94 lines (76 loc) · 2.27 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
93
94
;;;; math.scm
;;;; Math utilities
;;;; Imported by hypergiant.scm
(use data-structures random-bsd)
(export random-normal
random-float
clamp
vclamp
vclamp!
v/
vround
vceiling
vtruncate
vfloor
next-power-of-two
ease
linenear
smooth-step
smoother-step)
(define (random-normal mean sd)
(+ mean (* sd (/ (random 1000000) 1000000))))
(define random-float random-real)
(define (clamp x l u)
(min (max x l) u))
;; TODO vector operations could match gl-math better: optional return arguments that dictate where the result is created
(define (vclamp v l u #!optional non-gc?)
(make-point (clamp (point-x v) l u)
(clamp (point-y v) l u)
(clamp (point-z v) l u)
non-gc?))
(define (vclamp! v l u)
(point-x-set! (clamp (point-x v) l u))
(point-y-set! (clamp (point-y v) l u))
(point-z-set! (clamp (point-z v) l u)))
(define (v/ v s #!optional result)
(let ((r (/ s)))
(v* v r result)))
(define (vround v #!optional non-gc?)
(make-point (round (point-x v))
(round (point-y v))
(round (point-z v))
non-gc?))
(define (vfloor v #!optional non-gc?)
(make-point (floor (point-x v))
(floor (point-y v))
(floor (point-z v))
non-gc?))
(define (vceiling v #!optional non-gc?)
(make-point (ceiling (point-x v))
(ceiling (point-y v))
(ceiling (point-z v))
non-gc?))
(define (vtruncate v #!optional non-gc?)
(make-point (truncate (point-x v))
(truncate (point-y v))
(truncate (point-z v))
non-gc?))
(define (next-power-of-two n)
(inexact->exact (expt 2 (ceiling (/ (log n)
(log 2))))))
;;;; Easing functions
;; TODO more: https://github.com/EmmanuelOga/easing/blob/master/lib/easing.lua
;; http://sol.gfxile.net/interpolation/
(define (ease fun a b time start end)
(+ a (* (- b a)
(fun (/ (- time start)
(- end start))))))
(define linenear identity)
(define (smooth-step x)
(* x x
(- 3 (* 2 x))))
(define (smoother-step x)
(* x x x
(+ (* x (- (* x 6)
15))
10)))