You are on page 1of 3

COMP1405 - Assignment #4

(Due: Monday, November 9th at 9:00AM)


(1) Painter's Algorithm
In computer graphics, when displaying 3D triangulated models at various angles,
it is necessary to sort the triangles by distance from the viewing point within the environment.
Otherwise, the triangles will not be displayed properly. For example, below are two images ... one in
which triangles are displayed in the order that they appear in the model and the other displays triangles
in order sorted from their distance to the viewing location:

Spider(unsorted)

Spider(sorted)

Notice how the image on the left does not have properly displayed triangles. For example, the spider's
abdomen is not displayed properly and the legs appear to pierce one another. By sorting the triangles,
the problem goes away. A couple of examples appear on the next page as well.
Download the DisplayPanel.java and ShapeDisplayProgram.java files, as well as the various 3D
model files supplied on the course website (where you found this assignment). You can run the code.
It will ask you for a filename. Enter the name of the file that you want to display (without the .off
extension). For example, Spider, Man, Woman, Hand etc... The code should display the model
without the faces sorted properly. Your job is to write the sortByXCoordinates() method so that the
facesX and facesY arrays are sorted with the furthest triangle first in the arrays. YOU MUST USE THE
INSERTION SORT algorithm mentioned in class. You must write the code on your own to do this,
although the pseudocode is given to you in the notes.
These two arrays represent the coordinates of the faces to be displayed in 2D. The
computeProjection() method converts the 3D coordinates into 2D by just using the Y and Z
coordinates. facesX[i] is an array that represents the X coordinates of the three vertices that belong to
face i while facesY[i] represents the Y coordinates. So, for example, the first face/triangle to be
displayed has three vertices with coordinates as follows:
v1 = (facesX[0][0], facesY[0][0])
v2 = (facesX[0][1], facesY[0][1])
v3 = (facesX[0][2], facesY[0][2])
The faces are all created already. You just need to sort them. You will want to use another array to
store the distance of each face ... use a float array. Then you should set the distance for each face to

be the maximum of the three X coordinates of the vertices of that face (because we will sort by X
coordinates in our program). You can do this within the computeProjection() method.
Complete sortByXCoordinates() method so that it sorts the faces by these distances (using the
InsertionSort algoriithm). Once it is working, you should see images that resemble the images on the
right.

Man (unsorted)

Hand(unsorted)

Man(sorted)

Hand(sorted)

Woman(unsorted) Woman(sorted)

(2) Sort Comparison


Download the NameSearchProgram as well as the surnames.txt file. The program compares the
speeds of sorting the names in the file by using the BubbleSort algorithm as well as the BucketSort
algorithm. The program will produce two files called surnamesBubbleSorted.txt and
surnamesBucketSorted.txt which contain the names sorted by increasing order of the number of letters
in the names.
Currently however, the program does no sorting. You need to complete the bubbleSortByLength() and
bucketSortByLength() methods so that they properly sort the names in increasing order of their length.
For your information, the list of surnames contains names with at most 13 letters.
Once properly completed, you should see output that looks roughly like this:
Bubble Sorting ...
Bubble Sort Took: 39.375 seconds
Writing to file ...
Done.
Bucket Sorting ...
Bucket Sort Took: 0.046 seconds
Writing to file ...
Done.

Note that the bucket sort will take insanely less time that the bubble sort (i.e., about 0.1 percent of the
total time of the bubble sort in my case). If this is not the case, then you are likely not doing the bucket
sort properly/efficiently. You will be marked for doing a proper bucket sort. You should look at the files
produced. The names should be grouped properly according to their length in both cases.
Once you complete this, you should understand the huge advantage of the bucket sort.
NOTE: Submit a single zip file of a folder that contains all of your .java files AS WELL AS THE CODE THAT WAS GIVEN TO
YOU AND ALL THE TESTING FILES. Just ensure that you submit ALL the code and files necessary to test everything ... do
not leave out anything. Submit your assignment using CULearn. Note that if your internet connection at home is down or
does not work, we will not accept this as a reason for handing in an assignment late ... so make sure to submit the assignment
WELL BEFORE it is due !

Please NOTE that you WILL lose marks on this assignment if any of your files are missing. You
will also lose marks if your pseudocode is not written neatly with proper indentation. See
examples in the notes for proper style.

You might also like