Skip to main content

A 3D vector library for basic Vector operations.

Project description

V3d

v3d

v3d (Stands for Vector 3D) is a 3D vector library for basic Vector operations. It was inspired by: https://github.com/allelos/vectors

WHY?

A library was created by https://github.com/allelos/. However there was some missing parts(such as from_mag_and_dir).

Installation

pip install v3d

Documentation

Concept

This work inspired by https://github.com/allelos/vectors

V3D 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 polar
  • from_polar -> Converts polar to cartesian
  • add -> Calculates addition of two points
  • subtract -> Calculates subtraction of two points. Uses add
  • scale -> Calculates multiplication a point with a scalar
  • divide -> Calculates division a point with a scalar. Uses scale
  • dist -> Calculates distance between two points or a point and origin
  • is_same -> Checks if two points are same

Vector is a module with multiple operations.

Operations:

  • from_points -> Creates a Vector from two points
  • add -> Calculates addition of two vectors. Uses Point.add
  • subtract -> Calculates subtraction of two vectors. Uses add
  • multiply -> Calculates cross product of two vectors or scales a vector with a scalar
  • divide -> Scales a vector with 1/scalar. Uses multiply
  • dot -> Calculates dot product of two vectors
  • rotate -> 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 vector
  • unit -> Calculates unit vector of a vector
  • normal -> Calculates normal vector of a vector
  • heading -> Calculates heading direction of a vector
  • angle_between -> Calculates angle between two vectors
  • is_parallel -> Checks if two vectors are parallel
  • is_perpendicular -> Checks if two vectors are perpendicular
  • is_non_parallel -> Checks if two vectors are neither parallel nor perpendicular
  • is_same -> Checks if two vectors are same

Point

Creating a Point:

from v3d import Vector, Point

p1 = 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 coordinates

Add/Subtract:

# Add
p1.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)

# Subtract
p1.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/Divide

p2.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)
# False
p1 == p2
# False

# Notice we made a copy of p2 as p3
p3 = p2.copy()

p2.is_same(p3)
# True
p2 == p3
# True

Vector

Creating 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.0
abs(v1)
# 1.0

v1.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 p2
v2 = 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 possible
v1.divide(v2)
v1 / v2

Dot product:

v1.dot(v2)
# 1.414213562373095

Other operations:

# Angle between two vectors
v1.angle_between(v2)
# 64.7605981793211

# Check if two vector are same
v1.is_same(v2)
# False
v1 == v2
# False

# Noice Points p2 and p3 are the same so will the vectors be
v3 = Vector(p3)
v2.is_same(v3)
# True
v2 == v3
# True


# Check if two vectors are perpendicular
# We know cross product of two vectors will be a vector perpendicular to other two vectors

cross_product = v1 * v2
cross_product.is_perpendicular(v1) and cross_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 vectors

cross_product.is_parallel(v1) or cross_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 vectors

cross_product.is_non_parallel(v1) and cross_product.is_non_parallel(v2)
# False

Example

Example: https://github.com/mshemuni/V3D/blob/master/example.ipynb

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

v3d-0.0.5.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

v3d-0.0.5-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file v3d-0.0.5.tar.gz.

File metadata

  • Download URL: v3d-0.0.5.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for v3d-0.0.5.tar.gz
Algorithm Hash digest
SHA256 f20816e4e4d2a8141c9d788e6005d5926e0247ab61759a73d6748ca9c9494d41
MD5 009a6006c17568e2fdaf15d358af29ea
BLAKE2b-256 4ff4e294c8e548051481ef3dea8a99def26d20dbe537802d52bd1f082086c158

See more details on using hashes here.

File details

Details for the file v3d-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: v3d-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for v3d-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3e25168ce4aa3a82eb71e46f213737f4914f9fc01bf3424039aea192f16b3110
MD5 b3288ac14055df504bdfc6d08d522679
BLAKE2b-256 1cefce41ebebe6c803aa131744626ab67838a0c4d7550bce0d83af22baf43f2e

See more details on using hashes here.

Supported by

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