Skip to content

Commit 47847b3

Browse files
committed
Added version 1 of class Vector2D with basic behaviour(NO BUG)
1 parent 01e7af0 commit 47847b3

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

Vector2D.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include "Vector2D.h"
2+
3+
Vector2D::Vector2D()
4+
{
5+
m_x = 0;
6+
m_y = 0;
7+
}
8+
9+
Vector2D::Vector2D(float x, float y)
10+
{
11+
m_x = x;
12+
m_y = y;
13+
}
14+
15+
Vector2D Vector2D::operator+(const Vector2D& v2)
16+
{
17+
return Vector2D(m_x + v2.m_x, m_y + v2.m_y);
18+
}
19+
20+
Vector2D Vector2D::operator+=(const Vector2D& v2)
21+
{
22+
m_x += v2.m_x;
23+
m_y += v2.m_y;
24+
25+
return *this; //we return the self object.
26+
}
27+
28+
Vector2D Vector2D::operator-(const Vector2D& v2)
29+
{
30+
return Vector2D(m_x - v2.m_x, m_y - v2.m_y);
31+
}
32+
33+
Vector2D Vector2D::operator-=(const Vector2D& v2)
34+
{
35+
m_x -= v2.m_x;
36+
m_y -= v2.m_y;
37+
38+
return *this;
39+
}
40+
41+
Vector2D Vector2D::operator*(float scalar)
42+
{
43+
return Vector2D(m_x * scalar, m_y * scalar);
44+
}
45+
46+
Vector2D Vector2D::operator*=(float scalar)
47+
{
48+
m_x *= scalar;
49+
m_y *= scalar;
50+
51+
return *this;
52+
}
53+
54+
Vector2D Vector2D::operator/(float scalar)
55+
{
56+
return Vector2D(m_x / scalar, m_y / scalar);
57+
}
58+
59+
Vector2D Vector2D::operator/=(float scalar)
60+
{
61+
m_x /= scalar;
62+
m_y /= scalar;
63+
64+
return *this;
65+
}
66+
67+
float Vector2D::getX() const
68+
{
69+
return m_x;
70+
}
71+
72+
float Vector2D::getY() const
73+
{
74+
return m_y;
75+
}
76+
77+
float Vector2D::length() const
78+
{
79+
return sqrt(m_x * m_x + m_y * m_y);
80+
}
81+
82+
void Vector2D::setX(float x)
83+
{
84+
m_x = x;
85+
}
86+
87+
void Vector2D::setY(float y)
88+
{
89+
m_y = y;
90+
}
91+
92+
void Vector2D::normalize()
93+
{
94+
float aux_length = length();
95+
if (aux_length > 0) //don't divide 0!
96+
{
97+
(*this) *= 1 / aux_length;
98+
}
99+
}

Vector2D.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef VECTOR2D_H
2+
#define VECTOR2D_H
3+
4+
#include<math.h>
5+
6+
class Vector2D
7+
{
8+
public:
9+
//Constructors
10+
Vector2D();
11+
Vector2D(float x, float y);
12+
13+
//Operators
14+
Vector2D operator+(const Vector2D& v2);
15+
Vector2D operator+=(const Vector2D& v2);
16+
Vector2D operator-(const Vector2D& v2);
17+
Vector2D operator-=(const Vector2D& v2);
18+
Vector2D operator*(float scalar);
19+
Vector2D operator*=(float scalar);
20+
Vector2D operator/(float scalar);
21+
Vector2D operator/=(float scalar);
22+
23+
24+
//GETs
25+
float getX() const;
26+
float getY() const;
27+
float length() const; //lenght of the vector = sqrt(x²+y²)
28+
29+
//Methods
30+
void setX(float x); //change x value
31+
void setY(float y); //change y value
32+
33+
void normalize(); //make vector length = 1
34+
35+
private:
36+
37+
float m_x;
38+
float m_y;
39+
};
40+
41+
#endif // VECTOR2D_H

0 commit comments

Comments
 (0)