Vector class(using real vectors) don't understand

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:

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.

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.

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:

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

Posted 05 February 2012 - 04:16 PM

CTphpnwb, 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:

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.

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?

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

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.

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

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.

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.

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.

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:

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:

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