Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For those who would like to avoid numpy and do this in pure python, this may give you a few good ideas. I'm sure there are disadvantages and flaws to this little skit also. The "operator" module speeds up your math calculations as they are done with c functions:</p> <pre><code>from operator import sub, add, iadd, mul import copy class Acceleration(object): def __init__(self, x, y, z): super(Acceleration, self).__init__() self.accel = [x, y , z] self.dimensions = len(self.accel) @property def x(self): return self.accel[0] @x.setter def x(self, val): self.accel[0] = val @property def y(self): return self.accel[1] @y.setter def y(self, val): self.accel[1] = val @property def z(self): return self.accel[2] @z.setter def z(self, val): self.accel[2] = val def __iadd__(self, other): for x in xrange(self.dimensions): self.accel[x] = iadd(self.accel[x], other.accel[x]) return self def __add__(self, other): newAccel = copy.deepcopy(self) newAccel += other return newAccel def __str__(self): return "Acceleration(%s, %s, %s)" % (self.accel[0], self.accel[1], self.accel[2]) def getVelocity(self, deltaTime): return Velocity(mul(self.accel[0], deltaTime), mul(self.accel[1], deltaTime), mul(self.accel[2], deltaTime)) class Velocity(object): def __init__(self, x, y, z): super(Velocity, self).__init__() self.x = x self.y = y self.z = z def __str__(self): return "Velocity(%s, %s, %s)" % (self.x, self.y, self.z) if __name__ == "__main__": accel = Acceleration(1.1234, 2.1234, 3.1234) accel += Acceleration(1, 1, 1) print accel accels = [] for x in xrange(10): accel += Acceleration(1.1234, 2.1234, 3.1234) vel = accel.getVelocity(2) print "Velocity of object with acceleration %s after one second:" % (accel) print vel </code></pre> <p>prints the following:</p> <p>Acceleration(2.1234, 3.1234, 4.1234)</p> <p>Velocity of object with acceleration Acceleration(13.3574, 24.3574, 35.3574) after one second: Velocity(26.7148, 48.7148, 70.7148)</p> <p>You can get fancy for faster calculations:</p> <pre><code>def getFancyVelocity(self, deltaTime): from itertools import repeat x, y, z = map(mul, self.accel, repeat(deltaTime, self.dimensions)) return Velocity(x, y, z) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload