-
Notifications
You must be signed in to change notification settings - Fork 4
/
vec2.xtm
138 lines (109 loc) · 3 KB
/
vec2.xtm
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
;; -------VECTOR-------
;; functions to work with 2d vectors
(sys:load "libs/core/math.xtm")
(bind-type vec2 <float,float>)
(bind-func print:[void,vec2*]*
(lambda (p)
(if (null? p)
(begin (printout "<>") void)
(begin (printout "<"
(tref p 0) ","
(tref p 1) ">")
void))))
;; getter and setters
(bind-func getx
(lambda (v1:vec2*)
(tref v1 0)))
(bind-func setx
(lambda (v1:vec2* val:float)
(tset! v1 0 val)))
(bind-func gety
(lambda (v1:vec2*)
(tref v1 1)))
(bind-func sety
(lambda (v1:vec2* val:float)
(tset! v1 1 val)))
(bind-func v2set
(lambda (v1:vec2* x:float y:float)
(tset! v1 0 x)
(tset! v1 1 y)))
(bind-func v2set
(lambda (v1:vec2* v2:vec2*)
(tset! v1 0 (tref v2 0))
(tset! v1 1 (tref v2 1))))
;;vector arithmetic return value
(bind-func v2add
(lambda (v1:vec2* v2:vec2*)
(let ((sum:vec2* (vec2
(+ (getx v1) (getx v2))
(+ (gety v1) (gety v2)))))
sum)))
(bind-func v2sub
(lambda (v1:vec2* v2:vec2*)
(let ((diff:vec2* (vec2
(- (getx v1) (getx v2))
(- (gety v1) (gety v2)))))
diff)))
(bind-func xtm_division:[vec2*,vec2*,float]*
(lambda (v1 d)
(let ((vdiv:vec2* (zalloc)))
(v2set vdiv (/ (getx v1) d) (/ (gety v1) d))
vdiv)))
;;vector arithmetic mutable
(bind-func v2inc
(lambda (v1:vec2* v2:vec2*)
(setx v1 (+ (getx v1) (getx v2)))
(sety v1 (+ (gety v1) (gety v2)))))
(bind-func v2dec
(lambda (v1:vec2* v2:vec2*)
(setx v1 (- (getx v1) (getx v2)))
(sety v1 (- (gety v1) (gety v2)))))
(bind-func v2scale
(lambda (v1:vec2* scale:float)
(tset! v1 0 (* (tref v1 0) scale))
(tset! v1 1 (* (tref v1 1) scale))))
(bind-func v2div
(lambda (v1:vec2* scale:float)
(tset! v1 0 (/ (tref v1 0) scale))
(tset! v1 1 (/ (tref v1 1) scale))))
(bind-func magnitude
(lambda (v1:vec2*)
(let ((vec:float* (salloc 2)))
(pfill! vec (getx v1) (gety v1))
(vmag vec 2))))
(bind-func normalize
(lambda (v1:vec2*)
(let ((vec:float* (salloc 2)))
(pfill! vec (getx v1) (gety v1))
(vnormalise vec 2 vec)
(v2set v1 (pref vec 0) (pref vec 1)))))
(bind-func limit:[void,vec2*,float]*
(lambda (v1:vec2* lim:float)
(if (> (magnitude v1) lim)
(begin
(normalize v1)
(v2scale v1 lim)))
void))
;(bind-func constrain:[void,vec2*,float,float]*
; (lambda (v1:vec2* min:float max:float)
; (cond
; ((> (magnitude v1) max)
; (begin
; (normalize v1)
; (v2scale v1 max)))
; ((< (magnitude v1) min)
; (begin
; (normalize v1)
; (v2scale v1 min))))
; void))
;; functions
(bind-func distance:[float,float,float,float,float]*
(lambda (x1 y1 x2 y2)
(sqrt (+
(* (- x2 x1) (- x2 x1))
(* (- y2 y1) (- y2 y1))))))
(bind-func distance
(lambda (v1:vec2* v2:vec2*)
(sqrt (+
(* (- (getx v2) (getx v1)) (- (getx v2) (getx v1)))
(* (- (gety v2) (gety v1)) (- (gety v2) (gety v1)))))))