## CS115 Lab: C++ Overloading - Plotting Program Exercise

#### Explanation

For this part you will use overloaded member functions and make overloaded operators to get a simple plotting program to work. You will be working with three ADT's in this lab: Point2D, Vector2D, and Plot.

The plotting program uses the terminal text plotting package curses wrapped up in the Plot ADT. The Plot ADT uses the Point2D and Vector2D ADTs to simplify the job of moving the cursor and drawing lines. The following is an example of the interface for the program.:

```  0         1         2         3         4         5         6         7
012345678901234567890123456789012345678901234567890123456789012345678901234
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
What do you wish to do? (d: draw line, m: move point, q: quit) _

```

To use the program follow the prompts at the bottom. There are three commands: d, m and q. Use q to quit. d draws a line. You supply x and y displacements which are added to the current cursor to make the other end point of the line and all the characters on that line segment are filled in with a #. m moves the cursor by a displacement vector you supply but nothing is drawn. When the program starts the cursor is at (0,0)

The following sequence of commands:

```d
10
10
m
5
0
d
10
-10

```
will produce this result:
```  0         1         2         3         4         5         6         7
012345678901234567890123456789012345678901234567890123456789012345678901234
0#                        #
1 #                      #
2  #                    #
3   #                  #
4    #                #
5     #              #
6      #            #
7       #          #
8        #        #
9         #      #
10          #    #
11
12
13
14
15
16
17
18
19
What do you wish to do? (d: draw line, m: move point, q: quit) _
Cursor: 25.00 0.00
```

To complete the exercise you need to implement some overloaded operators that take points and vectors as arguments. The missing operators are vector division `vector/scalar` and scalar vector comparison `scalar<vector`. You will also need to use point vector addition `point + vector`.

Vector division consists of dividing the components of a vector by some value to make a new vector from the resulting component values. For example dividing the vector (4,2) by 2 produces the vector (4/2,2/2) = (2,1). Vector division is used in Plot to create a vector the right size to incrementally draw lines — one of unit length in the dominant axis.

Vector comparison compares the magnitude or length of a vector to a Euclidean distance. The Euclidean length of a two dimensional vector is defined as . The result of a comparison should be a boolean variable. The specific comparison you will implement is less than, <. It should return true if the left hand side length is smaller than the right hand side and false otherwise. Vector comparison is used in Plot to see whether a point that is being incremented to draw a line is close enough to the endpoint to stop being drawn.

To move a point to new coordinates you need to add a vector to it. Point2D supports a point vector addition operator. You will need to use the provided operator correctly where indicated. This operation is used in Plot to move points and to draw lines.

#### Instructions

• Transfer overload.zip from the CS Dept's anonymous ftp site to your Hercules account space.
• Do that by:
1. Entering the commands:
```cp /net/data/ftp/pub/class/170/ftp/cpp/overload.zip overload.zip

```
2. OR, by using the command line FTP program usin this information:
The path: pub/class/170/ftp/cpp
The file: overload.zip   Extraction command: unzip overload.zip

• Complete the listed changes by following the instructions in the comments that begin at the file and line numbers indicated:
• add a member `operator/` - Vector2d.h:30 and Vector2d.cpp:34
• add a non-member `operator<` - Vector2d.h:12 and Vector2d.cpp:15
• note the use of overloaded operators `< /` and `-` in plot.cpp:36,45
• use `Point2d::operator+(Vector2d)` - plot.cpp:20
• Remember that whether an argument is LHS or RHS is important!

• Find all the lines you need to change BEFORE making the changes. In emacs use the
`M-x goto-line`
command to get to a line. If you use a more advanced editor I trust you already know how to find a specific line.

• Compile and test your changes by typing:
```make
./pplot < VectorMessage
```
If you did everything right you should see some familiar text mode graphics. :) You can run pplot by itself to try things out. Just follow the prompts at the bottom.

• Now make some more changes this time to pointPlot.cpp. Find the calls to plotter.moveCursor and plotter.jumpCursor. These have been overloaded. Find the other prototypes for them in Plot.h and change each call to the other type.
• Compile and run again. There should be no changes to the output.