Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Simple 2D/3D vector library
New Posts  All Forums:Forum Nav:

# Simple 2D/3D vector library

I'm doing a computer modelling course, which means I need a vector/matrix library. Now, I'm really lazy, so most of the time I just find one on the net or use the TA's.

The problem is they are always super ugly, since C++ is usually a very unattractive language, and the libraries are built with no care given to code readability. They also usually have a ton of stuff that has absolutely no use in computer graphics.

I absolutely detest ugly code, so I decided to build a vector library that is simple and concise. Its also nice to brush up on the theory as well.

This is what I have so far. I'm just building it as I go, so its not completely finished. Its accurate as far as I've tested it.

Code:
``````#ifndef VECTOR_H
#define VECTOR_H
/*
* C++ Vector library using STL
* Author: nathris
*/

#include <math.h>

/* 2D Vector template
* Implements: + - dot length scalar mult
* Aliases: Vec2i (integer) Vec2f (float)
*/
template <class Type>
class Vec2
{
public:
Type x,y;
Vec2 () {};
Vec2(Type _x, Type _y) { x = _x; y = _y; }
void normalize() { Type mag = length(); x = x/mag; y = y/mag; }
Type length() { return sqrt( (x*x) + (y*y) ); }
Type dot(Vec2 v2) {     return x*v2.x + y*v2.y; }
Vec2 operator+ (Vec2 param) { return Vec2(x + param.x, y + param.y); }
Vec2 operator- (Vec2 param) { return Vec2(x - param.x, y - param.y); }
Vec2 operator* (Type s) { return Vec2(s*x,s*y); }
};
typedef Vec2<float> Vec2f;
typedef Vec2<int> Vec2i;

/* 3D Vector
* Implements: + - dot length scalar mult
* Aliases: Vec3i (integer) Vec3f (float)
*/
template <class Type>
class Vec3
{
public:
Type x,y,z;
Vec3 () {};
Vec3(Type _x, Type _y, Type _z) { x = _x; y = _y; z = _z; }
void normalize() { Type mag = length(); x = x/mag; y = y/mag; z = z/mag; }
Type length() { return sqrt( (x*x) + (y*y) + (z*z) ); }
Type dot(Vec3 v2) { return x*v2.x + y*v2.y + z*v2.z; }
Vec3 operator+ (Vec3 v2) { return Vec3(x + v2.x, y + v2.y, z + v2.z); }
Vec3 operator- (Vec3 v2) { return Vec3(x - v2.x, y - v2.y, z - v2.z); }
Vec3 operator* (Type s) { return Vec3(s*x,s*y,s*z); }
};
typedef Vec3<float> Vec3f;
typedef Vec3<int> Vec3i;
#endif``````
The syntax resembles OpenGL/GLSL syntax, since as a graphics programmer thats what I'm more familiar with. It works for 2D/3D vectors of float and int. I haven't tested it with other types, but as long as they implement the necessary operations it should work.

The only thing I'm not happy with is scalar multiplication. Technically the scalar should come first, but I couldn't figure out how to do that, so it has to come second.

I should probably wrap it in a namespace as well to prevent conflicts, but I don't really care right now.

I'm also building a 3x3/4x4 matrix library as well, but its not ready yet.
 Half Broke/Half Broken (19 items)
CPUMotherboardGraphicsRAM
Core i5 4670k ASUS Maximus VI Gene Gigabyte GTX 460 1GB Kingston Hyper-X
Hard DriveHard DriveHard DriveHard Drive
Samsung 830 OCZ Vertex 3 WD6401AALS WD5000AAKS
CoolingOSMonitorMonitor
Noctua NH-D14 elementary OS Dell Ultrasharp U2312HM LG W2442PA-BF
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750W Corsair Graphite 600T Logitech G700
Audio
ASUS Xonar DG
 Half Broke/Half Broken (19 items)
CPUMotherboardGraphicsRAM
Core i5 4670k ASUS Maximus VI Gene Gigabyte GTX 460 1GB Kingston Hyper-X
Hard DriveHard DriveHard DriveHard Drive
Samsung 830 OCZ Vertex 3 WD6401AALS WD5000AAKS
CoolingOSMonitorMonitor
Noctua NH-D14 elementary OS Dell Ultrasharp U2312HM LG W2442PA-BF
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750W Corsair Graphite 600T Logitech G700
Audio
ASUS Xonar DG
I recommend that you create a version of Normalize that returns the length of the vector prior to normalization, when doing vector math I often want the length of a vector right before I normalize it. Having your Normalize function return the length saves the user from requiring two sqrts. In your case, just return Mag in your Normalize function.

Additionally, if you multiply x and y by 1/Mag in your Normalize function you trade a div for two muls, which I believe is generally faster, depends on how smart the compiler is I guess.

Here's a link to a simple 3D vector class I wrote years ago:
http://www.nfostergames.com/Samples/3DVectorSample.htm

Seems like yours is going great though.
Edited by lordikon - 10/6/11 at 8:50pm
 Foldatron (17 items) Mat (10 items) Work iMac (9 items)
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000
PowerCase
750W PC P&C Silencer CoolerMaster 690
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard
Mouse
Mac wireless mouse
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display
Monitor
27" Apple thunderbolt display
 Foldatron (17 items) Mat (10 items) Work iMac (9 items)
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000
PowerCase
750W PC P&C Silencer CoolerMaster 690
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard
Mouse
Mac wireless mouse
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display
Monitor
27" Apple thunderbolt display
Also on top of what lordikon said, your missing a important equation the cross product. Some other functions that are handy to have are mid point, min, max, rotate. With your constructor i suggest adding a copy constructor, it will come in handy.

I suggest you look into inline, const, and pass by reference. These will help speed up your vector class.

Otherwise its looking good.
 UberMini (13 items)
CPUMotherboardGraphicsRAM
Intel i7 870 Gigabyte H55N-USB3 MSI HD 6870 G.Skill Ripjaws 4GB CL8
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB, 2TB Samsung F4 Sony slim burner Windows 7 64Bit/Ubuntu 11.04 Benq 24" + ASUS 22" 2MS
KeyboardPowerCaseMouse
Logitech G15 Silverstone 600watt 85+ Silverstone SG07 Razer CopperHead
Razer Mantis Speed
 UberMini (13 items)
CPUMotherboardGraphicsRAM
Intel i7 870 Gigabyte H55N-USB3 MSI HD 6870 G.Skill Ripjaws 4GB CL8
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB, 2TB Samsung F4 Sony slim burner Windows 7 64Bit/Ubuntu 11.04 Benq 24" + ASUS 22" 2MS
KeyboardPowerCaseMouse
Logitech G15 Silverstone 600watt 85+ Silverstone SG07 Razer CopperHead
Razer Mantis Speed
Like I said I'm building it incrementally over the semester, so it will get larger as my assignments get harder. That version doesn't even have comparison.
 Half Broke/Half Broken (19 items)
CPUMotherboardGraphicsRAM
Core i5 4670k ASUS Maximus VI Gene Gigabyte GTX 460 1GB Kingston Hyper-X
Hard DriveHard DriveHard DriveHard Drive
Samsung 830 OCZ Vertex 3 WD6401AALS WD5000AAKS
CoolingOSMonitorMonitor
Noctua NH-D14 elementary OS Dell Ultrasharp U2312HM LG W2442PA-BF
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750W Corsair Graphite 600T Logitech G700
Audio
ASUS Xonar DG
 Half Broke/Half Broken (19 items)
CPUMotherboardGraphicsRAM
Core i5 4670k ASUS Maximus VI Gene Gigabyte GTX 460 1GB Kingston Hyper-X
Hard DriveHard DriveHard DriveHard Drive
Samsung 830 OCZ Vertex 3 WD6401AALS WD5000AAKS
CoolingOSMonitorMonitor
Noctua NH-D14 elementary OS Dell Ultrasharp U2312HM LG W2442PA-BF
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750W Corsair Graphite 600T Logitech G700
Audio
ASUS Xonar DG
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Coding and Programming
• Simple 2D/3D vector library
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Simple 2D/3D vector library