The purpose of this lab is to get practice with defining and using your own functions. The essence of good structured programming is decomposing a large problem into a number of smaller sub-problems (and those sub-problems into even smaller sub-sub-problems, and so on and so on, as far as you need to go). Each of the sub-problems is solved by functions which the programmer must design, implement, and tie together into a single program.
Correctly identifying sub-problems is an important task. Whenever you think you have identified a sub-problem that can be solved with a function that will contribute to the solution of the whole problem, make sure that you can describe that sub-problem in a short simple sentence or two. If a task hasn't got a nice simple description, then maybe it isn't a very good task: maybe it is just part of a larger task, or maybe it needs to be split up into yet smaller tasks.
Having a clear description of the exact requirements of a sub-problem is equivalent to having a clear idea of what the function you're about to write must do. If the description of what a function must do is complicated or long, then the function itself will turn out to be complicated and long, and therefore unreliable.
In a previous lab exercise, you wrote a program that draws a pentagram, which is almost a five-pointed star, but has a pentagon inscribed inside it. Adapt that program so that it draws a clean five-pointed star with no extra lines, just like you would expect to see on an American flag.
Convert the star-drawing part of your program into a function which you could use many times to draw as many five-pointed stars as you like. The function should be position independent, which means that you could use it to draw a star anywhere in the graphics window, not just in one fixed place. The function should also have a parameter that defines the size of the star (by providing the length of the ten lines that make up the outline of the star). To save you from too much geometry, the angles are shown on the last page of this lab. Use your function to draw a series of stars of different sizes and in various places.
The graphics library contains a collection of functions that can record the outline of a shape as you are drawing it, and then fill it in with a solid colour. There are three important functions to use: StartShape() tells the system to start recording a new shape, AddHereToShape() tells the system to record the current pen position as one of the vertices (corners) of the shape being recorded, and FillShape() tells the system to fill in the outline just recorded with solid colour. You don't need to actually draw the outline: you can just move the pen around on the window, tracing out the outline of the shape, while recording the position of each corner. Here is an example; the following piece of program would just draw an irregular red quadrilateral:
Simply adding in calls to the new functions produces a solidly coloured version of the same shape:
Use this new method to modify your star-drawing function so that it draws solid stars, not just outlines.
Incorporate your function into a program that draws the flag of Texas as nicely as possible.
There is another function that makes the drawing of solid rectangles easier:
Using the solid-star-drawing function you have already written (but you can forget about the Texan flag), write one function that draws an evenly spaced row of six stars, and another function that draws an evenly spaced row of five stars. Make them solid white stars on a blue background.
Write a program that draws an accurate rendering of an American Flag. The program must make good use of functions so that it remains readable and understandable even though it is performing quite a complex task.
This picture shows the correct configuration of the stars and stripes on the flag. You don't have to copy it slavishly, but try to make your program produce good-looking accurate results. The official proportions of the flag (length to height) are 19 to 10.
The Geometry of a five-pointed star.