Vector3 code

This is a simple vector 'library' for managing 3d vectors in plain C as well as C++. It is intended to form the basic structure for vector support in simple raytracers. There are three versions of the code: a C version with structs, a C++ version with structs (and operator overloading), and a C++ class version. Example code for each version is presented below. Choose the one that best fits your coding style.

Please contact me if you find any errors in the code or have suggestions for improvements.

The code is free and public domain.
You may use it as you desire.
No credit is needed. Enjoy!

Bug reports are welcome, of course.

Vector3 - C only

Download files:
Pretty print of vector.c
Pretty printof vector.h
Code in a zip

void vector3_test()
{
	vector3 zero = {0,0,0};
	vector3 one = {1,1,1};
	vector3 y = {0,1,0};
	vector3 half = {0.5,0.5,0.5};
	vector3 a;
	
	vector3_invert(&a, &one);
	vector3_subtract(&a, &one, &a);
	vector3_add(&a, &a, &one);
	vector3_print(&a);
	
	vector3_multiply(&a, &one, 0.5);
	vector3_divide(&a, &a, 2);
	vector3_print(&a);
	
	vector3_reflect(&a, &one, &y);
	vector3_print(&a);
	
	vector3_scalar_sub(&a, &zero, -0.5);
	vector3_scalar_add(&a, &a, 0.5);
	vector3_print(&a);
	
	vector3_cross(&a, &one, &y);
	vector3_print(&a);
	
	srand(3);
	vector3_random(&a);
	vector3_print(&a);
	
	printf("%.2f %.2f\n", 
		   vector3_dot(&half, &y),
		   vector3_angle(&half, &y));
	
	printf("%.2f %.2f\n", 
		   vector3_distance(&one, &y),
		   vector3_distancesq(&one, &y));
	
	vector3_copy(&a, &one);
	printf("%.2f %.2f\n", 
		   vector3_length(&one),
		   vector3_length(vector3_normalize(&a)) );
}

Vector3 - C++ via struct

Download files:
Pretty print of vector.cpp
Pretty print of vector.h
Code in a zip

void vector3_test()
{
	vector3 zero = {0,0,0};
	vector3 one = {1,1,1};
	vector3 y = {0,1,0};
	vector3 half = {0.5,0.5,0.5};
	vector3 a;
	
	a = vector3_invert(one);
	a = one - a;
	a = a + one;
	vector3_print(a);
	
	a = one * 0.5;
	a = a / 2;
	vector3_print(a);
	
	a = vector3_reflect(one, y);
	vector3_print(a);
	
	a = zero - (-0.5);
	a = a + 0.5;
	vector3_print(a);
	
	a = vector3_cross(one, y);
	vector3_print(a);
	
	srand(3);
	a = vector3_random();
	vector3_print(a);
	
	printf("%.2f %.2f\n", 
		   vector3_dot(half, y),
		   vector3_angle(half, y));
	
	printf("%.2f %.2f\n", 
		   vector3_distance(one, y),
		   vector3_distancesq(one, y));
	
	a = vector3_copy(one);
	printf("%.2f %.2f\n", 
		   vector3_length(one),
		   vector3_length(vector3_normalize(a)) );
}

Vector3 - C++ via class

Download files:
Pretty print of vector.cpp
Pretty print of vector.h
Code in a zip

void vector3_test()
{
	vector3 zero = vector3(0,0,0);
	vector3 one = vector3(1,1,1);
	vector3 y = vector3(0,1,0);
	vector3 half = vector3(0.5,0.5,0.5);
	vector3 a;
	
	a = -one;
	a = one - a;
	a = a + one;
	a.print();
	
	a = one * 0.5;
	a = a / 2;
	a.print();
	
	a = one.reflect(y);
	a.print();
	
	a = zero - (-0.5);
	a = a + 0.5;
	a.print();
	
	a = one.cross(y);
	a.print();
	
	srand(3);
	a.randomize();
	a.print();
	
	printf("%.2f %.2f\n", 
		   half.dot(y), half.angle(y));
	
	printf("%.2f %.2f\n", 
		   one.distance(y), one.distancesq(y));
	
	a = one;
	printf("%.2f %.2f\n", 
		   one.length(), a.normalize().length() );
}