N-Dimensional Point Library
A simple, generic N-dimensional point library in Rust. This library provides a Point struct that can be used to represent points in an N-dimensional space. It supports various mathematical operations like addition, subtraction, multiplication, and division for both point-point and point-scalar operations.
Features
- Generic N-dimensional Point: Create points of any dimension with any numeric type.
- Basic Point Operations:
new(p: Vec<T>): Creates a new point.dim() -> usize: Returns the dimension of the point.dist() -> f64: Calculates the Euclidean distance from the origin.apply(func: F) -> f64: Applies a custom function to the point's data.data() -> &[T]: Returns a slice of the point's data.
- Operator Overloading: Intuitive arithmetic operations.
- Point-Point Operations:
+,-,* - Scalar Operations:
+,-,*,/
- Point-Point Operations:
- Ownership and Borrowing: Operations are implemented for both owned types (
Point<T>) and references (&Point<T>).
Getting Started
Creating a Point
You can create a new Point from a Vec<T>, where T is a numeric type.
use cratePoint;
// Create a 3-dimensional point with integer coordinates
let p1 = new;
// Create a 2-dimensional point with float coordinates
let p2 = new;
Usage
Basic Methods
Here are some of the basic methods available for Point instances:
// Get the dimension of the point
println!; // Output: 3
// Calculate the distance from the origin
println!; // Output: 3.7416573867739413
// Get a slice of the point's data
let data = p1.data;
println!; // Output: [1, 2, 3]
// Apply a custom function to the point's data
let sum = p1.apply;
println!; // Output: 6.0
Point Arithmetic
The library supports arithmetic operations between points of the same dimension.
let p1 = new;
let p2 = new;
// Addition
let p_add = &p1 + &p2;
println!; // Output: [5, 7, 9]
// Subtraction
let p_sub = &p1 - &p2;
println!; // Output: [-3, -3, -3]
// Multiplication (element-wise)
let p_mul = &p1 * &p2;
println!; // Output: [4, 10, 18]
Scalar Arithmetic
You can also perform arithmetic operations between a Point and a scalar value.
let p = new;
// Scalar Addition
let p_add_scalar = &p + 10;
println!; // Output: [11, 12, 13]
// Scalar Subtraction
let p_sub_scalar = &p - 5;
println!; // Output: [-4, -3, -2]
// Scalar Multiplication
let p_mul_scalar = &p * 2;
println!; // Output: [2, 4, 6]
// Scalar Division
let p_div_scalar = &new / 10;
println!; // Output: [1, 2, 3]
Testing
To run the tests and verify that everything is working correctly, you can use the following command: