Skip to main content

A Python library for simple use of common point and vector operations in 3-dimensional space as well as for 2-dimensions.

Project description

vectometry

The Python package vectometry implements a Point object as well as a Vector object and the common vector operations in 3-dimensional space as well as for 2-dimensions. All functions can be used as the function itself, or via Magical Functions and Operator Overloading. That means, for example the magnitude of a vector A can be calculated by vectometry.norm(A), but also by the Built-In function abs(A). And for example the scalar product of vectors A and B can be calculated by vectometry.dot(A,B), but also by the (*)-operator: A*B.

Developed by InformaticFreak (c) 2021

Installation

pip install vectometry

Documentation

Creating Vector and Point

A Point object or Vector object can be initialized by different ways.

At first by three single real numbers.

from vectometry import *

p1 = Point(1, 2, -3)
v1 = Vector(1, 2, -3)

Or by a list or tuple of three real numbers.

p2 = Point([2, -4, 6])
p3 = Point((3, 3, 3))

v2 = Vector([2, -4, 6])
v3 = Vector((3, 3, 3))

A Vector object can also be created by two Point objects, that means a Vector from Point p1 to Point p2.

v4 = Vector(p1, p2)
v4 = p2 - p1
v4 #=> Vector(1, -6, 9)

A point or vector for only 2-dimensions can also be created by the same Point object or Vector object, for this only two coordinates must be passed by a list/tuple or as singe values. The remaining third coordinate will be set to zero.

p2D = Point([5, 6])
p2D #=> Point(5, 6, 0)

v2D = Vector(1, 2)
v2D #=> Vector(1, 2, 0)

Compare position relationships of vectors

Perpendicular (orthogonal)

Two vectors can be perpendicular (orthogonal) to each other, which means that the smaller angle between the two vector is 90 degrees.

is_orthogonal(v1, v2) #=> False

Collinear (parallel/anti-parallel)

Two vectors are collinear, if they are parallel or anti-parallel to each other.

is_collinear(v1, v2) #=> False

Complanar

Three vectors can be in the same plane, that means they are complanar.

is_complanar(v1, v2, v3) #=> False

Calculation of common vector operations

All operations and calculations can be used by importing the functions. The basic operations are implemented with operator overloading, so regular operators can be used for same result.

Addition

Both lines calculates the addition of two Vector objects. It returns the same Vector object in both orders.

add(v1, v2) #=> Vector(3, -2, 3)
v1 + v2

The addition of two Point objects returns a Vector object as the negated displacement of both Point objects.

add(p1, p2) #=> Vector(3, -2, 3)
p1 + p2

Subtraction

Subtracts the one Vector object from the other Vector object. It return a Vector object.

sub(v1, v2) #=> Vector(-1, 6, -9)
v1 - v2

The difference of two Point objects returns a Vector object as the displacement of both Point objects.

sub(p1, p2) #=> Vector(-1, 6, -9)
p1 - p2

Multiplication by a real number

Multiplication of a Vector object by a real number returns a Vector object. The order of both factors doesn't matter.

n = 4
mul(v1, n) #=> Vector(4, 8, -12)
v1 * n

Division by a real number

A Vector object devided by a real number means the multiplication of a Vector object by the reciprocal of the same number. It returns a Vector object only be calculated in this order.

m = 2
div(v1, m) #=> Vector(0.5, 1.0, -1.5)
v1 / m
v1 * (1/m)

Negative Vector or Point

It returns a Vector pointing in the negative direction, which means it is anti-parallel.

neg(v1) #=> Vector(-1, -2, 3)
-v1

It return a Point object with negated coordinates.

neg(p1) #=> Point(-1, -2, 3)
-p1

Magnitude

The magnitude of a Vector object describes the length. It can be calculated from both lines and returns a positive real number including zero.

norm(v1) #=> 3.7416573867739413
abs(v1)

Unit Vector

It returns the unit vector of a Vector object, it means a vector with the same direction but a magnitude of one.

v1u = unit(v1)
v1u #=> Vector(0.2672612419124244, 0.5345224838248488, -0.8017837257372732)
norm(v1u) #=> 1.0

Dot/Scalar Product

Both lines calculates the dot product of two Vector objects. The dot product returns a real number.

dot(v1, v2) #=> -24
v1 * v2

Cross/Vector Product

Both lines calculates the cross product of two Vector objects. The cross product returns a Vector object.

cross(v1, v2) #=> Vector(0, -12, -8)
v1 % v2

Determinant

Calculates the determinant of three Vector objects. It returns a real number.

det(v1, v2, v3) #=> -60

The same result is calculated by a combination of the cross product and dot product:

dot(cross(v1, v2), v3)
v1 % v2 * v3

Angle

The lines calculates the smaller angle between two Vector objects, the result returns a real number. At third position the mode can be specified between degree with "deg" and radian with "rad". Degrees is optional and the default mode.

angle(v1, v2) #=> 148.997280866126
angle(v1, v2, "deg")
angle(v1, v2, "rad") #=> 2.6004931276326473

Area of a parallelogram

The following function calculates the area of a parallelogram spanned by two Vector objects. It returns a positive real number including zero.

area(v1, v2) #=> 14.422205101855956

Volume of a parallelepiped

It calculates the volume of a parallelepiped spanned by three Vector objects. It returns a positive real number including zero.

spate(v1, v2, v3) #=> 60

Miscellaneous

Get and set coordinates

The single coordinates of a Point object or Vector object can be set to the given value at first position of the specified x-, y- or z-method. No matter if the value is specified, the (new) coordinate is returned as a real number.

An example:

v = Vector(1, 2, 3)

v.x() #=> 1
v #=> Vector(1, 2, 3)

v.x(5) #=> 5
v #=> Vector(5, 2, 3)

Get Point of a Vector

It returns the coordinates of the point on which the Vector object pointing as an independent Point object.

v = Vector(1, 2, 3)
v.point() #=> Point(1, 2, 3)

Round a Point or Vector

It rounds the coordinates of a Point object or Vector object to the given decimal digits, default is zero decimal digits.

v = Vector(0.3454, 2.15, -7.14)
round(v, 1) #=> Vector(0.3, 2.1, -7.1)

Iteration and lists/tuples

A Point object and a Vector object can be iterated, all three coordinates are swept.

An example:

p = Point(1, 2, 3)
[ c for c in p ] #=> [1, 2, 3]

Another way to get a list/tuple of the coordinates of a Point object or Vector object are the following functions:

list(p) #=> [1, 2, 3]
tuple(p) #=> (1, 2, 3)

The amount of dimensions of a Point object or Vector object in 3-dimensional space are always 3, but the length function works anyway.

len(p) #=> 3
len(v) #=> 3

Copies/Duplicates

It returns an independent copy of a Point object or Vector object.

An example:

v = Vector(1, 2, 3)

v_indep = v.copy()
v_indep.x(-4)

v_dep = v
v_dep.x(-8)

v #=> Vector(-8, 2, 3)
v_indep #=> Vector(-4, 2, 3)
v_dep #=> Vector(-8, 2, 3)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vectometry-2021.8.tar.gz (8.0 kB view hashes)

Uploaded Source

Built Distribution

vectometry-2021.8-py3-none-any.whl (7.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page