vector class?

Hay all. At the moment I am trying to make my own 2D vector library just to get a better understanding of vectors, I like the <Vector.h> standard library in Microsoft visual studios 2010 but I would like to be able to make my own version of it. The features I want from it are functions like Dot product,magnitude and the four basic operators like +, -, * ,/. Any way I am stuck on how to do it so I was wondering if anyone knows any where I can learn some more about it or give me some of your own knowledge =D

So far I have this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "StdAfx.h"
#include <math.h>
#include <stdio.h>
#include "Vector2.h"

Vector2::Vector2()
{
	/*m_fLength = (pow(x,x) + pow(y,y));
	m_fLength = sqrt(m_fLength);*/
}

Vector2::~Vector2()
{

}

void Vector2::Addition()
{
	
}

void Vector2::Subtract()
{
	
}

void Vector2::Divide()
{

}


void Vector2::Multiply()
{
	
}

void Vector2::Dot(const Vector2 &right)
{
	returnValue = (x*right.x + y*right.y);
	
	//check
	/*x = ((x * y) + (y * x));
	y = ((y * x) + (x * y));*/
}

void Vector2::Magnitude()
{
	// return sqrt(x*x + y*y)
	length = sqrt(x*x + y*y);
}

void Vector2::Normalize()
{
	length = x/length;
	length = y/length;

	//x = x / m_fLength;
	//y = y / m_fLength;
}




that's my vector .cpp file

and here is my header file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

#ifndef _VECTOR_2_H_
#define _VECTOR_2_H_

class Vector2
{
public:

	float x, y;

	Vector2(); // x and y set to 0
	Vector2(float X, float Y); // x and y set to X and Y

	~Vector2();

	void Normalise();
	Vector2 GetNormalised();

	float Length();

	void Dot(const Vector2 &right);
	void Divide();
	void Subtract();
	void Multiply();
	void Addition();

	void Magnitude();
	void Normalize();

	Vector2 GetPerpendicularLeft();
	Vector2 GetPerpendicularRight();

	// operator overloads
	Vector2  operator * (float val);
	Vector2 &operator *=(float val);
	Vector2  operator / (float val);
	Vector2 &operator /=(float val);

	Vector2  operator + (const Vector2 &right)
	{
		x + right.x;
		y + right.y;
		return *this;
	}
	Vector2 &operator +=(const Vector2 &right)
	{
		x += right.x;
		y += right.y;
		return *this;
	}
	Vector2  operator - (const Vector2 &right)
	{
		x - right.x;
		y - right.y;
		return *this;
	}
	Vector2 &operator -=(const Vector2 &right)
	{
		x -= right.x;
		y -= right.y;
		return *this;
	}

	bool operator == (const Vector2 &right)
	{
		x == right.x;
		y == right.y;
		return this;
	}
	bool operator != (const Vector2 &right)
	{
		x != right.x;
		y != right.y;
		return this;
	}

	float GetAngleBetween(Vector2 vec = Vector2(0, 1)); //return the angle between this vector and vec

private:
	float length; 
	float returnValue;

};

#endif 


Thanks for your time!.

Any way I am stuck on how to do it


Stuck on how to do what part, exactly?

Also, your operators are mostly wrong...have you written many programs before this?
i figured they were wrong thus the post and i have written a few things and most of my stuff works but I don't particularly understand vectors and operators.

That's why I wanna write my own library to understand them better but I believe I am doing it wrong. so to under what I am stuck on i would say most of it lol >.<.

I cant find anywhere they teaches me about vectors except for the standard std/stl library one
If I were you, I would leverage complex numbers in your code -- if you store complex<float> instead of two different floats, you get all arithmetic, and magnitude, and some other stuff for free.

Otherwise, remember that binary ops are almost always implemented as non-member functions, and since you provide compound assignments as well, binary ops should be implemented in terms of compounds:

1
2
3
4
Vector2  operator + (Vector2 left, const Vector2& right)
{
    return left += right; // the canonical addition operator is done this way
}


PS: oh, and of course length and returnValue should not be in your class
Last edited on
Topic archived. No new replies allowed.