Programming Assignment # 2
Due: October 14.
In this assignment you will use a two dimensional array to develop a line drawing program.
The line drawing problem is the following: you want to draw a line to go from a point (0,0) to a point (p,q). However, lines can have arbitrary slopes, whereas you can only address the screen with integer coordinates. (This is true whether you are writing to a text-based output screen or a Graphics window in pixel coordinates). The goal is to choose the best sequence of integer coordinates that most closely follows the line.
Using the algorithm discussed in class, write a program that assigns a two dimensional array of characters with either blanks or marks, indicating the path your line drawing will take. To make this more concrete, suppose your program takes as input a row and column (integer), creates a two dimensional array of that size, and then draws the line from (0,0) to (row, col). (Note: your two dimensional array will be of size row+1 and col+1 in this case). To debug your program, you should first test it program by outputting the array to the screen, using System.out.print commands, on small size arrays. When you are sure it works, you can use the same two dimensional array, but have your line drawing program output to an applet window. An easy way to do this is to have the applet paint routine test each element in the two dimensional array as follows:
if (a[y][x] == MARKED) MyPixel.drawPixel(g,x,y);
The drawPixel method is found in MyPixel.class, which you can download here. It takes as arguments the same Graphics window g which is an argument to the paint method, and the integer coordinates i,j at which you wish to the draw the pixel.
To use the MyPixel class, download it to the same directory where your java program resides. When you compile your java applet, it will automatically include the MyPixel class.