22C:030/115 Computer Science III

Fall 2001

Program 2: A C++  Point3D Class

DUE DATE: 9/21/01 10:30 PM


This program requires you to implement and test a class called Point3D. The assignment will give you experience in implementing a class from a specificiation, in software coding and organization in C++, and in software testing. The assignment is based on problem 5 on page 86 of the text book, "Data Structures and other Objects Using C++" by M. Main and W. Savitch.



 
 
What to submit:
You should submit a directory named program2 (use location 001 if you
are in section 1, 002 if you are in section 2, etc.) that includes:
  1. README: A file giving a overview of the contents of the directory including:
  2. Point3D.h: The header file for the new Point3D class. Most of this file is supplied for you. Start with Point3D.h. Add your name to the top of the file. If some of your member functions are implemented as inline functions, then you should add those implementations to this file.
  3. Point3D.cxx: The implementation file for the new Point3D class. You must write all of this file, which implements the Point3D member functions. However, the implementation for the Point class, available in the software distributed with the text, provides a good starting point.
  4. 3Dtest.cxx: A program that generates points on a three-dimensional cylinder or cone as described below.
  5. A printed copy of the files README, Point3D.h, Point3D.cxx and, 3Dtest.cxx in lecture.

The Point3D Class

You are to implement a new class called Point3D based on the specifications in the file Point3D.h. The Point3D class is designed to represent points in three-dimensional space. Each Point3D has members giving the x,y, and z coordinates of the point. The specification of the Point3D class gives a precondition/postcondition contract for all the Point3D's member functions, including:

A Program Demonstrating the Point3D Class

To demonstrate the Point3D class you are to write a program that generates points on the surface of simple shapes.  Consider how we can use the Point3D member functions to produce a series of points on a cylinder.  We begin with a single point on the x-axis a distance r from the origin:
 
        Point3D p1(r, 0.0, 0.0);
We can generate a series of points on a circle in the x,y plane by incrementally rotating p1 about the z-axis:
 
        for (i=1; i<100; i++)
            p1.rotateZ((2.0*PI)/100.0);
where PI is a constant approximating the number pi.

We can generate points on a cylinder by progressively shifting our circle up the z-axis:
 

        for (j=1; j<10; j++){
            p1.shift(0.0, 0.0, 1.0);
            for (i=1; i<100; i++)
                 p1.rotateZ((2.0*PI)/100.0);
           }
Your program should print p1 at its initial position and after each shift and rotation.

In addition to generating a cylinder, your program should be able to generate points on a cone. The program should ask the user:

  1. whether to generate a cylinder or a cone.
  2. what radius should be used (for the cone this will be the radius at the base).
For an additional challenge, you can try generating other shapes such as a box or a sphere.

Viewing Your Output

To view the points generated by your program, you can store your points in a file, convert this file to VRML format, and then use a 3-D viewing program called VRMLview available in the class directory. The viewing program runs on Linux machines. You can also download a versions of the program that run on Linux, Win95, and WinNT at http://www.sim.no/vrmlview.html. Viewers for other platforms can be found at http://www.web3d.org/.
  1. First create a file containing the points generated by your program. Each point should be on a separate line. An example file called cylinder is available for you in the class programs directory. One simple way to create the file of points is to use Unix I/O redirection. The output a programs sends to the console window can be directed to a file (for example, junk) by appending the suffix "> junk". Thus, the following command will create a file named junk containing the output your program writes to the standard output stream.
  2.         %cylinderprog > junk
    
  3. Once the file of points is created you can use the a program called p3dtovrml that is available in the class programs directory. This program translates your file into VRML format. You can then view your program with a VRML viewer. Try the program with the cylinder data set by executing the following commands from your home directory. The first command will create a VRML file called cylinder.wrl in your home directory. The second command will display this on a Linux machine.
        %/group/class/c030/programs/p3dtovrml < /group/class/c030/cylinder > cylinder.wrl
    
        %/group/class/c030/programs/vrmlview < cylinder.wrl