Package: v3d
v3dv3d (Stands for Vector 3D) is a 3D vector library for basic Vector operations.
It was inspired by:https://github.com/allelos/vectorsWHY?A library was created byhttps://github.com/allelos/. However there was some missing parts(such as from_mag_and_dir).Installationpipinstallv3dDocumentationConceptThis work inspired byhttps://github.com/allelos/vectorsV3D has no dependency other than standard math and logging module.It consists of two parts, Point and Vector.A point is a class with multiple operations. It is also a data carrier for Vector.Operations:to_polar -> Converts from cartesian to polarfrom_polar -> Converts polar to cartesianadd -> Calculates addition of two pointssubtract -> Calculates subtraction of two points. Uses addscale -> Calculates multiplication a point with a scalardivide -> Calculates division a point with a scalar. Uses scaledist -> Calculates distance between two points or a point and originis_same -> Checks if two points are sameVector is a module with multiple operations.Operations:from_points -> Creates a Vector from two pointsadd -> Calculates addition of two vectors. Uses Point.addsubtract -> Calculates subtraction of two vectors. Uses addmultiply -> Calculates cross product of two vectors or scales a vector with a scalardivide -> Scales a vector with 1/scalar. Uses multiplydot -> Calculates dot product of two vectorsrotate -> Calculates a retated vector around given alpha, beta, gamma (alpha in x, beta in y and gamma in z axis)mag -> Calculates magnitude of a vectorunit -> Calculates unit vector of a vectornormal -> Calculates normal vector of a vectorheading -> Calculates heading direction of a vectorangle_between -> Calculates angle between two vectorsis_parallel -> Checks if two vectors are parallelis_perpendicular -> Checks if two vectors are perpendicularis_non_parallel -> Checks if two vectors are neither parallel nor perpendicularis_same -> Checks if two vectors are samePointCreating a Point:fromv3dimportVector,Pointp1=Point()# This will create a zero point. (0, 0, 0)p2=Point(1,3,1)# This will create a point at (1, 3, 1)Converting between Polar and Cartesian:p2.to_polar()# r, theta, phi (angles are in degrees)# (3.3166247903554, 72.4515993862077, 71.56505117707799)p1=Point.from_polar(1,45,0)# Creates a cartesian point by given polar coordinatesAdd/Subtract:# Addp1.add(p2)# Point(x=1.7071067811865475, y=3.0, z=1.7071067811865475)p2.add(p1)# Point(x=1.7071067811865475, y=3.0, z=1.7071067811865475)p1+p2# Point(x=1.7071067811865475, y=3.0, z=1.7071067811865475)p2+p1# Point(x=1.7071067811865475, y=3.0, z=1.7071067811865475)# Subtractp1.subtract(p2)# Point(x=-0.29289321881345254, y=-3.0, z=-0.2928932188134524)p1-p2# Point(x=-0.29289321881345254, y=-3.0, z=-0.2928932188134524)p2.subtract(p1)# Point(x=0.29289321881345254, y=3.0, z=0.2928932188134524)p2-p1# Point(x=0.29289321881345254, y=3.0, z=0.2928932188134524)Multiply/Dividep2.scale(2)# Point(x=2, y=6, z=2)p2*2# Point(x=2, y=6, z=2)p2.divide(2)# Point(x=0.5, y=1.5, z=0.5)p2/2# Point(x=0.5, y=1.5, z=0.5)Other Operations:# Distance from origin:p1.dist()# 1.0# Distance from other point:p1.dist(p2)# 3.028460479394408# Check if two point are the same:p1.is_same(p2)# Falsep1==p2# False# Notice we made a copy of p2 as p3p3=p2.copy()p2.is_same(p3)# Truep2==p3# TrueVectorCreating a Vector:# Notice p1 is a point type. It was created before.v1=Vector(p1)# Vector(Point(x=0.7071067811865475, y=0.0, z=0.7071067811865476))Magnitude, heading and unit vector:v1.mag()# 1.0abs(v1)# 1.0v1.heading()# (45.0, 0.0)v1.unit()# Vector(Point(x=0.7071067811865475, y=0.0, z=0.7071067811865476))Rotate:v1.rotate(alpha=45,beta=0,gamma=0)# alpha in x, beta in y and gamma in y axis# Vector(Point(x=0.7071067811865475, y=-0.5, z=0.5000000000000001))Rotate about another vector:# Creating a new vector from p2v2=Vector(p2)v1.rotate_about(v2,30)# Vector(Point(x=1.8625012984571216, y=0.5684060729445177, z=-0.2588190451025205))Add/Subtract:v1.add(v2)# Vector(Point(x=1.7071067811865475, y=3.0, z=1.7071067811865475))v1+v2# Vector(Point(x=1.7071067811865475, y=3.0, z=1.7071067811865475))v1.subtract(v2)# Vector(Point(x=-0.29289321881345254, y=-3.0, z=-0.2928932188134524))v1-v2# Vector(Point(x=-0.29289321881345254, y=-3.0, z=-0.2928932188134524))Multiply:v1.multiply(2)# Vector(Point(x=1.414213562373095, y=0.0, z=1.4142135623730951))v1*2# Vector(Point(x=1.414213562373095, y=0.0, z=1.4142135623730951))# In case of two vector multiplication cross product will be calculated.v1.multiply(v2)# Vector(Point(x=-2.121320343559643, y=1.1102230246251565e-16, z=2.1213203435596424))v1*v2# Vector(Point(x=-2.121320343559643, y=1.1102230246251565e-16, z=2.1213203435596424))v1.divide(2)# Vector(Point(x=0.35355339059327373, y=0.0, z=0.3535533905932738))v1/2# Vector(Point(x=0.35355339059327373, y=0.0, z=0.3535533905932738))# These two lines will raise an error. Vector by Vector division is not possiblev1.divide(v2)v1/v2Dot product:v1.dot(v2)# 1.414213562373095Other operations:# Angle between two vectorsv1.angle_between(v2)# 64.7605981793211# Check if two vector are samev1.is_same(v2)# Falsev1==v2# False# Noice Points p2 and p3 are the same so will the vectors bev3=Vector(p3)v2.is_same(v3)# Truev2==v3# True# Check if two vectors are perpendicular# We know cross product of two vectors will be a vector perpendicular to other two vectorscross_product=v1*v2cross_product.is_perpendicular(v1)andcross_product.is_perpendicular(v2)# True# Check if two vectors are parallel# We know cross product of two vectors will be a vector perpendicular to other two vectorscross_product.is_parallel(v1)orcross_product.is_parallel(v2)# False# Check if two vectors are non_parallel# We know cross product of two vectors will be a vector perpendicular to other two vectorscross_product.is_non_parallel(v1)andcross_product.is_non_parallel(v2)# FalseExampleExample:https://github.com/mshemuni/V3D/blob/master/example.ipynb