~pygame/pygame/trunk

« back to all changes in this revision

Viewing changes to docs/reST/ref/math.rst

  • Committer: pygame
  • Date: 2017-01-10 00:31:42 UTC
  • Revision ID: git-v1:2eea4f299a2e791f884608d7ed601558634af73c
commit 1639c41a8cb3433046882ede92c80ce69d59016b
Author: Thomas Kluyver <takowl@gmail.com>
Date:   Sun Jan 8 18:46:46 2017 +0000

    Build newer versions of libogg and libvorbis into Linux base images

    Closes #317
    Closes #323

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.. include:: common.txt
 
2
 
 
3
:mod:`pygame.math`
 
4
==================
 
5
 
 
6
.. module:: pygame.math
 
7
   :synopsis: pygame module for vector classes
 
8
 
 
9
| :sl:`pygame module for vector classes`
 
10
 
 
11
!!!EXPERIMENTAL!!! Note: This Modul is still in development and the ``API``
 
12
might change. Please report bug and suggestions to pygame-users@seul.org
 
13
 
 
14
The pygame math module currently provides Vector classes in two and three
 
15
dimensions, Vector2 and Vector3 respectively.
 
16
 
 
17
They support the following numerical operations: vec+vec, vec-vec, vec*number,
 
18
number*vec, vec/number, vec//number, vec+=vec, vec-=vec, vec*=number,
 
19
vec/=number, vec//=number. All these operations will be performed elementwise.
 
20
In addition vec*vec will perform a scalar-product (a.k.a. dot-product). If you
 
21
want to multiply every element from vector v with every element from vector w
 
22
you can use the elementwise method: ``v.elementwise()`` ``\*`` w
 
23
 
 
24
New in Pygame 1.10
 
25
 
 
26
.. function:: enable_swizzling
 
27
 
 
28
   | :sl:`globally enables swizzling for vectors.`
 
29
   | :sg:`enable_swizzling() -> None`
 
30
 
 
31
   Enables swizzling for all vectors until ``disable_swizzling()`` is called.
 
32
   By default swizzling is disabled.
 
33
 
 
34
   .. ## pygame.math.enable_swizzling ##
 
35
 
 
36
.. function:: disable_swizzling
 
37
 
 
38
   | :sl:`globally disables swizzling for vectors.`
 
39
   | :sg:`disable_swizzling() -> None`
 
40
 
 
41
   Disables swizzling for all vectors until ``enable_swizzling()`` is called.
 
42
   By default swizzling is disabled.
 
43
 
 
44
   .. ## pygame.math.disable_swizzling ##
 
45
 
 
46
.. class:: Vector2
 
47
 
 
48
   | :sl:`a 2-Dimensional Vector`
 
49
   | :sg:`Vector2() -> Vector2`
 
50
   | :sg:`Vector2(Vector2) -> Vector2`
 
51
   | :sg:`Vector2(x, y) -> Vector2`
 
52
   | :sg:`Vector2((x, y)) -> Vector2`
 
53
 
 
54
   Some general information about the Vector2 class.
 
55
 
 
56
   .. method:: dot
 
57
 
 
58
      | :sl:`calculates the dot- or scalar-product with the other vector`
 
59
      | :sg:`dot(Vector2) -> float`
 
60
 
 
61
      .. ## Vector2.dot ##
 
62
 
 
63
   .. method:: cross
 
64
 
 
65
      | :sl:`calculates the cross- or vector-product`
 
66
      | :sg:`cross(Vector2) -> float`
 
67
 
 
68
      calculates the third component of the cross-product.
 
69
 
 
70
      .. ## Vector2.cross ##
 
71
 
 
72
   .. method:: length
 
73
 
 
74
      | :sl:`returns the euclidic length of the vector.`
 
75
      | :sg:`length() -> float`
 
76
 
 
77
      calculates the euclidic length of the vector which follows from the
 
78
      Pythagorean theorem: ``vec.length()`` ==
 
79
      ``math.sqrt(vec.x**2 + vec.y**2)``
 
80
 
 
81
      .. ## Vector2.length ##
 
82
 
 
83
   .. method:: length_squared
 
84
 
 
85
      | :sl:`returns the squared euclidic length of the vector.`
 
86
      | :sg:`length_squared() -> float`
 
87
 
 
88
      calculates the euclidic length of the vector which follows from the
 
89
      Pythagorean theorem: ``vec.length_squared()`` == vec.x**2 + vec.y**2 This
 
90
      is faster than ``vec.length()`` because it avoids the square root.
 
91
 
 
92
      .. ## Vector2.length_squared ##
 
93
 
 
94
   .. method:: normalize
 
95
 
 
96
      | :sl:`returns a vector with the same direction but length 1.`
 
97
      | :sg:`normalize() -> Vector2`
 
98
 
 
99
      Returns a new vector that has length == 1 and the same direction as self.
 
100
 
 
101
      .. ## Vector2.normalize ##
 
102
 
 
103
   .. method:: normalize_ip
 
104
 
 
105
      | :sl:`normalizes the vector in place so that its length is 1.`
 
106
      | :sg:`normalize_ip() -> None`
 
107
 
 
108
      Normalizes the vector so that it has length == 1. The direction of the
 
109
      vector is not changed.
 
110
 
 
111
      .. ## Vector2.normalize_ip ##
 
112
 
 
113
   .. method:: is_normalized
 
114
 
 
115
      | :sl:`tests if the vector is normalized i.e. has length == 1.`
 
116
      | :sg:`is_normalized() -> Bool`
 
117
 
 
118
      Returns True if the vector has length == 1. Otherwise it returns False.
 
119
 
 
120
      .. ## Vector2.is_normalized ##
 
121
 
 
122
   .. method:: scale_to_length
 
123
 
 
124
      | :sl:`scales the vector to a given length.`
 
125
      | :sg:`scale_to_length(float) -> None`
 
126
 
 
127
      Scales the vector so that it has the given length. The direction of the
 
128
      vector is not changed. You can also scale to length 0. If the vector is
 
129
      the zero vector (i.e. has length 0 thus no direction) an
 
130
      ZeroDivisionError is raised.
 
131
 
 
132
      .. ## Vector2.scale_to_length ##
 
133
 
 
134
   .. method:: reflect
 
135
 
 
136
      | :sl:`returns a vector reflected of a given normal.`
 
137
      | :sg:`reflect(Vector2) -> Vector2`
 
138
 
 
139
      Returns a new vector that points in the direction as if self would bounce
 
140
      of a surface characterized by the given surface normal. The length of the
 
141
      new vector is the same as self's.
 
142
 
 
143
      .. ## Vector2.reflect ##
 
144
 
 
145
   .. method:: reflect_ip
 
146
 
 
147
      | :sl:`reflect the vector of a given normal in place.`
 
148
      | :sg:`reflect_ip(Vector2) -> None`
 
149
 
 
150
      Changes the direction of self as if it would have been reflected of a
 
151
      surface with the given surface normal.
 
152
 
 
153
      .. ## Vector2.reflect_ip ##
 
154
 
 
155
   .. method:: distance_to
 
156
 
 
157
      | :sl:`calculates the euclidic distance to a given vector.`
 
158
      | :sg:`distance_to(Vector2) -> float`
 
159
 
 
160
      .. ## Vector2.distance_to ##
 
161
 
 
162
   .. method:: distance_squared_to
 
163
 
 
164
      | :sl:`calculates the squared euclidic distance to a given vector.`
 
165
      | :sg:`distance_squared_to(Vector2) -> float`
 
166
 
 
167
      .. ## Vector2.distance_squared_to ##
 
168
 
 
169
   .. method:: lerp
 
170
 
 
171
      | :sl:`returns a linear interpolation to the given vector.`
 
172
      | :sg:`lerp(Vector2, float) -> Vector2`
 
173
 
 
174
      Returns a Vector which is a linear interpolation between self and the
 
175
      given Vector. The second parameter determines how far between self an
 
176
      other the result is going to be. It must be a value between 0 and 1 where
 
177
      0 means self an 1 means other will be returned.
 
178
 
 
179
      .. ## Vector2.lerp ##
 
180
 
 
181
   .. method:: slerp
 
182
 
 
183
      | :sl:`returns a spherical interpolation to the given vector.`
 
184
      | :sg:`slerp(Vector2, float) -> Vector2`
 
185
 
 
186
      Calculates the spherical interpolation from self to the given Vector. The
 
187
      second argument - often called t - must be in the range [-1, 1]. It
 
188
      parametrizes where - in between the two vectors - the result should be.
 
189
      If a negative value is given the interpolation will not take the
 
190
      complement of the shortest path.
 
191
 
 
192
      .. ## Vector2.slerp ##
 
193
 
 
194
   .. method:: elementwise
 
195
 
 
196
      | :sl:`The next operation will be performed elementwize.`
 
197
      | :sg:`elementwise() -> VectorElementwizeProxy`
 
198
 
 
199
      Applies the following operation to each element of the vector.
 
200
 
 
201
      .. ## Vector2.elementwise ##
 
202
 
 
203
   .. method:: rotate
 
204
 
 
205
      | :sl:`rotates a vector by a given angle in degrees.`
 
206
      | :sg:`rotate(float) -> Vector2`
 
207
 
 
208
      Returns a vector which has the same length as self but is rotated
 
209
      counterclockwise by the given angle in degrees.
 
210
 
 
211
      .. ## Vector2.rotate ##
 
212
 
 
213
   .. method:: rotate_ip
 
214
 
 
215
      | :sl:`rotates the vector by a given angle in degrees in place.`
 
216
      | :sg:`rotate_ip(float) -> None`
 
217
 
 
218
      Rotates the vector counterclockwise by the given angle in degrees. The
 
219
      length of the vector is not changed.
 
220
 
 
221
      .. ## Vector2.rotate_ip ##
 
222
 
 
223
   .. method:: angle_to
 
224
 
 
225
      | :sl:`calculates the angle to a given vector in degrees.`
 
226
      | :sg:`angle_to(Vector2) -> float`
 
227
 
 
228
      Returns the angle between self and the given vector.
 
229
 
 
230
      .. ## Vector2.angle_to ##
 
231
 
 
232
   .. method:: as_polar
 
233
 
 
234
      | :sl:`returns a tuple with radial distance and azimuthal angle.`
 
235
      | :sg:`as_polar() -> (r, phi)`
 
236
 
 
237
      Returns a tuple (r, phi) where r is the radial distance, and phi is the
 
238
      azimuthal angle.
 
239
 
 
240
      .. ## Vector2.as_polar ##
 
241
 
 
242
   .. method:: from_polar
 
243
 
 
244
      | :sl:`Sets x and y from a polar coordinates tuple.`
 
245
      | :sg:`from_polar((r, phi)) -> None`
 
246
 
 
247
      Sets x and y from a tuple (r, phi) where r is the radial distance, and
 
248
      phi is the azimuthal angle.
 
249
 
 
250
      .. ## Vector2.from_polar ##
 
251
 
 
252
   .. ## pygame.math.Vector2 ##
 
253
 
 
254
.. class:: Vector3
 
255
 
 
256
   | :sl:`a 3-Dimensional Vector`
 
257
   | :sg:`Vector3() -> Vector3`
 
258
   | :sg:`Vector3(Vector3) -> Vector3`
 
259
   | :sg:`Vector3(x, y, z) -> Vector3`
 
260
   | :sg:`Vector3((x, y, z)) -> Vector3`
 
261
 
 
262
   Some general information about the Vector3 class.
 
263
 
 
264
   .. method:: dot
 
265
 
 
266
      | :sl:`calculates the dot- or scalar-product with the other vector`
 
267
      | :sg:`dot(Vector3) -> float`
 
268
 
 
269
      .. ## Vector3.dot ##
 
270
 
 
271
   .. method:: cross
 
272
 
 
273
      | :sl:`calculates the cross- or vector-product`
 
274
      | :sg:`cross(Vector3) -> float`
 
275
 
 
276
      calculates the cross-product.
 
277
 
 
278
      .. ## Vector3.cross ##
 
279
 
 
280
   .. method:: length
 
281
 
 
282
      | :sl:`returns the euclidic length of the vector.`
 
283
      | :sg:`length() -> float`
 
284
 
 
285
      calculates the euclidic length of the vector which follows from the
 
286
      Pythagorean theorem: ``vec.length()`` ==
 
287
      ``math.sqrt(vec.x**2 + vec.y**2 + vec.z**2)``
 
288
 
 
289
      .. ## Vector3.length ##
 
290
 
 
291
   .. method:: length_squared
 
292
 
 
293
      | :sl:`returns the squared euclidic length of the vector.`
 
294
      | :sg:`length_squared() -> float`
 
295
 
 
296
      calculates the euclidic length of the vector which follows from the
 
297
      Pythagorean theorem: ``vec.length_squared()`` == vec.x**2 + vec.y**2 +
 
298
      vec.z**2 This is faster than ``vec.length()`` because it avoids the
 
299
      square root.
 
300
 
 
301
      .. ## Vector3.length_squared ##
 
302
 
 
303
   .. method:: normalize
 
304
 
 
305
      | :sl:`returns a vector with the same direction but length 1.`
 
306
      | :sg:`normalize() -> Vector3`
 
307
 
 
308
      Returns a new vector that has length == 1 and the same direction as self.
 
309
 
 
310
      .. ## Vector3.normalize ##
 
311
 
 
312
   .. method:: normalize_ip
 
313
 
 
314
      | :sl:`normalizes the vector in place so that its length is 1.`
 
315
      | :sg:`normalize_ip() -> None`
 
316
 
 
317
      Normalizes the vector so that it has length == 1. The direction of the
 
318
      vector is not changed.
 
319
 
 
320
      .. ## Vector3.normalize_ip ##
 
321
 
 
322
   .. method:: is_normalized
 
323
 
 
324
      | :sl:`tests if the vector is normalized i.e. has length == 1.`
 
325
      | :sg:`is_normalized() -> Bool`
 
326
 
 
327
      Returns True if the vector has length == 1. Otherwise it returns False.
 
328
 
 
329
      .. ## Vector3.is_normalized ##
 
330
 
 
331
   .. method:: scale_to_length
 
332
 
 
333
      | :sl:`scales the vector to a given length.`
 
334
      | :sg:`scale_to_length(float) -> None`
 
335
 
 
336
      Scales the vector so that it has the given length. The direction of the
 
337
      vector is not changed. You can also scale to length 0. If the vector is
 
338
      the zero vector (i.e. has length 0 thus no direction) an
 
339
      ZeroDivisionError is raised.
 
340
 
 
341
      .. ## Vector3.scale_to_length ##
 
342
 
 
343
   .. method:: reflect
 
344
 
 
345
      | :sl:`returns a vector reflected of a given normal.`
 
346
      | :sg:`reflect(Vector3) -> Vector3`
 
347
 
 
348
      Returns a new vector that points in the direction as if self would bounce
 
349
      of a surface characterized by the given surface normal. The length of the
 
350
      new vector is the same as self's.
 
351
 
 
352
      .. ## Vector3.reflect ##
 
353
 
 
354
   .. method:: reflect_ip
 
355
 
 
356
      | :sl:`reflect the vector of a given normal in place.`
 
357
      | :sg:`reflect_ip(Vector3) -> None`
 
358
 
 
359
      Changes the direction of self as if it would have been reflected of a
 
360
      surface with the given surface normal.
 
361
 
 
362
      .. ## Vector3.reflect_ip ##
 
363
 
 
364
   .. method:: distance_to
 
365
 
 
366
      | :sl:`calculates the euclidic distance to a given vector.`
 
367
      | :sg:`distance_to(Vector3) -> float`
 
368
 
 
369
      .. ## Vector3.distance_to ##
 
370
 
 
371
   .. method:: distance_squared_to
 
372
 
 
373
      | :sl:`calculates the squared euclidic distance to a given vector.`
 
374
      | :sg:`distance_squared_to(Vector3) -> float`
 
375
 
 
376
      .. ## Vector3.distance_squared_to ##
 
377
 
 
378
   .. method:: lerp
 
379
 
 
380
      | :sl:`returns a linear interpolation to the given vector.`
 
381
      | :sg:`lerp(Vector3, float) -> Vector3`
 
382
 
 
383
      Returns a Vector which is a linear interpolation between self and the
 
384
      given Vector. The second parameter determines how far between self an
 
385
      other the result is going to be. It must be a value between 0 and 1 where
 
386
      0 means self an 1 means other will be returned.
 
387
 
 
388
      .. ## Vector3.lerp ##
 
389
 
 
390
   .. method:: slerp
 
391
 
 
392
      | :sl:`returns a spherical interpolation to the given vector.`
 
393
      | :sg:`slerp(Vector3, float) -> Vector3`
 
394
 
 
395
      Calculates the spherical interpolation from self to the given Vector. The
 
396
      second argument - often called t - must be in the range [-1, 1]. It
 
397
      parametrizes where - in between the two vectors - the result should be.
 
398
      If a negative value is given the interpolation will not take the
 
399
      complement of the shortest path.
 
400
 
 
401
      .. ## Vector3.slerp ##
 
402
 
 
403
   .. method:: elementwise
 
404
 
 
405
      | :sl:`The next operation will be performed elementwize.`
 
406
      | :sg:`elementwise() -> VectorElementwizeProxy`
 
407
 
 
408
      Applies the following operation to each element of the vector.
 
409
 
 
410
      .. ## Vector3.elementwise ##
 
411
 
 
412
   .. method:: rotate
 
413
 
 
414
      | :sl:`rotates a vector by a given angle in degrees.`
 
415
      | :sg:`rotate(Vector3, float) -> Vector3`
 
416
 
 
417
      Returns a vector which has the same length as self but is rotated
 
418
      counterclockwise by the given angle in degrees around the given axis.
 
419
 
 
420
      .. ## Vector3.rotate ##
 
421
 
 
422
   .. method:: rotate_ip
 
423
 
 
424
      | :sl:`rotates the vector by a given angle in degrees in place.`
 
425
      | :sg:`rotate_ip(Vector3, float) -> None`
 
426
 
 
427
      Rotates the vector counterclockwise around the given axis by the given
 
428
      angle in degrees. The length of the vector is not changed.
 
429
 
 
430
      .. ## Vector3.rotate_ip ##
 
431
 
 
432
   .. method:: rotate_x
 
433
 
 
434
      | :sl:`rotates a vector around the x-axis by the angle in degrees.`
 
435
      | :sg:`rotate_x(float) -> Vector3`
 
436
 
 
437
      Returns a vector which has the same length as self but is rotated
 
438
      counterclockwise around the x-axis by the given angle in degrees.
 
439
 
 
440
      .. ## Vector3.rotate_x ##
 
441
 
 
442
   .. method:: rotate_x_ip
 
443
 
 
444
      | :sl:`rotates the vector around the x-axis by the angle in degrees in place.`
 
445
      | :sg:`rotate_x_ip(float) -> None`
 
446
 
 
447
      Rotates the vector counterclockwise around the x-axis by the given angle
 
448
      in degrees. The length of the vector is not changed.
 
449
 
 
450
      .. ## Vector3.rotate_x_ip ##
 
451
 
 
452
   .. method:: rotate_y
 
453
 
 
454
      | :sl:`rotates a vector around the y-axis by the angle in degrees.`
 
455
      | :sg:`rotate_y(float) -> Vector3`
 
456
 
 
457
      Returns a vector which has the same length as self but is rotated
 
458
      counterclockwise around the y-axis by the given angle in degrees.
 
459
 
 
460
      .. ## Vector3.rotate_y ##
 
461
 
 
462
   .. method:: rotate_y_ip
 
463
 
 
464
      | :sl:`rotates the vector around the y-axis by the angle in degrees in place.`
 
465
      | :sg:`rotate_y_ip(float) -> None`
 
466
 
 
467
      Rotates the vector counterclockwise around the y-axis by the given angle
 
468
      in degrees. The length of the vector is not changed.
 
469
 
 
470
      .. ## Vector3.rotate_y_ip ##
 
471
 
 
472
   .. method:: rotate_z
 
473
 
 
474
      | :sl:`rotates a vector around the z-axis by the angle in degrees.`
 
475
      | :sg:`rotate_z(float) -> Vector3`
 
476
 
 
477
      Returns a vector which has the same length as self but is rotated
 
478
      counterclockwise around the z-axis by the given angle in degrees.
 
479
 
 
480
      .. ## Vector3.rotate_z ##
 
481
 
 
482
   .. method:: rotate_z_ip
 
483
 
 
484
      | :sl:`rotates the vector around the z-axis by the angle in degrees in place.`
 
485
      | :sg:`rotate_z_ip(float) -> None`
 
486
 
 
487
      Rotates the vector counterclockwise around the z-axis by the given angle
 
488
      in degrees. The length of the vector is not changed.
 
489
 
 
490
      .. ## Vector3.rotate_z_ip ##
 
491
 
 
492
   .. method:: angle_to
 
493
 
 
494
      | :sl:`calculates the angle to a given vector in degrees.`
 
495
      | :sg:`angle_to(Vector3) -> float`
 
496
 
 
497
      Returns the angle between self and the given vector.
 
498
 
 
499
      .. ## Vector3.angle_to ##
 
500
 
 
501
   .. method:: as_spherical
 
502
 
 
503
      | :sl:`returns a tuple with radial distance, inclination and azimuthal angle.`
 
504
      | :sg:`as_spherical() -> (r, theta, phi)`
 
505
 
 
506
      Returns a tuple (r, theta, phi) where r is the radial distance, theta is
 
507
      the inclination angle and phi is the azimuthal angle.
 
508
 
 
509
      .. ## Vector3.as_spherical ##
 
510
 
 
511
   .. method:: from_spherical
 
512
 
 
513
      | :sl:`Sets x, y and z from a spherical coordinates 3-tuple.`
 
514
      | :sg:`from_spherical((r, theta, phi)) -> None`
 
515
 
 
516
      Sets x, y and z from a tuple (r, theta, phi) where r is the radial
 
517
      distance, theta is the inclination angle and phi is the azimuthal angle.
 
518
 
 
519
      .. ## Vector3.from_spherical ##
 
520
 
 
521
   .. ##  ##
 
522
 
 
523
   .. ## pygame.math.Vector3 ##
 
524
 
 
525
.. ## pygame.math ##