libcvec is a lightweight C library for 2D and 3D vector math. It provides support for multiple numeric types, including integers, unsigned integers, longs, unsigned longs, floats, and doubles. The library implements basic vector operations such as addition, subtraction, Hadamard multiplication, scalar multiplication/division, dot product, length, and normalization (for floating-point types).
.
├── Makefile # Build script for library and tests
├── README.md # This file
├── src
│ ├── Vec2 # 2D vector implementation files
│ │ ├── Vec2.h # Header file for Vec2 types and functions
│ │ ├── Vec2d.c # Vec2 operations for double
│ │ ├── Vec2f.c # Vec2 operations for float
│ │ ├── Vec2i.c # Vec2 operations for int
│ │ ├── Vec2l.c # Vec2 operations for long
│ │ ├── Vec2lu.c # Vec2 operations for unsigned long
│ │ └── Vec2ui.c # Vec2 operations for unsigned int
│ └── Vec3 # 3D vector implementation files
│ ├── Vec3.h # Header file for Vec3 types and functions
│ ├── Vec3d.c # Vec3 operations for double
│ ├── Vec3f.c # Vec3 operations for float
│ ├── Vec3i.c # Vec3 operations for int
│ ├── Vec3l.c # Vec3 operations for long
│ ├── Vec3lu.c # Vec3 operations for unsigned long
│ └── Vec3ui.c # Vec3 operations for unsigned int
└── tests # Test files for library functions
├── Vec2_tests.c # Unit tests for Vec2 functionality
├── Vec3_tests.c # Unit tests for Vec3 functionality
├── tests.c # Main test runner
└── tests.h # Header for tests
- 2D Vectors (
Vec2) and 3D Vectors (Vec3) Support for various numeric types:- Integer types:
int,unsigned int,long,unsigned long - Floating-point types:
float,double
- Integer types:
- Operations provided:
- Initialization: Create vectors with specific coordinates.
- Arithmetic: Addition, subtraction, scalar multiplication, and scalar division.
- Component-wise operations: Hadamard multiplication.
- Properties: Length (magnitude) and dot product.
- Normalization: (Floating-point vectors only)
This project uses a Makefile to build the library and run tests.
-
Building the Library
To compile the library and generate a static archive (
libcvec.a), run:make
The compiled objects and the archive will be placed in the
builddirectory. Additionally, the header files (Vec2.handVec3.h) are copied into that directory. -
Running Tests
To compile and run the tests, simply run:
make testThis command compiles the tests using the library and executes the test binary. All tests (for both 2D and 3D vectors) should pass if the library functions correctly.
-
Cleaning Up
To remove generated files and clean up the build directories, run:
make clean
To use libcvec in your own project:
-
Include the Headers
Include the appropriate header files in your source files:
#include "Vec2.h" // For 2D vectors #include "Vec3.h" // For 3D vectors
-
Linking
When compiling your project, link against the generated library (e.g.,
libcvec.a). For example:gcc -Wall -Wextra -std=c99 -o myapp myapp.c -L/path/to/build -lcvec
-
Example
Here's a simple example using a 2D vector of floats:
#include "Vec2.h" int main(void) { // Initialize two 2D float vectors struct Vec2f a = Vec2f_init(1.0f, 2.0f); struct Vec2f b = Vec2f_init(3.0f, 4.0f); // Add the vectors struct Vec2f result = Vec2f_add(a, b); printf("Result: (%f, %f)\n", result.x, result.y); // Normalize vector a struct Vec2f norm = Vec2f_normalize(a); printf("Normalized a: (%f, %f)\n", norm.x, norm.y); return 0; }
Feel free to fork the repository and submit pull requests with bug fixes, improvements, or new features. Please ensure that your changes are accompanied by appropriate tests.