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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 2017-2020 David Thompson davet@gnu.org
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included in the section entitled "GNU
Free Documentation License".
A copy of the license is also available from the Free Software
Foundation Web site at http://www.gnu.org/licenses/fdl.html.
* Chickadee: (chickadee). Game programming toolkit for Guile.
The document was typeset with
http://www.texinfo.org/ (GNU Texinfo).
-->
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Vectors (The Chickadee Game Toolkit)</title>
<meta name="description" content="Vectors (The Chickadee Game Toolkit)">
<meta name="keywords" content="Vectors (The Chickadee Game Toolkit)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Index.html" rel="index" title="Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Math.html" rel="up" title="Math">
<link href="Rectangles.html" rel="next" title="Rectangles">
<link href="Basics.html" rel="prev" title="Basics">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
@media (min-width: 1140px) {
body {
margin-left: 14rem;
margin-right: 4rem;
max-width: 52rem;
}
}
@media (min-width: 800px) and (max-width: 1140px) {
body {
margin-left: 6rem;
margin-right: 4rem;
max-width: 52rem;
}
}
@media (max-width: 800px) {
body {
margin: 1rem;
}
}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://dthompson.us/css/dthompson.css">
</head>
<body lang="en">
<span id="Vectors"></span><div class="header">
<p>
Next: <a href="Rectangles.html" accesskey="n" rel="next">Rectangles</a>, Previous: <a href="Basics.html" accesskey="p" rel="prev">Basics</a>, Up: <a href="Math.html" accesskey="u" rel="up">Math</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Vectors-1"></span><h4 class="subsection">2.2.2 Vectors</h4>
<p>Unlike Scheme’s vector data type, which is a sequence of arbitrary
Scheme objects, Chickadee’s <code>(chickadee math vector)</code> module
provides vectors in the linear algebra sense: Sequences of numbers
specialized for particular coordinate spaces. As of now, Chickadee
provides 2D and 3D vectors, with 4D vector support coming in a future
release.
</p>
<p>Here’s a quick example of adding two vectors:
</p>
<div class="example">
<pre class="example">(define v (vec2+ (vec2 1 2) (vec2 3 4)))
</pre></div>
<p>Since vectors are used so frequently, the reader macro <code>#v</code> is
used to cut down on typing:
</p>
<div class="example">
<pre class="example">(define v (vec2+ #v(1 2) #v(3 4)))
</pre></div>
<p><em>A Note About Performance</em>
</p>
<p>A lot of time has been spent making Chickadee’s vector operations
perform relatively efficiently in critical code paths where excessive
garbage generation will cause major performance issues. The general
rule is that procedures ending with <code>!</code> perform an in-place
modification of one of the arguments in order to avoid allocating a
new vector. These procedures are also inlined by Guile’s compiler in
order to take advantage of optimizations relating to floating point
math operations. The downside is that since these are not pure
functions, they do not compose well and create more verbose code.
</p>
<span id="g_t2D-Vectors"></span><h4 class="subsubsection">2.2.2.1 2D Vectors</h4>
<dl>
<dt id="index-vec2">Procedure: <strong>vec2</strong> <em>x y</em></dt>
<dd><p>Return a new 2D vector with coordinates (<var>x</var>, <var>y</var>).
</p></dd></dl>
<dl>
<dt id="index-vec2_002fpolar">Procedure: <strong>vec2/polar</strong> <em>r theta</em></dt>
<dd><p>Return a new 2D vector containing the Cartesian representation of the
polar coordinate (<var>r</var>, <var>theta</var>). The angle <var>theta</var> is
measured in radians.
</p></dd></dl>
<dl>
<dt id="index-vec2_003f">Procedure: <strong>vec2?</strong> <em>obj</em></dt>
<dd><p>Return <code>#t</code> if <var>obj</var> is a 2D vector.
</p></dd></dl>
<dl>
<dt id="index-vec2_002dx">Procedure: <strong>vec2-x</strong> <em>v</em></dt>
<dd><p>Return the X coordinate of the 2D vector <var>v</var>.
</p></dd></dl>
<dl>
<dt id="index-vec2_002dy">Procedure: <strong>vec2-y</strong> <em>v</em></dt>
<dd><p>Return the Y coordinate of the 2D vector <var>v</var>.
</p></dd></dl>
<dl>
<dt id="index-vec2_002dcopy">Procedure: <strong>vec2-copy</strong> <em>v</em></dt>
<dd><p>Return a fresh copy of the 2D vector <var>v</var>.
</p></dd></dl>
<dl>
<dt id="index-vec2_002dmagnitude">Procedure: <strong>vec2-magnitude</strong> <em>v</em></dt>
<dd><p>Return the magnitude of the 2D vector <var>v</var>.
</p></dd></dl>
<dl>
<dt id="index-vec2_002ddot_002dproduct">Procedure: <strong>vec2-dot-product</strong> <em>v1 v2</em></dt>
<dd><p>Return the dot product of the 2D vectors <var>v1</var> and <var>v2</var>.
</p></dd></dl>
<dl>
<dt id="index-vec2_002dnormalize">Procedure: <strong>vec2-normalize</strong> <em>v</em></dt>
<dd><p>Return the normalized form of the 2D vector <var>v</var>.
</p></dd></dl>
<dl>
<dt id="index-vec2_002b">Procedure: <strong>vec2+</strong> <em>v x</em></dt>
<dd><p>Add <var>x</var>, either a 2D vector or a scalar (i.e. a real number), to
the 2D vector <var>v</var> and return a new vector containing the sum.
</p></dd></dl>
<dl>
<dt id="index-vec2_002d">Procedure: <strong>vec2-</strong> <em>v x</em></dt>
<dd><p>Subtract <var>x</var>, either a 2D vector or a scalar, from the 2D vector
<var>v</var> and return a new vector containing the difference.
</p></dd></dl>
<dl>
<dt id="index-vec2_002a">Procedure: <strong>vec2*</strong> <em>v x</em></dt>
<dd><p>Multiply the 2D vector <var>v</var> by <var>x</var>, a 2D vector or a scalar,
and return a new vector containing the product.
</p></dd></dl>
<dl>
<dt id="index-set_002dvec2_002dx_0021">Procedure: <strong>set-vec2-x!</strong> <em>v x</em></dt>
<dd><p>Set the X coordinate of the 2D vector <var>v</var> to <var>x</var>.
</p></dd></dl>
<dl>
<dt id="index-set_002dvec2_002dy_0021">Procedure: <strong>set-vec2-y!</strong> <em>v y</em></dt>
<dd><p>Set the Y coordinate of the 2D vector <var>v</var> to <var>y</var>.
</p></dd></dl>
<dl>
<dt id="index-set_002dvec2_0021">Procedure: <strong>set-vec2!</strong> <em>v x y</em></dt>
<dd><p>Set the X and Y coordinates of the 2D vector <var>v</var> to <var>x</var> and
<var>y</var>, respectively.
</p></dd></dl>
<dl>
<dt id="index-vec2_002dcopy_0021">Procedure: <strong>vec2-copy!</strong> <em>source target</em></dt>
<dd><p>Copy the 2D vector <var>source</var> into the 2D vector <var>target</var>.
</p></dd></dl>
<dl>
<dt id="index-vec2_002dadd_0021">Procedure: <strong>vec2-add!</strong> <em>v x</em></dt>
<dd><p>Perform an in-place modification of the 2D vector <var>v</var> by adding
<var>x</var>, a 2D vector or a scalar.
</p></dd></dl>
<dl>
<dt id="index-vec2_002dsub_0021">Procedure: <strong>vec2-sub!</strong> <em>v x</em></dt>
<dd><p>Perform an in-place modification of the 2D vector <var>v</var> by
subtracting <var>x</var>, a 2D vector or a scalar.
</p></dd></dl>
<dl>
<dt id="index-vec2_002dmult_0021">Procedure: <strong>vec2-mult!</strong> <em>v x</em></dt>
<dd><p>Perform an in-place modification of the 2D vector <var>v</var> by
multiplying it by <var>x</var>, a 2D vector or a scalar.
</p></dd></dl>
<span id="g_t3D-Vectors"></span><h4 class="subsubsection">2.2.2.2 3D Vectors</h4>
<dl>
<dt id="index-vec3">Procedure: <strong>vec3</strong> <em>x y</em></dt>
<dd><p>Return a new 2D vector with coordinates (<var>x</var>, <var>y</var>).
</p></dd></dl>
<dl>
<dt id="index-vec3_003f">Procedure: <strong>vec3?</strong> <em>obj</em></dt>
<dd><p>Return <code>#t</code> if <var>obj</var> is a 3D vector.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dx">Procedure: <strong>vec3-x</strong> <em>v</em></dt>
<dd><p>Return the X coordinate of the 3D vector <var>v</var>.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dy">Procedure: <strong>vec3-y</strong> <em>v</em></dt>
<dd><p>Return the Y coordinate of the 3D vector <var>v</var>.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dz">Procedure: <strong>vec3-z</strong> <em>v</em></dt>
<dd><p>Return the Z coordinate of the 3D vector <var>v</var>.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dcopy">Procedure: <strong>vec3-copy</strong> <em>v</em></dt>
<dd><p>Return a fresh copy of the 3D vector <var>v</var>.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dmagnitude">Procedure: <strong>vec3-magnitude</strong> <em>v</em></dt>
<dd><p>Return the magnitude of the 3D vector <var>v</var>.
</p></dd></dl>
<dl>
<dt id="index-vec3_002ddot_002dproduct">Procedure: <strong>vec3-dot-product</strong> <em>v1 v2</em></dt>
<dd><p>Return the dot product of the 3D vectors <var>v1</var> and <var>v2</var>.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dcross">Procedure: <strong>vec3-cross</strong> <em>v1 v2</em></dt>
<dd><p>Return a new 3D vector containing the cross product of <var>v1</var> and
<var>v2</var>.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dnormalize">Procedure: <strong>vec3-normalize</strong> <em>v</em></dt>
<dd><p>Return the normalized form of the 3D vector <var>v</var>.
</p></dd></dl>
<dl>
<dt id="index-vec3_002b">Procedure: <strong>vec3+</strong> <em>v x</em></dt>
<dd><p>Add <var>x</var>, either a 3D vector or a scalar (i.e. a real number), to
the 3D vector <var>v</var> and return a new vector containing the sum.
</p></dd></dl>
<dl>
<dt id="index-vec3_002d">Procedure: <strong>vec3-</strong> <em>v x</em></dt>
<dd><p>Subtract <var>x</var>, either a 3D vector or a scalar, from the 3D vector
<var>v</var> and return a new vector containing the difference.
</p></dd></dl>
<dl>
<dt id="index-vec3_002a">Procedure: <strong>vec3*</strong> <em>v x</em></dt>
<dd><p>Multiply the 3D vector <var>v</var> by <var>x</var>, a 3D vector or a scalar,
and return a new vector containing the product.
</p></dd></dl>
<dl>
<dt id="index-set_002dvec3_002dx_0021">Procedure: <strong>set-vec3-x!</strong> <em>v x</em></dt>
<dd><p>Set the X coordinate of the 3D vector <var>v</var> to <var>x</var>.
</p></dd></dl>
<dl>
<dt id="index-set_002dvec3_002dy_0021">Procedure: <strong>set-vec3-y!</strong> <em>v y</em></dt>
<dd><p>Set the Y coordinate of the 3D vector <var>v</var> to <var>y</var>.
</p></dd></dl>
<dl>
<dt id="index-set_002dvec3_002dz_0021">Procedure: <strong>set-vec3-z!</strong> <em>v z</em></dt>
<dd><p>Set the Z coordinate of the 3D vector <var>v</var> to <var>z</var>.
</p></dd></dl>
<dl>
<dt id="index-set_002dvec3_0021">Procedure: <strong>set-vec3!</strong> <em>v x y z</em></dt>
<dd><p>Set the X, Y, and Z coordinates of the 3D vector <var>v</var> to <var>x</var>,
<var>y</var>, and <var>z</var>, respectively.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dcopy_0021">Procedure: <strong>vec3-copy!</strong> <em>source target</em></dt>
<dd><p>Copy the 3D vector <var>source</var> into the 3D vector <var>target</var>.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dadd_0021">Procedure: <strong>vec3-add!</strong> <em>v x</em></dt>
<dd><p>Perform an in-place modification of the 3D vector <var>v</var> by adding
<var>x</var>, a 3D vector or a scalar.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dsub_0021">Procedure: <strong>vec3-sub!</strong> <em>v x</em></dt>
<dd><p>Perform an in-place modification of the 3D vector <var>v</var> by
subtracting <var>x</var>, a 3D vector or a scalar.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dmult_0021">Procedure: <strong>vec3-mult!</strong> <em>v x</em></dt>
<dd><p>Perform an in-place modification of the 3D vector <var>v</var> by
multiplying it by <var>x</var>, a 3D vector or a scalar.
</p></dd></dl>
<dl>
<dt id="index-vec3_002dcross_0021">Procedure: <strong>vec3-cross!</strong> <em>dest v1 v2</em></dt>
<dd><p>Compute the cross product of the 3D vectors <var>v1</var> and <var>v2</var> and
store the result in <var>dest</var>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Rectangles.html" accesskey="n" rel="next">Rectangles</a>, Previous: <a href="Basics.html" accesskey="p" rel="prev">Basics</a>, Up: <a href="Math.html" accesskey="u" rel="up">Math</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|