Statistics
| Revision:

root / PyOpenGL-Demo / NeHe / lesson44 / glVector.py @ 1

History | View | Annotate | Download (2 kB)

1 1 equemene
# mirros glVector.cpp - slow no nummarray
2 1 equemene
import math
3 1 equemene
4 1 equemene
5 1 equemene
# The glVector of lesson 44 has three componenets only
6 1 equemene
# i j k
7 1 equemene
class glVector:
8 1 equemene
        # Need to create from a point?
9 1 equemene
        def __init__ (self, *args):
10 1 equemene
                if (len (args) == 3):
11 1 equemene
                        self.i = args [0]
12 1 equemene
                        self.j = args [1]
13 1 equemene
                        self.k = args [2]
14 1 equemene
                        return
15 1 equemene
                if (len (args) == 1):
16 1 equemene
                        # // Assign this vector to the vector passed in.
17 1 equemene
                        src_v = args [0]
18 1 equemene
                        if (not isinstance (src_v, glVector)):
19 1 equemene
                                raise TypeError, "Invalid ctor argument for glVector"
20 1 equemene
                        self.i = src_v.i
21 1 equemene
                        self.j = src_v.j
22 1 equemene
                        self.k = src_v.k
23 1 equemene
                        return
24 1 equemene
                elif (len (args) == 0):
25 1 equemene
                        self.i = 0
26 1 equemene
                        self.j = 0
27 1 equemene
                        self.k = 0
28 1 equemene
                        return
29 1 equemene
                else:
30 1 equemene
                        raise TypeError, "Invalid ctor argument for glVector"
31 1 equemene
32 1 equemene
        # def __setattr__ (self, name, value)
33 1 equemene
        #        """ We want to """
34 1 equemene
        #        self.name = value
35 1 equemene
36 1 equemene
        def Magnitude (self):
37 1 equemene
                Mag = math.sqrt (self.i * self.i + self.j * self.j + self.k * self.k)
38 1 equemene
                return Mag
39 1 equemene
40 1 equemene
        def Normalize (self):
41 1 equemene
                mag = self.Magnitude ()
42 1 equemene
                if (mag != 0.0):
43 1 equemene
                        self.i = self.i / mag
44 1 equemene
                        self.j = self.j / mag
45 1 equemene
                        self.k = self.k / mag
46 1 equemene
                return
47 1 equemene
48 1 equemene
        def __mul__ (self, scalar):
49 1 equemene
                result = glVector (self)
50 1 equemene
                result *= scalar
51 1 equemene
                return result
52 1 equemene
53 1 equemene
        def __imul__ (self, other):
54 1 equemene
                if (type (other) == int):
55 1 equemene
                        scalar = other
56 1 equemene
                        self.i *= scalar
57 1 equemene
                        self.j *= scalar
58 1 equemene
                        self.k *= scalar
59 1 equemene
                elif (type (other) == float):
60 1 equemene
                        scalar = other
61 1 equemene
                        self.i *= scalar
62 1 equemene
                        self.j *= scalar
63 1 equemene
                        self.k *= scalar
64 1 equemene
                else:
65 1 equemene
                        raise TypeError, "Invalid type (%s) for multiplication argument" % (str (type (other)))
66 1 equemene
67 1 equemene
                return self
68 1 equemene
69 1 equemene
70 1 equemene
        def __add__ (self, v):
71 1 equemene
                result = copy.copy (self)
72 1 equemene
                result += v
73 1 equemene
                return result
74 1 equemene
75 1 equemene
        def __iadd__ (self, v):
76 1 equemene
                self.i += v.i
77 1 equemene
                self.j += v.j
78 1 equemene
                self.k += v.k
79 1 equemene
                return self
80 1 equemene
81 1 equemene
        def __sub__ (self, other):
82 1 equemene
                self.i -= v.i
83 1 equemene
                self.j -= v.j
84 1 equemene
                self.k -= v.k
85 1 equemene
                return self
86 1 equemene
87 1 equemene
        def __str__ (self):
88 1 equemene
                return "i=%f j=%f k=%f, magnitude=%f" % (self.i, self.j, self.k, self.Magnitude ())
89 1 equemene
90 1 equemene
91 1 equemene
92 1 equemene
# Unit Test harness if this python module is run directly.
93 1 equemene
if __name__ == "__main__":
94 1 equemene
        print "testing slow glpoint/vect.\n"
95 1 equemene
        v = glVector ()
96 1 equemene
        v.i = 1.1
97 1 equemene
        v.Magnitude ()
98 1 equemene
        print v
99 1 equemene
        print "mult new"
100 1 equemene
        print (v * 2)
101 1 equemene
        print "Done"