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:
-
README: A file giving a overview of the contents of the directory
including:
-
Your name and section number.
-
A short (2-3 sentence description) of the assignment.
-
A one sentence description of each file in the directory.
-
An explanation of how to compile your program.
-
Names of any individuals with whom you discussed program development.
-
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.
-
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.
-
3Dtest.cxx: A program that generates points on a three-dimensional
cylinder or cone as described below.
-
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 default constructor.
-
A function to shift the location of a point.
-
Functions to rotate the point angle radians about the x axis:
x' = x
y' = y*cos(angle) - z*sin(angle)
z' = y*sin(angle) + z*cos(angle)
-
Functions to rotate the point angle radians about the y axis:
x' = x*cos(angle) + z*sin(angle)
y' = y
z' = -x*sin(angle) + z*cos(angle)
-
Functions to rotate the point angle radians about the z axis:
x' = x*cos(angle) - y*sin(angle)
y' = x*sin(angle) + y*cos(angle)
z' = z
-
Functions to get and set the values of the x, y, and z coordinates of the
point.
-
A nonmember function called distance, that computes the distance
between two points.
-
An operator that returns the sum of two points:
(x1,y1,z1)
+ (x2,y2,z2) = (x1+x2,y1+y2,z1+z2)
-
An operator that returns the difference of two points:
(x1,y1,z1)
- (x2,y2,z2) = (x1-x2,y1-y2,z1-z2)
-
A function that returns the cross product of two points (one kind of point
multiplication):
cross((x1,y1,z1),(x2,y2,z2))
= (y1*z2-z1*y2, z1*x2-x1*z2,
x1*y2-y1*x2)
-
A function that returns the dot product of two points (another kind of
point multiplication.) Note that the result is a single number, not a point
a :
dot((x1,y1,z1),(x2,y2,z2))
= x1*x2 + y1*y2 + z1*z2
-
An operator that returns the product of a double and a point:
val * (x1,y1,z1)
= (val*x1, val*y1, val*z1)
-
An comparison function equal that tests to see whether two Point3Ds are
"equal" to within some tolerance. You should return true if the distance
between the points is less than tolerance.
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:
-
whether to generate a cylinder or a cone.
-
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/.
-
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.
%cylinderprog > junk
-
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