teaching machines

CS 455 Lecture 2 – Triangles

January 29, 2015 by . Filed under cs455, lectures, spring 2015.

Agenda

TODO

Before next class:

Before next Thursday:

Lab

Pair up with a neighbor to write a Vector3 class with the following specification:

  1. Uses floats to represent the vector’s three components.
  2. A default constructor that zeroes the vector’s three components.
  3. A constructor that accepts the initial values of the three components.
  4. A subscript method ( operator[]) that accepts an index of 0, 1, or 2. When the index is 0, it returns the x-component. 1, the y-component. 2, the z-component. So that this message may serve as both a getter ( cout << v[0]) and a setter ( v[0] = 1.2f) return the float as a reference to a member variable.
  5. const subscript method.
  6. A method that gets the vector’s length or magnitude.
  7. const method that determines the dot product of this vector and a parameter vector (which comes in as a const reference).
  8. A method that normalizes the vector.
  9. + method that adds component-wise this vector to a parameter vector and returns the sum as a new vector.
  10. += method similar to the previous method but which alters this vector instead of yielding a new one.
  11. * method that scales component-wise this vector by a float scale factor parameter and returns the scaled vector as a new vector.
  12. *= method similar to the previous method but which alters this vector instead of yielding a new one.

Declare the class in Vector3.h. Define it in Vector3.cpp. Create these files through Windows Explorer in the top-level source directory.

Many students prefer editing in Notepad++. You may make your edits with it, and when you go to run your projects, jump to Visual Studio. Messy, huh? I prefer the workflow of Linux or Mac.

Also write a << method for printing vectors. Note that this can’t be a member of Vector3, since the invoking object will be an ostream, not a Vector3.

Main

Now let’s create a second project in our Git repository for testing your Vector3 class. Create a directory named vectest and create file vectest.cpp with a main function that creates some vectors, scales them, adds them, and prints them. Invoke every method you wrote and make sure it does the correct thing.

Copy over first/CMakeLists.txt to your vectest directory. Change all the first references to vectest. Append  ../Vector3.cpp to the add_executable command. Also, in order to make CMake aware of this new project, add this line to the overall CMakeLists.txt in your top-level source directory:

add_subdirectory(vectest)

Rebuild in Visual Studio.

Add your new files to your Git project using the GUI client and push them to Bitbucket.

Haiku

when gifts were distributed:
Be great at one thing
Or be good at lots of things
Machines picked the first