
Since you are building the Vec2 class, you know that self is of type Vec2 or one of its subclasses, if any. In your first version, I don't understand your various if isinstance(self, types):Ĭhecks. Thanks for all of your feedback and help! This can all be found in this github repository under the math3d folder.

#Python vector code code
I would rather that users not even touch the base Vec class at all! Is there a cleaner way to do imports if I switch the dot function code to a module level, either by doing some import trickery in the _init_.py file in the math3d folder where all this code is or in vec.py or vec2.py?įor anyone interested in what the "final" code looks like, I now have completed writing code for the Vec2/3/4, Mat2/3/4, and Quat classes. In that case, should I even implement a random function in the base Vec class, or is there a way to keep both Vec.random(n) as well as Vec2.random()?Īdditionally, I was trying to move functions like dot to the module level, but then wouldn't users have to call both of these imports if they were dotting two Vec2 vectors: from import dot One is the random function in Vec2 needs to be called like Vec2.random(2) which is ugly. However, this does introduce a new issue. Tests = [d and < comparisons that compare based on vector magnitude. Pairs = zip(list(np.fabs(a.data)), list(np.fabs(b.data))) Return Vec(np.dot(a.data, equals(cls, a, b, tolerance=0.0): Return self.square_length() >= other.square_length() Return (a.x * b.x) + (a.y * equals(cls, a, b, tolerance=0.0): Return (self.x * self.x) + (self.y * self.y)ĭef transform(self, matrix):#mat2, mat2d, mat3, cross(cls, a, b): Return Vec2(round(self.x), round(self.y)) Return Vec2(self.x/length, self.y/length) Return Vec2(math.floor(self.x), math.floor(self.y)) Return Vec2(math.ceil(self.x), math.ceil(self.y)) Return Vec2(self.x - other.x, self.y - other.y) Return Vec2(self.x - other, self.y - other) Return Vec2(self - other.x, self - other.y) Return Vec2(other - self.x, other - self.y) Return Vec2(other * self.x, other * self.y) Return Vec2(self.x * other.x, self.y * other.y) Return Vec2(self.x * other, self.y * other) Return Vec2(self * other.x, self * other.y) Return Vec2(self.x + other.x, self.y + other.y) Return Vec2(self.x + other, self.y + other) Return Vec2(self + other.x, self + other.y) As part of a game engine I am creating, I am in the process of writing a bunch of math classes (vector2/3/4, matrix2/3/4, and quaternions to start with).
