-
Notifications
You must be signed in to change notification settings - Fork 3
/
vec.lisp
50 lines (38 loc) · 1.07 KB
/
vec.lisp
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
(in-package :cl-mesh)
(use-package :let-plus)
(defun v3 (a b c)
(vector a b c))
(defun v+ (a b)
(declare (type (simple-vector 3) a b))
(map 'vector #'+ a b))
(defun v- (a b)
(declare (type (simple-vector 3) a b))
(map 'vector #'- a b))
(defun v-norm2 (a)
(declare (type (simple-vector 3) a))
(v-dot a a))
(defun v-norm (a)
(declare (type (simple-vector 3) a))
(sqrt (v-norm2 a)))
(defun v-cos-angle (a b)
(declare (type (simple-vector 3) a b))
(/ (v-dot a b) (v-norm a) (v-norm b)))
(defun v-dist2 (a b)
(declare (type (simple-vector 3) a b))
(v-norm2 (v- a b)))
(defun v*f (a f)
(declare (type (simple-vector 3) a))
(map 'vector #'(lambda (x) (* x f)) a))
(defun v-normal (a)
(declare (type (simple-vector 3) a))
(v*f a (/ (v-norm a))))
(defun v-dot (a b)
(declare (type (simple-vector 3) a b))
(reduce #'+ (map 'vector #'* a b)))
(defun v-cross (a b)
(declare (type (simple-vector 3) a b))
(let+ ((#(x1 x2 x3) a)
(#(y1 y2 y3) b))
(v3 (- (* x2 y3) (* x3 y2))
(- (* x3 y1) (* x1 y3))
(- (* x1 y2) (* x2 y1)))))