Python unitize vector and make it a vector

It’s easier to understand, if you understand object oriented programming.
Just image that your vector ‘xvector’ is an instance of a vector class that is stored in your variable ‘xvector’. The vector class has its own attributes and class methods/functions that you can call to manipulate your instance of that class.

Example:

class Vector:
    def __init__(self, x, y, z):
        self.x = x # attribute x
        self.y = y # attribute y
        self.z = z # attribute z
        self.length = self.magnitude() # attribute length
    
    def magnitude(self): # method
        return (self.x**2 + self.y**2 + self.z**2)**0.5
    
    def unitize(self): # method
        self.x = self.x / self.length
        self.y = self.y / self.length
        self.z = self.z / self.length
        self.length = self.magnitude()
        return True


xvector = Vector(-1.4123, -2.365, 2.877) # class instance
print xvector.length # class attribute

xvector.unitize() # class method
print xvector.length 

47

This is a pure Python example, which should work and is meant to explain object oriented stuff. The Rhinocommon library is written in C#, so everything should work similarly, although things might be structured differently.

3 Likes