View on GitHub

A simple vector library written in plain C for educational purpose.

Introduction

This library is written in hope to simplify the coding process for non-professional vector computation.

This library should not be used for industrial or scientific use due to the limited precision and performance; Complex number is not supported either.

Usage

1. You may copy the files in ./include to your project directory and compile it seperately;

2. You may generate a single head file vector.h for your project:

This generate a single head file in the project root directory ./. Copy the ./vector.h to your project and include it.

Example

This is a brief demonstration on the commonly used functions’ usage. The output of each statement follows in comments.

Several practical examples are available in ./example.

Type

vector

The vector type contains components in three dimensions and the overall magnitude. All these data are in double type. Each time an operation is performed to a vector, the magnitude would update automatically.

You may get a component directly:

Functions

Arithmetic

vecAdd(A, B) returns the sum: A + B.

Subtraction

vecSub(A, B) returns the difference: A - B.

Negative

vecNeg(V) returns the negative: - V.

Modulus

vecMod(V) returns the modulus: | V |.

Unit Vector

vecUnit(V) returns the unit vector: ^ V.

Scalar Product

vecScl(V, s) returns the vector s * V.

Dot Product

vecDot(A, B) returns the dot product: A · B.

Cross Product

vecCrx(A, B) returns the Cross product: A × B.

Utilities

Initialization

There’re two ways to initialize a vector:

1. vecSet(x, y, z) returns a vector specify the coordinates (x,y,z).
2. vecInit() returns a vector with coordinates all of 0.0. However, you should use vecSet(0.0, 0.0, 0.0) instead for better readability.

Pretty Print

1. vecPrint(V) prints the vector in the form of <x, y, z> | modulus |, 2 decimal places by default
2. vecnPrint(V, p) using the specified precision p. If p is 0, print in exponential form.