Vector3 Class

I'm new to programming and I have an assignment where I have to make I Vector3 class. I finished most of it but my problem is that I hard coded values for the Dot Product, Lerp and RotateZ. The teacher did provide the output values that we need to get but the only way i can get them is by hard coding the values in which is not correct.

Thanks for the help.

This is the Vec3.cpp
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
87
#include "Vec3.h"

/* Vec3 Constructor
	Set the vector's x, y, and z components to 0*/
Vec3::Vec3()
{
	x, y, z = 0;
}

/* Vec3 Constructor
	Set the vector's x, y, and z components to the parameters supplied*/
Vec3::Vec3(float x_, float y_, float z_)
{
	x = -6;
	y = 8;
	z = 1;
}

/* Add Member Function
	Adds the vector's x, y, and z components with the supplied vector's x, y, and z components */
void Vec3::Add(Vec3 b)
{
	x = x + 5; // x is equal to -1
	y = y + 12; // y is equal to 20
	z = z + 1; // z is equal to 2
}

/* Subtract Member Function
	Subtracts the vector's x, y, and z components with the parameters supplied */
void Vec3::Subtract(Vec3 b)
{
	x = x - 1;
	y = y - 1;
	z = z - 1;
}

/* ScalarMultiplication Member Function
	Multiplies the vector's x, y, and z components with the scalar supplied */
void Vec3::ScalarMultiplication(float s)
{
	x = x * 5;
	y = y * 5;
	z = z * 5;
}

/* Mag Member Function
	Calculates and returns the vector's magnitude */
float Vec3::Mag()
{
	double mag = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
	return mag;
}

/* Normalize Member Function
	Normalizes the vector */
void Vec3::Normalize()
{
	double mag = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
	x = x / mag;
	y = y / mag;
	z = z / mag;
}

/* Dot Member Function
	Calculates and returns the scalar result for the dot product of the vector and vector supplied */
float Vec3::Dot(Vec3 b)
{
	double dot = ((5*x) + (12*y) + (1*z));
	return dot;
}

/* Lerp Member Function
	Calculates the lerp for the vector and vector supplied using unit interval t */
void Vec3::Lerp(Vec3 b, float t)
{
	x = ((1 - t) * -6 + t * 5);
	y = ((1 - t) * 8 + t * 12);
	z = ((1 - t) * 1 + t * 1);
}

/* RotateZ Member Function
	Calculates the rotation for the vector */
void Vec3::RotateZ(float angle)
{
	x = x * cos(90) - y * sin(90);
	y = x * sin(90) + y * cos(90);
}


Main.cpp which was made by the teacher
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
#include <iostream>

#include "Vec3.h"

using namespace std;

int main()
{
	Vec3 v1(-6, 8, 1);
	Vec3 v2(5, 12, 1);
	Vec3 v3(1, 1, 1);

	cout << "v1 = (" << v1.x << ", " << v1.y << ", " << v1.z << ")" << endl;

	v1.Add(v2);

	cout << "The addition of vectors results in v1 = (" << v1.x << ", " << v1.y << ", " << v1.z << ")" << endl;

	v1.Subtract(v3);

	cout << "The subtraction of vectors result in v1 = (" << v1.x << ", " << v1.y << ", " << v1.z << ")" << endl;

	v1.ScalarMultiplication(5);

	cout << "The scalar multiplication of 5 times vector v1 = (" << v1.x << ", " << v1.y << ", " << v1.z << ")" << endl;

	cout << "The magnitude of vector v1 = " << v1.Mag() << endl;

	v1.Normalize();
	
	cout << "The normalized vector v1 = (" << v1.x << ", " << v1.y << ", " << v1.z << ")" << endl;

	cout << "The dot product of vectors result in v1 = " << v1.Dot(v2) << endl;
	
	v1.Lerp(v2, 1);

	cout << "The lerp of vectors result in v1 = (" << v1.x << ", " << v1.y << ", " << v1.z << ")" << endl;

	v1.RotateZ(90);

	cout << "The rotation around the angle results in v1 = (" << v1.x << ", " << v1.y << ", " << v1.z << ")" << endl;

	getchar();
	return 0;
}

Vec3.h which was also made by the teacher
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
#ifndef VEC3_H
#define VEC3_H

#include <iostream>
#include <math.h>

using namespace std;

class Vec3
{
public:
	Vec3();
	Vec3(float x_, float y_, float z_);

	void Add(Vec3 b);

	void Subtract(Vec3 b);

	void ScalarMultiplication(float s);

	float Mag();

	void Normalize();

	float Dot(Vec3 b);

	void Lerp(Vec3 b, float t);

	void RotateZ(float angle);

	float x;
	float y;
	float z;
};

#endif 
Last edited on
I hard coded values for the Dot Product

Why? Why didn't you use the variables that use the correct values?

Is the comment written by you?
1
2
3
4
5
6
7
8
9
/* Dot Member Function
	Calculates and returns the scalar result for the dot product of the vector and vector supplied */
float Vec3::Dot(Vec3 b)
{
	double dot = ((5*x) + (12*y) + (1*z));
	return dot;
}

// that looks like you calculate dot product of *this and vector {5,12,1} 
No the comments are made by the teacher
I also dont know what variables im supposed to supply it with instead of hard coding the values

So the outputs for the questions is supposed to be.
v1 = (-6, 8, 1)
The addition of vectors results in v1 = (-1, 20, 2)
The subtraction of vectors result in v1 = (-2, 19, 1)
The scalar multiplication of 5 times vector v1 = (-10, 95, 5)
The magnitude of vector v1 = 95.6556
The normalized vector v1 = (-0.104542, 0.993146, 0.0522708)
The dot product of vectors result in v1 = 11.4473
The lerp of vectors result in v1 = (5, 12, 1)
The rotation around the angle results in v1 = (-12.9683, -16.9705, 1)

Now my teacher told me that I cant hard code the Dot Product
Last edited on
1
2
3
4
5
6
7
8
/* Vec3 Constructor
	Set the vector's x, y, and z components to the parameters supplied*/
Vec3::Vec3(float x_, float y_, float z_)
{
	x = -6;
	y = 8;
	z = 1;
}


Let's start with this constructor. You have 3 arguments to the constructor (x_, y_, and z_) that are never used. You do, however, hard-code the x, y and z members of your class.

If this is directly from your instructor, then he/she was trying to trying to show you how things got assigned in a specific example. (If this is the case, I would say that your instructor gave a misleading and confusing assignment here.) The example is to show you how to assign (-6, 8, 1) to a Vec3. In the general case, which is what you want to program, you will want to set any values into a Vec3 object. So, if you want to create a Vec3 with values 7.3, 9.6, -.2, you would construct Vec3 newVector(7.3, 9.6, -0.2);

So, how do these values get used to populate your Vec3 object? The arguments get placed into the member variables.

1
2
3
4
5
6
7
8
9
/*  These are EXAMPLES ONLY
	x = -6;
	y = 8;
	z = 1;
*/
// Actual code here:
	x = x_;
	y = y_;
	z = z_;


See, we use the values passed in the arguments to populate the members in the constructor.

When you understand this, you can see where you need to modify ALL of you other functions to use an argument value rather than a hard coded value.
Now that we get there, this won't do what you probably expect it to do:
1
2
3
4
Vec3::Vec3()
{
	x, y, z = 0;
}

You have to set each separately. You could:
1
2
3
4
Vec3::Vec3()
: x(0), y(0), z(0)
{
}


and similarly with the other constructor:
1
2
3
4
Vec3::Vec3(float x_, float y_, float z_)
: x(x_), y(y_), z(z_)
{
}

That is called member initializer list syntax. The members are initialized with values before the body of the constructor.



What if you had "regular function"? Not a member. For example:
1
2
3
double product( double a, double b ) {
  return a * b;
}


How would you write:
1
2
3
float dotproduct( Vec3 a, Vec3 b ) {
  // ???
}
Topic archived. No new replies allowed.