Update geometry
This commit is contained in:
		
							parent
							
								
									dc3889437d
								
							
						
					
					
						commit
						fb8c8d0266
					
				| @ -1,33 +1,62 @@ | |||||||
| import math | import math | ||||||
| 
 | 
 | ||||||
| class Vector: | class Vector: | ||||||
|  |     """ 3D Vector | ||||||
|  | 
 | ||||||
|  |     Simple class that represents a 3D vector | ||||||
|  |     """ | ||||||
|  | 
 | ||||||
|     def __init__(self, x = 0.0, y = 0.0, z = 0.0): |     def __init__(self, x = 0.0, y = 0.0, z = 0.0): | ||||||
|  |         """ | ||||||
|  |         Creates a vector from it's coordinates | ||||||
|  |         """ | ||||||
|         self.x = x |         self.x = x | ||||||
|         self.y = y |         self.y = y | ||||||
|         self.z = z |         self.z = z | ||||||
| 
 | 
 | ||||||
|     def from_array(self, arr): |     def from_array(self, arr): | ||||||
|  |         """ | ||||||
|  |         Creates a vector from an array | ||||||
|  |         """ | ||||||
|         self.x = float(arr[0]) if len(arr) > 0 else None |         self.x = float(arr[0]) if len(arr) > 0 else None | ||||||
|         self.y = float(arr[1]) if len(arr) > 1 else None |         self.y = float(arr[1]) if len(arr) > 1 else None | ||||||
|         self.z = float(arr[2]) if len(arr) > 2 else None |         self.z = float(arr[2]) if len(arr) > 2 else None | ||||||
|         return self |         return self | ||||||
| 
 | 
 | ||||||
|     def __add__(self, other): |     def __add__(self, other): | ||||||
|  |         """ | ||||||
|  |         Sums two vectors | ||||||
|  |         """ | ||||||
|         return Vector(self.x + other.x, self.y + other.y, self.z + other.z) |         return Vector(self.x + other.x, self.y + other.y, self.z + other.z) | ||||||
| 
 | 
 | ||||||
|     def __mul__(self, other): |     def __mul__(self, other): | ||||||
|  |         """ | ||||||
|  |         Computes the product between a vector and a number | ||||||
|  |         """ | ||||||
|         return Vector(self.x * other, self.y * other, self.z * other) |         return Vector(self.x * other, self.y * other, self.z * other) | ||||||
| 
 | 
 | ||||||
|     def __rmul__(self, other): |     def __rmul__(self, other): | ||||||
|  |         """ | ||||||
|  |         Computes the product between a vector and a number | ||||||
|  |         """ | ||||||
|         return self.__mul__(other) |         return self.__mul__(other) | ||||||
| 
 | 
 | ||||||
|     def norm2(self): |     def norm2(self): | ||||||
|  |         """ | ||||||
|  |         Computes the square of the norm of a vector | ||||||
|  |         """ | ||||||
|         return self.x * self.x + self.y * self.y + self.z * self.z |         return self.x * self.x + self.y * self.y + self.z * self.z | ||||||
| 
 | 
 | ||||||
|     def norm(self): |     def norm(self): | ||||||
|  |         """ | ||||||
|  |         Compute the norm of a vector | ||||||
|  |         """ | ||||||
|         return math.sqrt(self.norm2()) |         return math.sqrt(self.norm2()) | ||||||
| 
 | 
 | ||||||
|     def normalize(self): |     def normalize(self): | ||||||
|  |         """ | ||||||
|  |         Divides each coordinate of the vector by its norm | ||||||
|  |         """ | ||||||
|         norm = self.norm() |         norm = self.norm() | ||||||
|         if abs(norm) > 0.0001: |         if abs(norm) > 0.0001: | ||||||
|             self.x /= norm |             self.x /= norm | ||||||
| @ -35,21 +64,33 @@ class Vector: | |||||||
|             self.z /= norm |             self.z /= norm | ||||||
| 
 | 
 | ||||||
|     def cross_product(v1, v2): |     def cross_product(v1, v2): | ||||||
|  |         """ | ||||||
|  |         Computes the cross product between the two vectors | ||||||
|  |         """ | ||||||
|         return Vector( |         return Vector( | ||||||
|             v1.y * v2.z - v1.z * v2.y, |             v1.y * v2.z - v1.z * v2.y, | ||||||
|             v1.z * v2.x - v1.x * v2.z, |             v1.z * v2.x - v1.x * v2.z, | ||||||
|             v1.x * v2.y - v1.y * v2.x) |             v1.x * v2.y - v1.y * v2.x) | ||||||
| 
 | 
 | ||||||
|     def from_points(v1, v2): |     def from_points(v1, v2): | ||||||
|  |         """ | ||||||
|  |         Creates a vector from two points | ||||||
|  |         """ | ||||||
|         return Vector( |         return Vector( | ||||||
|             v2.x - v1.x, |             v2.x - v1.x, | ||||||
|             v2.y - v1.y, |             v2.y - v1.y, | ||||||
|             v2.z - v1.z) |             v2.z - v1.z) | ||||||
| 
 | 
 | ||||||
|     def __str__(self): |     def __str__(self): | ||||||
|  |         """ | ||||||
|  |         Prints the coordinates of the vector between partheses | ||||||
|  |         """ | ||||||
|         return '(' + ", ".join([str(self.x), str(self.y), str(self.z)]) + ")" |         return '(' + ", ".join([str(self.x), str(self.y), str(self.z)]) + ")" | ||||||
| 
 | 
 | ||||||
|     def dot(self, other): |     def dot(self, other): | ||||||
|  |         """ | ||||||
|  |         Computes the dot product of two vectors | ||||||
|  |         """ | ||||||
|         return self.x * other.x + self.y * other.y + self.z * other.z |         return self.x * other.x + self.y * other.y + self.z * other.z | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user