Homework 1

Full points : 100
 

Refer to the documenting and submitting a homework section.

Write a program that interactively draws polygons.

Every time a user clicks the left mouse button, the program adds a vertex until the right button is pressed. All accumulated vertices will eventually make a polygon. Note that the intermediate process should be visible by displaying vertices with a big point and drawing lines between vertices. The very last vertex and current cursor position should be connected with a rubber band line to display a possible line segment.  After right button is pressed, the polygon should be displayed filled with a solid color of your choice.

Here's the working binary files in windows 64bit format. You have to have glut library installed to used this program.

Hint 1:

At first, just write a program that display and save points as a user clicks the left mouse button.

After that, write a routine in the display function to display a polygon using all the saved points. Note that the right mouse button down will initiate this. So you should have conditions in the mouse callback function.

Now think about the rubber band line. The rubber band line should be displayed continuously as a user moves the mouse while pressing left button. So mouse event should be checked and saved. Moving mouse will initiate the motion callback and every time you enter the motion callback function, you need to save current mouse position so that it can be used in the display function for drawing a line between the last point and the saved current mouse position. After saving the current mouse position in the motion callback function, you need to invoke redisplay (glutPostRedisplay) function to force an additional screen update. Then the display function will be called, and the display function will redraw the line, resulting in continuous line redrawing.

 

Extra Credit 1: 20% extra

This sample program doesn't work when the window size is redefined. Make your program to reflect the updated new window size and maintain the pre-drawn polygon, and make sure to work correctly within new window size. Note that you don't need to maintain the aspect ratio.

 

Extra Credit 2: 20% extra

This sample program doesn't generate correct polygonal structure when your ordering of vertices are not selected in convex way. When you generate the final color-filled structure, make sure to correctly generate the non-convex structure.