CZ2104, Lab 3, due Wednesday, 20 Sep 2000 This set of exercises is for 2D interactive graphics. Part 1 and 2 are compulsory (100 marks). Part 3 is optional and can be very difficult. If you do Part 3 correctly, you earn extra 30 marks of credits. [Best viewed by IE5 or Word] |
|
Questions: |
Comments, hints, and what to hand in: |
Part 1 |
|
This
is exactly tutorial 2, problem 2.4, implemented on computer. Turtle graphics is an alternative
positioning system that is based on the concept of a turtle moving around the
screen with a pen attached to the bottom of his shell. The turtle’s position can be described by
a triplet (x, y, q), giving the location of
the center and the orientation of the turtle. A typical API for such a system includes functions such as: init(x,y,theta); forward(distance); right(angle); left(angle); pen(up_down); Implement
a turtle-graphics library using OpenGL.
Test your functions to make sure they work as intended. |
1.:
hand in the code. Your code should be carefully commented. Also keep your soft copy in your directory
for TA to inspect. Same for Part 2,
and Part 3. |
Part 2 A
turtle walks continuously inside a square window, leaving a trail behind. Mouse is used to control the move of the
turtle. Development an interactive turtle graphics, as follows: (a)
Use the key ‘s’ to start and ‘q’ to stop the drawing of the
turtle. The starting point is at
mouse location (x,y), the starting turtle orientation is set at random
between 0 and 360 degree. (b)
Press left button will increase the angle by d;
press right button will decrease the angle by d. If the angle is positive the turtle turns
to the left; if it is negative the turtle turns to the right. [the left and right button play the role
of a steer-wheel of a car]. The angle
d is initialized to 0. (c)
Otherwise, the turtle first walks straight at step size d, and then
decides the new angle for the next step. You
experiment to find a good value for d and d. If the program runs too fast, you might
want to slow it down (e.g., calling a wait function or do some useless
computation in the idle()). |
2.: Programming the key() function: ‘s’ starts and ‘q’ stops the walking. In mouse() function, the angle is increased
(for left button) or decreased (for right botton) by d. Idle()
function walks forward by d (i.e. call forward(d)), then turn to right or
left by the current angle [i.e. call left(angle) or right(angle)]. |
Part 3 (Optional)Add more features to the program in Part 2. (a) Use the ‘Esc’ (escape key) to quit the program. (b) Use a menu to choose color of the line (turtle’s trace), width of the line, step size d of the turtle. This menu can contain submenu. The middle button brings up the pop up menu (also pause the walk). (c) Handle the case when turtle is outside the window. Let the turtle reflect back, rather than walk out of the window. |
3.:
Don’t try unless you have time. |