Source

pygame / src / math.c

  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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/*
  pygame - Python Game Library

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#define PYGAMEAPI_MATH_INTERNAL
#include "pygame.h"
#include "pygamedocs.h"
#include "structmember.h"
#include "pgcompat.h"
#include <float.h>
#include <math.h>


static PyTypeObject PyVector2_Type;
#define PyVector2_Check(x) ((x)->ob_type == &PyVector2_Type)
#define PyVector_Check(x) (PyVector2_Check(x))



/********************************
 * Global helper functions
 ********************************/

static int
checkRealNumber(PyObject *obj)
{
    if (PyNumber_Check(obj)) {
        if (!PyComplex_Check(obj)) {
            return 1;
        }
    }
    return 0;
}

static double
PySequence_GetItem_AsDouble(PyObject *seq, Py_ssize_t index)
{
    PyObject *item;
    double value;
    assert(PySequence_Check(seq));
    item = PySequence_GetItem(seq, index);
    value = PyFloat_AsDouble(item);
    Py_XDECREF(item);
    return value;
}

static int 
checkPyVectorCompatible(PyObject *obj, int dim)
{
    int i;
    PyObject *tmp;

    switch(dim) {
    case 2:
        if (PyVector2_Check(obj)) {
            return 1;
        }
        break;
/*
    case 3:
        if (PyVector3d_Check(obj)) {
            return 1;
        }
        break;
    case 4:
        if (PyVector4d_Check(obj)) {
            return 1;
        }
        break;
*/
    default:
        PyErr_SetString(PyExc_SystemError, 
                        "Wrong internal call to checkPyVectorCompatible.");
        return 0;
    }

    if (!PySequence_Check(obj) || (PySequence_Length(obj) != dim)) {
        return 0;
    }

    for (i = 0; i < dim; ++i) {
        tmp = PySequence_GetItem(obj, i);
        if (!checkRealNumber(tmp)) {
            Py_DECREF(tmp);
            return 0;
        }
        Py_DECREF(tmp);
    }
    return 1;
}


static PyMemberDef vector_members[] = {
    {"epsilon", T_DOUBLE, offsetof(PyVector, epsilon), 0,
     "small value used in comparisons"},
    {NULL}  /* Sentinel */
};


static PyObject*
PyVector_NEW(int dim)
{
    PyVector *vec;
    switch (dim) {
    case 2:
        vec = PyObject_New(PyVector, &PyVector2_Type);
        break;
/*
    case 3:
        vec = PyObject_New(PyVector, &PyVector3_Type);
        break;
    case 4:
        vec = PyObject_New(PyVector, &PyVector4_Type);
        break;
*/
    default:
        PyErr_SetString(PyExc_SystemError, 
                        "Wrong internal call to PyVector_NEW.\n");
        return NULL;
    }

    if (vec != NULL) {
        vec->dim = dim;
        vec->epsilon = FLT_EPSILON;
        vec->coords = PyMem_New(double, dim);
        if (vec->coords == NULL) {
            Py_DECREF(vec);
            return PyErr_NoMemory();
        }
    }

    return (PyObject *)vec;
}

static void
vector_dealloc(PyVector* self)
{
    PyMem_Del(self->coords);
    self->ob_type->tp_free((PyObject*)self);
}






/**********************************************
 * Generic vector PyNumber emulation routines
 **********************************************/


static PyObject *
vector_add(PyObject *o1, PyObject *o2)
{
    int i;
    if (PyVector_Check(o1)) {
        int dim = ((PyVector*)o1)->dim;
        if (checkPyVectorCompatible(o2, dim)) {
            PyVector *ret = (PyVector*)PyVector_NEW(dim);
            for (i = 0; i < dim; i++) {
                ret->coords[i] = ((PyVector*)o1)->coords[i] + PySequence_GetItem_AsDouble(o2, i);
            }
            return (PyObject*)ret;
        }
    }
    else {
        int dim = ((PyVector*)o2)->dim;
        if (checkPyVectorCompatible(o1, dim)) {
            PyVector *ret = (PyVector*)PyVector_NEW(dim);
            for (i = 0; i < dim; i++) {
                ret->coords[i] = PySequence_GetItem_AsDouble(o1, i) + ((PyVector*)o2)->coords[i];
            }
            return (PyObject*)ret;
        }
    }
     
    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}

static PyObject *
vector_inplace_add(PyVector *self, PyObject *other)
{
    int i;
    if (checkPyVectorCompatible(other, self->dim)) {
        for (i = 0; i < self->dim; i++) {
            self->coords[i] += PySequence_GetItem_AsDouble(other, i);
        }
        Py_INCREF(self);
        return (PyObject*)self;
    }
    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}


static PyObject *
vector_sub(PyObject *o1, PyObject *o2)
{
    int i;
    if (PyVector_Check(o1)) {
        int dim = ((PyVector*)o1)->dim;
        if (checkPyVectorCompatible(o2, dim)) {
            PyVector *ret = (PyVector*)PyVector_NEW(dim);
            for (i = 0; i < dim; i++) {
                ret->coords[i] = ((PyVector*)o1)->coords[i] - PySequence_GetItem_AsDouble(o2, i);
            }
            return (PyObject*)ret;
        }
    }
    else {
        int dim = ((PyVector*)o2)->dim;
        if (checkPyVectorCompatible(o1, dim)) {
            PyVector *ret = (PyVector*)PyVector_NEW(dim);
            for (i = 0; i < dim; i++) {
                ret->coords[i] = PySequence_GetItem_AsDouble(o1, i) - ((PyVector*)o2)->coords[i];
            }
            return (PyObject*)ret;
        }
    }
        
    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}

static PyObject *
vector_inplace_sub(PyVector *self, PyObject *other)
{
    int i;
    if (checkPyVectorCompatible(other, self->dim)) {
        for (i = 0; i < self->dim; i++) {
            self->coords[i] += PySequence_GetItem_AsDouble(other, i);
        }
        Py_INCREF(self);
        return (PyObject*)self;
    }
    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}

static PyObject *
vector_mul(PyObject *o1, PyObject *o2)
{
    int i, row, col, dim;
    if (PyVector_Check(o1)) {
        dim = ((PyVector*)o1)->dim;
        /* vector * vector ? */
        if (checkPyVectorCompatible(o2, dim)) {
            double ret = 0.;
            for (i = 0; i < dim; i++) {
                ret += ((PyVector*)o1)->coords[i] * PySequence_GetItem_AsDouble(o2, i);
            }
            return PyFloat_FromDouble(ret);
        }
        /* vector * scalar ? */
        else if (checkRealNumber(o2)) {
            PyVector *ret = (PyVector*)PyVector_NEW(dim);
            double scalar = PyFloat_AsDouble(o2);
            for (i = 0; i < dim; i++) {
                ret->coords[i] = ((PyVector*)o1)->coords[i] * scalar;
            }
            return (PyObject*)ret;
        }
    }
    else {
        dim = ((PyVector*)o2)->dim;
        /* vector * vector ? */
        if (checkPyVectorCompatible(o1, dim)) {
            double ret = 0.;
            for (i = 0; i < dim; i++) {
                ret += PySequence_GetItem_AsDouble(o1, i) * ((PyVector*)o2)->coords[i];
            }
            return PyFloat_FromDouble(ret);
        }
        /* scalar * vector ? */
        else if (checkRealNumber(o1)) {
            PyVector *ret = (PyVector*)PyVector_NEW(dim);
            double scalar = PyFloat_AsDouble(o1);
            for (i = 0; i < dim; i++) {
                ret->coords[i] = scalar * ((PyVector*)o2)->coords[i];
            }
            return (PyObject*)ret;
        }
    }

    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}

static PyObject *
vector_inplace_mul(PyVector *self, PyObject *other)
{
    int i, row, col, dim;
    dim = self->dim;
    /* vector * scalar ? */
    if (checkRealNumber(other)) {
        double scalar = PyFloat_AsDouble(other);
        for (i = 0; i < dim; i++) {
            self->coords[i] *= scalar;
        }
        Py_INCREF(self);
        return (PyObject*)self;
    }

    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}

static PyObject *
vector_div(PyVector *self, PyObject *other)
{
    int i;
    if (checkRealNumber(other)) {
        double tmp = 1. / PyFloat_AsDouble(other);
        PyVector *ret = (PyVector*)PyVector_NEW(self->dim);
        for (i = 0; i < self->dim; i++) {
            ret->coords[i] = self->coords[i] * tmp;
        }
        return (PyObject*)ret;
    }
    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}

static PyObject *
vector_inplace_div(PyVector *self, PyObject *other)
{
    int i;
    if (checkRealNumber(other)) {
        double tmp = 1. / PyFloat_AsDouble(other);
        for (i = 0; i < self->dim; i++) {
            self->coords[i] *= tmp;
        }
        Py_INCREF(self);
        return (PyObject*)self;
    }
    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}

static PyObject *
vector_floor_div(PyVector *self, PyObject *other)
{
    int i;
    if (checkRealNumber(other)) {
        double tmp = 1. / PyFloat_AsDouble(other);
        PyVector *ret = (PyVector*)PyVector_NEW(self->dim);
        for (i = 0; i < self->dim; i++) {
            ret->coords[i] = floor(self->coords[i] * tmp);
        }
        return (PyObject*)ret;
    }
    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}

static PyObject *
vector_inplace_floor_div(PyVector *self, PyObject *other)
{
    int i;
    if (checkRealNumber(other)) {
        double tmp = 1. / PyFloat_AsDouble(other);
        for (i = 0; i < self->dim; i++) {
            self->coords[i] = floor(self->coords[i] * tmp);
        }
        Py_INCREF(self);
        return (PyObject*)self;
    }
    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}

static PyObject *
vector_neg(PyVector *self)
{
    int i;
    PyVector *ret = (PyVector*)PyVector_NEW(self->dim);
    for (i = 0; i < self->dim; i++) {
        ret->coords[i] = -self->coords[i];
    }
    return (PyObject*)ret;
}

static PyObject *
vector_pos(PyVector *self)
{
    int i;
    PyVector *ret = (PyVector*)PyVector_NEW(self->dim);
    memcpy(ret->coords, self->coords, sizeof(ret->coords[0]) * ret->dim);
    return (PyObject*)ret;
}

static int
vector_nonzero(PyVector *self)
{
    int i;
    for (i = 0; i < self->dim; i++) {
        if (fabs(self->coords[i]) > self->epsilon) {
            return 1;
        }
    }
    return 0;
}

PyNumberMethods vector_as_number = {
    (binaryfunc)vector_add,         /* nb_add;       __add__ */
    (binaryfunc)vector_sub,         /* nb_subtract;  __sub__ */
    (binaryfunc)vector_mul,         /* nb_multiply;  __mul__ */
    (binaryfunc)vector_div,         /* nb_divide;    __div__ */
    (binaryfunc)0,                  /* nb_remainder; __mod__ */
    (binaryfunc)0,                  /* nb_divmod;    __divmod__ */
    (ternaryfunc)0,                 /* nb_power;     __pow__ */
    (unaryfunc)vector_neg,          /* nb_negative;  __neg__ */
    (unaryfunc)vector_pos,          /* nb_positive;  __pos__ */
    (unaryfunc)0,                   /* nb_absolute;  __abs__ */
    (inquiry)vector_nonzero,        /* nb_nonzero;   __nonzero__ */
    (unaryfunc)0,                   /* nb_invert;    __invert__ */
    (binaryfunc)0,                  /* nb_lshift;    __lshift__ */
    (binaryfunc)0,                  /* nb_rshift;    __rshift__ */
    (binaryfunc)0,                  /* nb_and;       __and__ */
    (binaryfunc)0,                  /* nb_xor;       __xor__ */
    (binaryfunc)0,                  /* nb_or;        __or__ */
    (coercion)0,                    /* nb_coerce;    __coerce__ */
    (unaryfunc)0,                   /* nb_int;       __int__ */
    (unaryfunc)0,                   /* nb_long;      __long__ */
    (unaryfunc)0,                   /* nb_float;     __float__ */
    (unaryfunc)0,                   /* nb_oct;       __oct__ */
    (unaryfunc)0,                   /* nb_hex;       __hex__ */

    /* Added in release 2.0 */
    (binaryfunc)vector_inplace_add, /* nb_inplace_add;       __iadd__ */
    (binaryfunc)vector_inplace_sub, /* nb_inplace_subtract;  __isub__ */
    (binaryfunc)vector_inplace_mul, /* nb_inplace_multiply;  __imul__ */
    (binaryfunc)vector_inplace_div, /* nb_inplace_divide;    __idiv__ */
    (binaryfunc)0,                  /* nb_inplace_remainder; __imod__ */
    (ternaryfunc)0,                 /* nb_inplace_power;     __pow__ */
    (binaryfunc)0,                  /* nb_inplace_lshift;    __ilshift__ */
    (binaryfunc)0,                  /* nb_inplace_rshift;    __irshift__ */
    (binaryfunc)0,                  /* nb_inplace_and;       __iand__ */
    (binaryfunc)0,                  /* nb_inplace_xor;       __ixor__ */
    (binaryfunc)0,                  /* nb_inplace_or;        __ior__ */

    /* Added in release 2.2 */
    (binaryfunc)vector_floor_div,   /* nb_floor_divide;         __floor__ */
    (binaryfunc)vector_div,         /* nb_true_divide;          __truediv__ */
    (binaryfunc)vector_inplace_floor_div, /* nb_inplace_floor_divide; __ifloor__ */
    (binaryfunc)vector_inplace_div, /* nb_inplace_true_divide;  __itruediv__ */

    /* Added in release 2.5 */
    (unaryfunc)0,                   /* nb_index;  __index__ */
};



/*************************************************
 * Generic vector PySequence emulation routines
 *************************************************/

static Py_ssize_t
vector_len(PyVector *self)
{
    return (Py_ssize_t)self->dim;
}

static PyObject *
vector_GetItem(PyVector *self, Py_ssize_t index)
{
    if (index < 0 || index >= self->dim) {
        PyErr_SetString(PyExc_ValueError, "subscript out of range.");
        Py_RETURN_NONE;
    }
    return PyFloat_FromDouble(self->coords[index]);
}

static int
vector_SetItem(PyVector *self, Py_ssize_t index, PyObject *value)
{
/*
    if (!checkRealNumber(value)) {
        PyErr_SetString(PyExc_TypeError, "cannot assign a non float.");
        return -1;
    }
*/
    if (index < 0 || index >= self->dim) {
        PyErr_SetString(PyExc_TypeError, "subscript out of range.");
        return -1;
    }
    self->coords[index] = PyFloat_AsDouble(value);
    return 0;
}

PySequenceMethods vector_as_sequence = {
    (lenfunc)vector_len,             /* sq_length;    __len__ */
    (binaryfunc)0,                   /* sq_concat;    __add__ */
    (ssizeargfunc)0,                 /* sq_repeat;    __mul__ */
    (ssizeargfunc)vector_GetItem,    /* sq_item;      __getitem__ */
    (ssizessizeargfunc)0,            /* sq_slice;     __getslice__ */
    (ssizeobjargproc)vector_SetItem, /* sq_ass_item;  __setitem__ */
    (ssizessizeobjargproc)0,         /* sq_ass_slice; __setslice__ */
};

static PyObject*
vector_getx (PyVector *self, void *closure)
{
    return PyFloat_FromDouble(self->coords[0]);
}

static int
vector_setx (PyVector *self, PyObject *value, void *closure)
{
    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError, "Cannot delete the x attribute");
        return -1;
    }

    self->coords[0] = PyFloat_AsDouble(value);
    return 0;
}

static PyObject*
vector_gety (PyVector *self, void *closure)
{
    return PyFloat_FromDouble(self->coords[1]);
}

static int
vector_sety (PyVector *self, PyObject *value, void *closure)
{
    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError, "Cannot delete the y attribute");
        return -1;
    }

    self->coords[1] = PyFloat_AsDouble(value);
    return 0;
}



static PyObject *
vector_richcompare(PyVector *self, PyObject *other, int op)
{
    int i;
    if (!checkPyVectorCompatible(other, self->dim)) {
        if (op == Py_EQ)
            Py_RETURN_FALSE;
        else if (op == Py_NE)
            Py_RETURN_TRUE;
        else {
            Py_INCREF(Py_NotImplemented);
            return Py_NotImplemented;
        }
    }

    switch(op) {
    case Py_EQ:
        for (i = 0; i < self->dim; i++) {
            if (fabs(self->coords[i] - PySequence_GetItem_AsDouble(other, i)) >= self->epsilon) {
                Py_RETURN_FALSE;
            }
        }
        Py_RETURN_TRUE;
        break;
    case Py_NE:
        for (i = 0; i < self->dim; i++) {
            if (fabs(self->coords[i] - PySequence_GetItem_AsDouble(other, i)) >= self->epsilon) {
                Py_RETURN_TRUE;
            }
        }
        Py_RETURN_FALSE;
        break;
    default:
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
        break;
    }
}







static PyObject *
vector2_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyVector *vec = (PyVector *)type->tp_alloc(type, 0);

    if (vec != NULL) {
        vec->dim = 2;
        vec->epsilon = FLT_EPSILON;
        vec->coords = PyMem_New(double, vec->dim);
        if (vec->coords == NULL) {
            vec->ob_type->tp_free((PyObject*)vec);
            return NULL;
        }
    }

    return (PyObject *)vec;
}

static int
vector2_init(PyVector *self, PyObject *args, PyObject *kwds)
{
    PyObject *xOrSequence=NULL, *y=NULL, *z=NULL;
    static char *kwlist[] = {"x", "y", NULL};

    if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist,
                                      &xOrSequence, &y))
        return -1;

    if (xOrSequence) {
        if (checkRealNumber(xOrSequence)) {
            self->coords[0] = PyFloat_AsDouble(xOrSequence);
        } 
        else if (checkPyVectorCompatible(xOrSequence, self->dim)) {
            self->coords[0] = PySequence_GetItem_AsDouble(xOrSequence, 0);
            self->coords[1] = PySequence_GetItem_AsDouble(xOrSequence, 1);
            return 0;
        } 
        else {
            PyErr_SetString(PyExc_ValueError,
                            "Vector2d must be initialized with 2 real numbers or a sequence of 2 real numbers");
            return -1;
        }
    } 
    else {
        self->coords[0] = 0.;
    }

    if (y) {
        if (checkRealNumber(y)) {
            self->coords[1] = PyFloat_AsDouble(y);
        } 
        else {
            PyErr_SetString(PyExc_ValueError,
                            "Vector2d must be initialized with 2 real numbers or a sequence of 2 real numbers");
            return -1;
        }
    } 
    else {
        self->coords[1] = 0.;
    }

    return 0;
}


static PyMethodDef vector2_methods[] = {
/*
    {"rotate", (PyCFunction)PyVector2d_rotate, METH_VARARGS,
     "rotates the vector around the given axis by the amount given by angle."
    },
    {"rotated", (PyCFunction)PyVector2d_rotated, METH_VARARGS,
     "returns a vector rotated around the given axis by the amount given by angle."
    },
    {"normalized", (PyCFunction)PyVectorNd_normalized, METH_NOARGS,
     "returns a vector that has length == 1 and the same direction as self."
    },
    {"normalize", (PyCFunction)PyVectorNd_normalize, METH_NOARGS,
     "Normalizes the vector so that it has length == 1."
    },
    {"isNormalized", (PyCFunction)PyVectorNd_isNormalized, METH_NOARGS,
     "returns True if the vector has length == 1. otherwise it returns False."
    },
    {"cross", (PyCFunction)PyVector2d_cross, METH_O,
     "calculates the cross product."
    },
    {"dot", (PyCFunction)PyVectorNd_dot, METH_O,
     "calculates the dot product."
    },
    {"angleTo", (PyCFunction)PyVector2d_angleTo, METH_O,
     "returns the angle between self and the given vector."
    },
*/
    {NULL}  /* Sentinel */
};

static PyObject *
vector2_repr(PyVector *self)
{
    return PyString_FromFormat("<Vector2d(%g, %g)>", 
                               self->coords[0], self->coords[1]);
}

static PyObject *
vector2_str(PyVector *self)
{
    return PyString_FromFormat("(%g, %g)", 
                               self->coords[0], self->coords[1]);
}


static PyGetSetDef vector2_getsets[] = {
    { "x", (getter)vector_getx, (setter)vector_setx, NULL, NULL },
    { "y", (getter)vector_gety, (setter)vector_sety, NULL, NULL },
    { NULL, 0, NULL, NULL, NULL }  /* Sentinel */
};


/********************************
 * PyVector2d type definition
 ********************************/

static PyTypeObject PyVector2_Type = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "pygame.math.Vector2",         /*tp_name*/
    sizeof(PyVector),          /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    /* Methods to implement standard operations */
    (destructor)vector_dealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    (reprfunc)vector2_repr,   /*tp_repr*/
    /* Method suites for standard classes */
    &vector_as_number,       /*tp_as_number*/
    &vector_as_sequence,     /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    /* More standard operations (here for binary compatibility) */
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    (reprfunc)vector2_str,    /*tp_str*/
    PyObject_GenericGetAttr,   /*tp_getattro*/
    0,                         /*tp_setattro*/
    /* Functions to access object as input/output buffer */
    0,                         /*tp_as_buffer*/
    /* Flags to define presence of optional/expanded features */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES | Py_TPFLAGS_HAVE_INPLACEOPS, /*tp_flags*/
    /* Documentation string */
    DOC_PYGAMEVECTOR2,               /* tp_doc */

    /* Assigned meaning in release 2.0 */
    /* call function for all accessible objects */
    0,                         /* tp_traverse */
    /* delete references to contained objects */
    0,                         /* tp_clear */

    /* Assigned meaning in release 2.1 */
    /* rich comparisons */
    (richcmpfunc)vector_richcompare, /* tp_richcompare */
    /* weak reference enabler */
    0,                         /* tp_weaklistoffset */

    /* Added in release 2.2 */
    /* Iterators */
    0,                         /* tp_iter */
    0,                         /* tp_iternext */
    /* Attribute descriptor and subclassing stuff */
    vector2_methods,          /* tp_methods */
    vector_members,          /* tp_members */
    vector2_getsets,        /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)vector2_init,   /* tp_init */
    0,                         /* tp_alloc */
    (newfunc)vector2_new,     /* tp_new */
    0,                         /* tp_free */
    0,                         /* tp_is_gc */
    0,                         /* tp_bases */
    0,                         /* tp_mro */
    0,                         /* tp_cache */
    0,                         /* tp_subclasses */
    0,                         /* tp_weaklist */
};


static PyMethodDef _math_methods[] =
{
    {NULL, NULL, 0, NULL}
};

/* DOC */ static char _math_doc[] = 
/* DOC */    "Module for various math related classes and functions\n";

/****************************
 * Module init function
 ****************************/

MODINIT_DEFINE (math)
{
    fprintf(stdout, "init\n"); fflush(stdout);
    PyObject *module, *apiobj;
    static void *c_api[PYGAMEAPI_MATH_NUMSLOTS];

#if PY3
    static struct PyModuleDef _module = {
        PyModuleDef_HEAD_INIT,
        "math",
        _math_doc,
        -1,
        _math_methods,
        NULL, NULL, NULL, NULL
    };
#endif

    /* initialize the extension types */
    if ((PyType_Ready(&PyVector2_Type) < 0) /*|| 
        (PyType_Ready(&PyVector3_Type) < 0) ||
        (PyType_Ready(&PyVector4_Type) < 0)*/) {
        MODINIT_ERROR;
    }

    /* initialize the module */
#if PY3
    module = PyModule_Create (&_module);
#else
    module = Py_InitModule3 (MODPREFIX "math", _math_methods, _math_doc);
#endif

    if (module == NULL) {
        MODINIT_ERROR;
    }

    /* add extension types to module */
    Py_INCREF(&PyVector2_Type);
//    Py_INCREF(&PyVector3_Type);
//    Py_INCREF(&PyVector4_Type);
    if ((PyModule_AddObject(module, "Vector2", (PyObject *)&PyVector2_Type) != 0) /*||
        (PyModule_AddObject(module, "Vector3", (PyObject *)&PyVector3_Type) != 0) ||
        (PyModule_AddObject(module, "Vector4", (PyObject *)&PyVector4_Type) != 0)*/) {
        Py_DECREF(&PyVector2_Type);
//        Py_DECREF(&PyVector3_Type);
//        Py_DECREF(&PyVector4_Type);
        DECREF_MOD (module);
        MODINIT_ERROR;
    }

    /* export the C api */
    c_api[0] = &PyVector2_Type;
//    c_api[1] = &PyVector3_Type;
//    c_api[2] = &PyVector4_Type;
//    c_api[3] = PyVector_NEW;
//    c_api[4] = checkPyVectorCompatible;
    apiobj = PyCObject_FromVoidPtr(c_api, NULL);
    if (apiobj == NULL) {
        DECREF_MOD (module);
        MODINIT_ERROR;
    }

    if (PyModule_AddObject(module, PYGAMEAPI_LOCAL_ENTRY, apiobj) != 0) {
        Py_DECREF (apiobj);
        DECREF_MOD (module);
        MODINIT_ERROR;
    }
    Py_DECREF (apiobj);

    MODINIT_RETURN (module);
}