14 Replies - 1761 Views - Last Post: 06 February 2012 - 03:50 PM Rate Topic: -----

#1 starkhavenprincessrin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Vector class(using real vectors) don't understand

Posted 05 February 2012 - 03:10 PM

Hi,

So I'm convinced that my current teachers really shouldn't be teaching the class that I'm in because I really don't understand this lab and it's due on Wednesday the 8th.

If I could just have some suggestions on exactly what I supposed to do to get this program going. Doesn't have to be much maybe just an explanation on what this calls for.

Here's what the assignment says:

Learning Objectives

Create a C++ class that defines a vector object in homogeneous coordinates, and provides functions which determine angles associated with that vector.
Introduction

Vectors are a convenient and concise way of representing both location and motion in either 2D or 3D space. The attributes of a vector, and operations performed on vectors, are basic computational necessities. For this reason, a vector class that provides this information will be extremely useful. In this lab, you shall write a C++ class to work with vectors, and in future labs you will extend the functions of this class.

Separate classes for 2D and 3D vectors are not necessary. The xy-plane is already embedded in the 3D space, and this plane is simply a copy of the 2D space. Therefore, to represent a 2D vector in a 3D space, it is sufficient to set the third coordinate of the 3D space, z, equal to zero. (This will shorten our work for class, but may not always be the preferred solution in an actual game.)

It is actually easier to work with 3D vectors that have four components instead of three. Certainly this is not obvious now, but later it will allow us to represent translations of objects using matrix multiplication. When 4D vectors are used for 3D space, the coordinates are called homogeneous coordinates. Our book introduces homogeneous coordinates in chapter 6, but without specifying the name of the system. We shall refer to the fourth coordinate as the w-coordinate, and its value will always be equal to one.

There are many different formulas that relate vectors to angles. In three dimensions, they include angles made with each of the three axes, conversions from rectangular to Magnitude-Heading-Pitch, and vice versa. Specifically, the 3D formulas are:

cosα=vx∥v∥
cosβ=vy∥v∥
cosγ=vz∥v∥
vx=∥v∥cosPcosH
vy=∥v∥cosPsinH
vz=∥v∥sinP
∥v∥=v2x+v2y+v2z−−−−−−−−−−√
sinP=vz∥v∥
cosH=vxv2x+v2y−−−−−−√

The 2D formulas are actually special cases of these 3D formulas.

In this lab, you will create a C++ class that will define vectors and perform some simple vector computations, and any C++ program may call upon the services of this class. Since C++ already has a class called ‘vector’ in the Standard Template Library (whose purpose is not mathematical), you need to use a different name for your class (vector3D, for example). Usually the class would be its own file, but in order for us to ease into the concept of classes we will start off by having the class defined within our program.

The Vector3D Class
Your Vector3D class should have the following functions
Publicly

A constructor Vector3D. When the constructor is called it should set default values of <0,0,0,1> for the vector.
Two functions called SetRectGivenRect that will set the rectangular coordinates for the vector given an input of rectangular coordinates. One for 2D and one for 3D.
A function called setRectGivenPolar that will set the rectangular coordinates given the magnitude and heading (2D case)
A function called setRectGivenMagHeadPitch that will set the rectangular coordinates for the vector given an input of the magnitude, heading and pitch.(3D case)
A function called printRect that prints the rectangular coordinates to the screen.
A function called printMagHeadPitch that prints the magnitude, heading and pitch to the screen.
A function called printAngles that prints the angles alpha, beta and gamma that the vector makes with the three positive axes.
A function called getMagnitude that returns the magnitude of the vector.
A function called getPitch that returns the pitch of the vector.
A function called getHeading that returns the heading of the vector.
A function called getAlpha that returns the angle alpha
A function called getBeta that returns the angle beta
A function called getGamma that returns the angle gamma.
A function called getX that returns the x-coordinate.
A function called getY that returns the y-coordinate.
A function called getZ that returns the z-coordinate.

Privately

You should store the rectangular coordinates of the vector private to the class.

Defining the Vector3D Class

The class should be defined before your main. The syntax is as follows

Class Vector3D

class Vector3D
{
Public:
return_type Vector3D(input_type) \\Constructor for the class Vector3D
     {
          \\The code here is executed every time a Vector3D is created
     }
return_type FunctionName(input_type) \\Public Function for the class Vector3D
     {
          \\The code here is executed every time this function is called
     }
Private:
variable_type variableName \\Private Variable Declaration
return_type FunctionName(input_type) \\Private Function for the class Vector3D
     {
          \\The code here is executed every time the function is called.
          \\This function can only be called from inside the class
     }
};


Prelab Questions and Exercises

What will be the 4D homogeneous coordinates of the 3D vector (x, y, z)? Also, what will be the 4D homogeneous coordinates of the 2D vector (x, y)?
What formulas will be needed for the 4 set functions that Vector3D will have?
What modifications are needed so that your class can also handle the instantiation of 2D vectors?
Four of the nine formulas given in the introduction explicitly define the quantity (the formula is already solved for the particular variable of interest). These formulas are easy to implement. But the other formulas are written in terms of a trig function of an angle. Identify any cases that need to be addressed when solving for the angle in each formula.
Some of the formulas may need a degree-radian conversion, or vice versa. Explain how you plan to implement this conversion.

Laboratory Procedures

1. Create a C++ program that asks the user to enter a vector (either 2D or 3D) in rectangular components, calls upon the C++ class for its computations, then outputs using printRect, printMagHeadPitch, and printAngles.
2. Create a second C++ program that asks the user to enter a vector described by its magnitude, heading and pitch and then calls upon the C++ class for its computations, , then outputs using printRect, printMagHeadPitch, and printAngles.

(Do NOT combine this program with the previous program. These should be separate programs)

Since we are defining the class in the program, you can just cut and paste your code for the Vector3D class from the first program into the second program

3. Execute each of the two programs above, for your instructor’s verification. (Both programs should be ready to demonstrate at the same time, so it is clear that they are calling on the same class – and no changes were made in the interim.)
a. program in #2: initials _
b. program in #3: initials _
4. Save your class files! You will use them again in future labs (including the very next lab).




As I said my teachers have been no help at all, mostly due to the fact that they are teaching a programming class but neither of them have any programming experience.

:code:

This post has been edited by jimblumberg: 05 February 2012 - 04:28 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Vector class(using real vectors) don't understand

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Vector class(using real vectors) don't understand

Posted 05 February 2012 - 03:22 PM

Quote

As I said my teachers have been no help at all, mostly due to the fact that they are teaching a programming class but neither of them have any programming experience.
That's unfortunate. But we don't do homework here.
Was This Post Helpful? 0
  • +
  • -

#3 starkhavenprincessrin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Vector class(using real vectors) don't understand

Posted 05 February 2012 - 03:34 PM

View PostOler1s, on 05 February 2012 - 03:22 PM, said:

Quote

As I said my teachers have been no help at all, mostly due to the fact that they are teaching a programming class but neither of them have any programming experience.
That's unfortunate. But we don't do homework here.


I'm not asking for someone to do the homework. If I knew what to do I'd put up some code for it, but I'm completely lost on what this wants me to do.

All I'm looking of is an explanation on what this is asking for, since my teachers didn't explain at all.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Vector class(using real vectors) don't understand

Posted 05 February 2012 - 03:46 PM

Here's an example that might make it clearer:
using namespace std;

#include <iostream>

class Vector3D {

private:
	double x;
	double y;
	double z;
	
public:
	Vector3D() 	{
		x = 10;
		y = 20;
		z = 30;
	}
	
	double getx() {
		return x;
	}
	
	double gety() {
		return y;
	}
	
	double getz() {
		return z;
	}
	
};

int main (int argc, char * const argv[]) {
	Vector3D vec;
	cout << vec.getx() << " \t" << vec.gety() << " \t" << vec.getz() << endl;
	return 0;
}


If you can see how it does what it does then the rest should be easy.
Was This Post Helpful? 1
  • +
  • -

#5 starkhavenprincessrin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Vector class(using real vectors) don't understand

Posted 05 February 2012 - 03:49 PM

View PostCTphpnwb, on 05 February 2012 - 03:46 PM, said:

Here's an example that might make it clearer:
using namespace std;

#include <iostream>

class Vector3D {

private:
	double x;
	double y;
	double z;
	
public:
	Vector3D() 	{
		x = 10;
		y = 20;
		z = 30;
	}
	
	double getx() {
		return x;
	}
	
	double gety() {
		return y;
	}
	
	double getz() {
		return z;
	}
	
};

int main (int argc, char * const argv[]) {
	Vector3D vec;
	cout << vec.getx() << " \t" << vec.gety() << " \t" << vec.getz() << endl;
	return 0;
}


If you can see how it does what it does then the rest should be easy.


Oh so you create the class, and then call it in the main function. The way my teachers where telling us to do it was like we had to make the class in a completely different program or something.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Vector class(using real vectors) don't understand

Posted 05 February 2012 - 04:00 PM

You don't call the class at all. You create an instance of it and then call its methods. Where the class definition resides is unimportant. It could easily be in a header file, for example.
Was This Post Helpful? 1
  • +
  • -

#7 starkhavenprincessrin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Vector class(using real vectors) don't understand

Posted 05 February 2012 - 04:04 PM

View PostCTphpnwb, on 05 February 2012 - 04:00 PM, said:

You don't call the class at all. You create an instance of it and then call its methods. Where the class definition resides is unimportant. It could easily be in a header file, for example.


so by creating the instance and placing methods in it, I can then call the methods of the class in the main function.

So all the real work has to be done with in the class and it's methods, while main is pretty much just used for calling the methods up?
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Vector class(using real vectors) don't understand

Posted 05 February 2012 - 04:09 PM

Yes, you call the class methods through the instance of the class. It doesn't all have to be in main() though. You could have another function that you pass a reference to the instance and it's methods could be called from there:
using namespace std;

#include <iostream>

class Vector3D {

private:
	double x;
	double y;
	double z;
	
public:
	Vector3D() 	{
		x = 10;
		y = 20;
		z = 30;
	}
	
	double getx() {
		return x;
	}
	
	double gety() {
		return y;
	}
	
	double getz() {
		return z;
	}
	
};

double average(Vector3D &Avector)
{
	double sum = Avector.getx() + Avector.gety() + Avector.getz();
	return sum / 3.0;
}

int main (int argc, char * const argv[]) {
	Vector3D vec;
	cout << vec.getx() << " \t" << vec.gety() << " \t" << vec.getz() << endl;
	cout << average(vec) << endl;
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#9 starkhavenprincessrin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Vector class(using real vectors) don't understand

Posted 05 February 2012 - 04:16 PM

View PostCTphpnwb, on 05 February 2012 - 04:09 PM, said:

Yes, you call the class methods through the instance of the class. It doesn't all have to be in main() though. You could have another function that you pass a reference to the instance and it's methods could be called from there:
using namespace std;

#include <iostream>

class Vector3D {

private:
	double x;
	double y;
	double z;
	
public:
	Vector3D() 	{
		x = 10;
		y = 20;
		z = 30;
	}
	
	double getx() {
		return x;
	}
	
	double gety() {
		return y;
	}
	
	double getz() {
		return z;
	}
	
};

double average(Vector3D &Avector)
{
	double sum = Avector.getx() + Avector.gety() + Avector.getz();
	return sum / 3.0;
}

int main (int argc, char * const argv[]) {
	Vector3D vec;
	cout << vec.getx() << " \t" << vec.gety() << " \t" << vec.getz() << endl;
	cout << average(vec) << endl;
	return 0;
}



Ah, okay I get it, so even though the class will have most of the important stuff, other functions can use what is with in the class and be called in the main. So all the work doesn't have to be in the class.

Thanks I understand what it is I should do now, your explanation of it was simple but what I really needed to get my mind around what I need to do.
Was This Post Helpful? 0
  • +
  • -

#10 starkhavenprincessrin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Vector class(using real vectors) don't understand

Posted 05 February 2012 - 04:51 PM

@CTphpnwb

What if I wanted to enter a value into one of the class functions, like the user was to enter a vector value, and I wanted to use those values as the new x, y , and z values. Would I be able to feed the new values into the class, or would that require a function that is outside of the class?
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Vector class(using real vectors) don't understand

Posted 05 February 2012 - 05:09 PM

That's what you need to write methods to handle. Pass the value to the method and let the method set the variable.
Was This Post Helpful? 0
  • +
  • -

#12 starkhavenprincessrin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Vector class(using real vectors) don't understand

Posted 05 February 2012 - 05:17 PM

View PostCTphpnwb, on 05 February 2012 - 05:09 PM, said:

That's what you need to write methods to handle. Pass the value to the method and let the method set the variable.



Got it, thanks again!
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,235
  • Joined: 23-August 08

Re: Vector class(using real vectors) don't understand

Posted 06 February 2012 - 04:24 AM

Quote

As I said my teachers have been no help at all, mostly due to the fact that they are teaching a programming class but neither of them have any programming experience.


NOTE: This post is not meant to assail you.

Sorry, but how the hell do teachers with "no programming experience" end up teaching a programming class? WTF school do you GO to?

Oh, I think I found it.

Sorry, but if you're being taught by teachers with "no programming experience", you're being ripped off and should complain to the administration.

Actually, looking at the syllabus, this looks to be a class to teach you the MATH and PHYSICS behind game programming, rather than a true CS/programming course. So maybe you were expected to know C++ WELL before the class started.

Quote

Prerequisites:
MATH 171 (College Algebra) or MATH 173 (Precalculus) with grade of ‘C’ or higher or appropriate score on the math assessment test. CS 200 (Programming Algorithms Using C++) or equivalent programming experience.


I don't know. Just weird to be getting programming assignments from someone who couldn't really tell you if you're doing it right!

This post has been edited by JackOfAllTrades: 06 February 2012 - 04:37 AM

Was This Post Helpful? 0
  • +
  • -

#14 starkhavenprincessrin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Vector class(using real vectors) don't understand

Posted 06 February 2012 - 06:50 AM

View PostJackOfAllTrades, on 06 February 2012 - 04:24 AM, said:

Quote

As I said my teachers have been no help at all, mostly due to the fact that they are teaching a programming class but neither of them have any programming experience.


NOTE: This post is not meant to assail you.

Sorry, but how the hell do teachers with "no programming experience" end up teaching a programming class? WTF school do you GO to?

Oh, I think I found it.

Sorry, but if you're being taught by teachers with "no programming experience", you're being ripped off and should complain to the administration.

Actually, looking at the syllabus, this looks to be a class to teach you the MATH and PHYSICS behind game programming, rather than a true CS/programming course. So maybe you were expected to know C++ WELL before the class started.

Quote

Prerequisites:
MATH 171 (College Algebra) or MATH 173 (Precalculus) with grade of C or higher or appropriate score on the math assessment test. CS 200 (Programming Algorithms Using C++) or equivalent programming experience.


I don't know. Just weird to be getting programming assignments from someone who couldn't really tell you if you're doing it right!



I've contacted the president of the college I'm at and the head of the gaming department about all the trouble that not on me but the rest of the class has been having with the teachers and the class in general. My dad suggested this to me on Friday and I did just that later that night once we got home.

Nice to know that other people say that I should do that too, thought my dad and I were the only ones.
Was This Post Helpful? 0
  • +
  • -

#15 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Vector class(using real vectors) don't understand

Posted 06 February 2012 - 03:50 PM

Just to get the other point of the argument across.

First of all I'm assuming this is some sort of computer graphics course. Judging from the assignment they are trying you teach you this by gradually creating a primitive 3D engine, this would definitely be a first step towards that. If I were to teach a computer graphics course an assignment along these lines is what I would start with.

I can't really judge on how they are teaching this but since you mention this is a lab assignment I'm assuming there was a lab class to get you started and where you should have asked questions.

Anyway if this is correct:

Quote

Prerequisites:
MATH 171 (College Algebra) or MATH 173 (Precalculus) with grade of ‘C’ or higher or appropriate score on the math assessment test. CS 200 (Programming Algorithms Using C++) or equivalent programming experience.


Then an assignment along these lines should be within your grasp. I'm assuming you did learn about vectors and some trigonometry (sine/cosine/tangens/cotangens) and preferably learned something about inverse trigonometric functions (arc cos/arc sin/arc tan) and polar coordinates. This is just an application of that. And the programming prerequisites should enable you to actually program it.

To get you started. Basically they ask you to create a class to create a vector and define some operations on it. CTphpnwb has provided a good base. Here is a somewhat more elaborate explanation of the mathematical parts, as I'm quite certain that is the part you are mostly struggling with:

Polar coordinates are another way of describing a 2D vector or point. Instead of using the coordinates in a fixed base, you use
the (distance to a fixed point/length of the vector) and the angle with one fixed vector, the x-axis.

To go from polar to Cartesian is really easy:

x = r * cos(theta)
y = r * sin(theta)

The other way around is slightly more involved. First r, is just the magnitude/length of the vector, so:

r = sqrt(x^2 + y^2)

The theta is the angle between x axis and the vector (x,y). Look at it on a geometric circle like this: http://upload.wikime...tesian.svg.png.

You might remember the tangens of an angle is equal to opposite size/adjacent sides, the opposite side you know, it's y, the adjacent side is x, so:

tan(theta) = y/x

Now the tangens has sort of an inverse function, the arc tangens, so:

arc_tan(tan(theta)) = arc_tan(y/x)
theta = arc_tan(y/x)

Now the mathematicians here will shoot me for writing this because it is not entirely correct. The arc tangens is not exactly the inverse of the tangens, only for angles -Pi/2 <= theta <= -Pi/2. But without getting to deep into the mathematics, the C/C++ standard library (math/cmath header) provide a function that compensates for this, so when you write your code, use:

theta = atan2(y,x)

It also solves the division by 0 error when x is 0.

Next up the magnitude, heading and pitch. Well I've never heard this term before but I'm assuming they mean cylindrical coordinates http://en.wikipedia....rdinate_system. Where the heading would be the angle theta, the magnitude is obviously the magnitude of the vector ( r ) and the pitch is the height ( z ). Basically the transformations are the same as for polar coordinates

Cylindrical -> carthesian

x = r * cos(theta)
y = r * sin(theta)
z = z

z on the right side being what they call pitch. And

Carthesian -> Cylindrical

r = sqrt(x^2 + y^2 + z^2)
theta = atan2(y,x)
z = z

Now with those 3 angles, they mean the angles of your vector with the 3 primary axis x,y,z or (1,0,0), (0,1,0), (0,0,1). I would go through the dot product to get those.

Basically, suppose you have 2 vectors a,b, then their dot product is

a.b = a_x*b_x + a_y*b_y + a_z*b_z

This is easy to calculate, but it is also equal to:

a.b = |a||b|cos(angle between a and b)

|a| is just the length of the vector a, so sqrt(a_x^2+b_y^2+b_z^2), by now you should have a function for that. This second definition is useful to get the angle, let's call it theta.

cos(theta) = a.b / |a||b|

We can again use one of the inverse trigonometric functions to get the angle, arc cosine this time:

arc_cos(cos(theta)) = arc_cos(a.b / |a||b|)
theta = arc_cos(a.b / |a||b|)

But we don't want the angle between 2 random vectors, we want the angle between the current vector (v = (x,y,z)) and the unit vectors (1,0,0), (0,1,0), (0,0,1), so:

alpha = arc_cos(((1,0,0) . (x,y,z)) / (|v|*1))
alpha = arc_cos(x / |v|)

And similarly:

beta = arc_cos(y / |v|)
gamma = arc_cos(z / |v|)

Arc cosine is the acos function in the C math.h/cmath header.

Note that I was just writing this as I go long, some mistakes could have sneaked in there. It is important to understand the derivations though if you want to actually learn anything, so I'll leave the verifying up to you.

You might be able to call them on one thing. When dealing with homogeneous coordinates, the 4th coordinate should be 1 for a point and 0 for a vector. But you'll be using this class mainly to represent points, so maybe they didn't find it necessary to make the distinction.

This post has been edited by Karel-Lodewijk: 08 February 2012 - 07:54 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1