61 lines
1.5 KiB
Plaintext
61 lines
1.5 KiB
Plaintext
(define-module math
|
|
:imports ("oo.slime")
|
|
:exports (pi abs sqrt make-vector3)
|
|
|
|
(define pi
|
|
:doc "Tha famous circle constant."
|
|
3.14159265)
|
|
|
|
(define (abs x)
|
|
:doc "Accepts one argument and returns the absoulte value of it"
|
|
(if (> x 0) x (- x)))
|
|
|
|
(define (sqrt x)
|
|
:doc "Accepts one argument and returns the square root of it"
|
|
(** x 0.5))
|
|
|
|
(define-class (vector3 x y z)
|
|
(define (set-x new-x) (mutate x new-x))
|
|
(define (set-y new-y) (mutate y new-y))
|
|
(define (set-z new-z) (mutate z new-z))
|
|
|
|
(define (length)
|
|
(** (+ (* x x) (* y y) (* z z)) 0.5))
|
|
|
|
(define (scale fac)
|
|
(mutate x (* fac x))
|
|
(mutate y (* fac y))
|
|
(mutate z (* fac z))
|
|
fac)
|
|
|
|
(define (add other)
|
|
(make-vector3
|
|
(+ (-> other x) x)
|
|
(+ (-> other y) y)
|
|
(+ (-> other z) z)))
|
|
|
|
(define (subtract other)
|
|
(make-vector3
|
|
(- (-> other x) x)
|
|
(- (-> other y) y)
|
|
(- (-> other z) z)))
|
|
|
|
(define (equal? other)
|
|
(and (= (-> other x) x)
|
|
(= (-> other y) y)
|
|
(= (-> other z) z)))
|
|
|
|
(define (scalar-product other)
|
|
(+ (* (-> other x) x)
|
|
(* (-> other y) y)
|
|
(* (-> other z) z)))
|
|
|
|
(define (cross-product other)
|
|
(make-vector3
|
|
(- (* (-> other z) y) (* (-> other y) z))
|
|
(- (* (-> other x) z) (* (-> other z) x))
|
|
(- (* (-> other y) x) (* (-> other x) y))))
|
|
|
|
(define (print)
|
|
(print :sep "" "[vector3] (" x " " y " " z ")"))))
|