Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

documentation

risknu edited this page Nov 30, 2023 · 3 revisions

rivector - risknu vectors

This Python library introduces a set of fast vector classes, designed for convenient and rapid vector operations, similar to those in Unity. It is well-suited for game development using Pygame and also useful for general mathematical applications. The library is open source and available on GitHub.

How it Works and Why It's Fast

The core mathematical logic and vector class are entirely written in C++. They are then utilized through CPython in Python, transforming it into a wrapper class that can be fully utilized in Python. Speaking of its power, here are some speed-related details, including a comparison with the popular numpy library in mathematics:

Methods/Functions Documentation

Constructor

from rivector import Vector2
vector = Vector2(x: float, y: float)
  • Parameters:
    • x: The x-component of the vector.
    • y: The y-component of the vector.

set Method

vector.set(x: float, y: float)
  • Parameters:
    • new_X: New value for the x-component.
    • new_Y: New value for the y-component.

Note

Modifies the current vector.

equals Method

vector.equals(b: Vector2)
  • Parameters:
    • b: Another Vector2 object for comparison.
  • Returns: true if the vectors are exactly equal; otherwise, false.

clamp_magnitude Method

vector.clamp_magnitude(max_length: float)
  • Parameters:
    • max_length: The maximum length for the vector.
  • Returns: A dynamically allocated array representing the clamped vector.

$$ \begin{align*} \text{magnitude} &= \sqrt{x^2 + y^2} \\ \text{if } \text{magnitude} &> \text{maxlength} \\ \text{factor} &= \frac{\text{maxlength}}{\text{magnitude}} \\ x &= x \times \text{factor} \\ y &= y \times \text{factor} \end{align*} $$

distance Method

vector.distance(b: Vector2)
  • Parameters:
    • b: Another Vector2 object.
  • Returns: The distance between the two vectors.

$$ \begin{align*} & \sqrt{(x-b.x_coord)^{2}+(y-b.y_coord)^{2}} \end{align*} $$

lerp_unclamped Method

vector.lerp_unclamped(a: Vector2, b: Vector2, t: float)
  • Parameters:
    • a: Starting Vector2.
    • b: Ending Vector2.
    • t: Interpolation factor (0.0 to 1.0).
  • Returns: A dynamically allocated array representing the interpolated vector.

$$ \begin{align*} x &= a.x_coord + (b.x_coord - a.x_coord) \cdot t \\ y &= a.y_coord + (b.y_coord - a.y_coord) \cdot t \end{align*} $$

max Method

vector.max(a: Vector2, b: Vector2)
  • Parameters:
    • a: First Vector2.
    • b: Second Vector2.
  • Returns: A dynamically allocated array representing the vector with the largest components.

min Method

vector.min(a: Vector2, b: Vector2)
  • Parameters:
    • a: First Vector2.
    • b: Second Vector2.
  • Returns: A dynamically allocated array representing the vector with the smallest components.

perpendicular Method

vector.perpendicular(a: Vector2)
  • Parameters:
    • a: The Vector2 object.
  • Returns: A dynamically allocated array representing the perpendicular vector.

move_towards Method

vector.move_towards(a: Vector2, b: Vector2, max_distance_delta: float)
  • Parameters:
    • a: Current position.
    • b: Target position.
    • max_distance_delta: Maximum distance to move towards the target.
  • Returns: A dynamically allocated array representing the new position.

$$ \begin{align*} & direction = (b - a).normalize() \\ & x = a.x_coord + direction.x * max_distancec_delta \\ & y = a.y_coord + direction.y * max_distancec_delta \end{align*} $$

reflect Method

vector.reflect(a: Vector2, b: Vector2)
  • Parameters:
    • a: Incident vector.
    • b: Normal vector.
  • Returns: A dynamically allocated array representing the reflected vector.

$$ \begin{align*} & parallel = (a * b) * b \\ & x = 2 * parallel.x - a.x_coord \\ & y = 2 * parallel.y - a.y_coord \\ \end{align*} $$

scale Method

vector.scale(a: Vector2, scale: float)
  • Parameters:
    • a: The Vector2 object.
    • scale: The scaling factor.
  • Returns: A dynamically allocated array representing the scaled vector.

$$ \begin{align*} & x = a.x_coord * scale \\ & y = a.y_coord * scale \\ \end{align*} $$

signed_angle Method

vector.signed_angle(a: Vector2, b: Vector2)
  • Parameters:
    • a: Starting Vector2.
    • b: Ending Vector2.
  • Returns: The signed angle in degrees between the two vectors.

$$ \begin{align*} \text{angle} &= \arctan2(ba, ab) \\ \text{signedangle} &= \text{angle} \end{align*} $$

smooth_damp Method

vector.smooth_damp(a: Vector2, b: Vector2, c: Vector2, smooth_time: float, max_speed: float, delta_time: float)
  • Parameters:
    • a: Current position.
    • b: Target position.
    • c: Current velocity.
    • smooth_time: Smoothing time.
    • max_speed: Maximum speed.
    • delta_time: Time since the last call.
  • Returns: A dynamically allocated array representing the smoothly interpolated position.

$$ \begin{align*} & difference = b - 1 \\ & spring_force = difference * smooth_time * max_speed \\ & damping_force = c * 2 * \sqrt{(smooth_time)} * sprintf_force \\ & acceleration = (spring_force + damping_force) * delta_time \\ & x = a.x_coord + difference.x + acceleration.x \\ & y = a.y_coord + difference.y + acceleration.y \end{align*} $$

to_list Method

vector.to_list()
  • Returns: A dynamically allocated array representing the vector.

$$ \begin{align*} & list = \left[ x, y \right] \end{align*} $$

sqrmagnitude Method

vector.sqrmagnitude
  • Returns: The squared magnitude of the vector.

$$ \begin{align*} & sqrmagnitude = x^{2}+y^{2} \end{align*} $$

magnitude Method

vector.magnitude
  • Returns: The magnitude of the vector.

$$ \begin{align*} & magnitude = \sqrt{x^{2}+y^{2}} \end{align*} $$

normalized Method

vector.normalized()
  • Returns: A dynamically allocated array representing the normalized vector.

$$ \begin{align*} & magnitude = \sqrt{x^{2}+y^{2}} \\ & x = x/magnitude \\ & y = y/magnitude \end{align*} $$

dot Method

vector.dot(a: Vector2, b: Vector2)
  • Parameters:
    • a: First Vector2.
    • b: Second Vector2.
  • Returns: The dot product of the two vectors.

$$ \begin{align*} \text{dot} &= a.x_coord \cdot b.x_coord + a.y_coord \cdot b.y_coord \end{align*} $$

angle Method

vector.angle(a: Vector2, b: Vector2)
  • Parameters:
    • a: First Vector2.
    • b: Second Vector2.
  • Returns: The angle in radians between the two vectors.

$$ \begin{align*} \text{angle} &= \arccos\left(\frac{a \cdot b}{|a| \cdot |b|}\right) \end{align*} $$

Getter methods

vector.y_coord
vector.x_coord
  • Returns: The x or y component of the vector, respectively.