We are going to have some fun with the following graphics functions:
- rectangle(left, top, right, bottom)
- moveto(x, y)
- lineto(x, y)
- Declare integer variables midx, midy and size;
- Using the getmaxx and getmaxy functions put the coords of the middle of the screen in midx and midy. Hint:
start by putting the maximum values e.g. midx=getmaxx(); and then halving the values.
- Since screens are generally smaller in the y direction lets make 'size' into half the value returned.
- Then make it smaller again by multiplying size by 0.95.
- Now draw a circle centred on midx and midy of with radius "size".
- Now draw a box (rectangle) around the circle so that left value will be centrex-size and so on.
- Go to the start of the main function and decare an integer variable called "count".
- Now put the circle and rectangle function into a while loop so that they will be drawn 20 times. Use count (set count
to zero before the loop, test for count<20 in the while statement and have a count incremented in the loop.
- Somewhere in the loop, after the circle and rectangle calls, divide size by 1.4142.
- Run and Show me. I'm going to ask you how it works!
Start by entering a program with just:
Add more moveto and lineto statements until you understand how they work (including the coordinate system). The get pprogram 2
from the handout (cut and paste). Play with the number values in the program. when you can explain it call me.
The template of this is on the handout. The graphics is all in a function. The function takes the parameters x,y,and size.
Look at the way that the function is constructed. All the graphics calls are centred on the x,y parameters and its sized using
size. I want you to do two things to the program.
- Add some features to the function that maintain this centring on x,y and obey the sizing rule.
- Change the main function so that "DrawShape" is called in a loop in such a way that the positin and size changes.
Another cut and paste one I'm afraid. This is very cool. Look how short it is. This program uses a technique called "recursion".
The function calls itself! Let me explain how it works:
- The function is sent the coordinates of the corners of a triangle and a 'level'.
- The it draws the triangle with a moveto and three linetos.
- If the level is below 1 thats all, it 'returns'.
- Otherwise it calls itself 3 times except that:
The net result is that we draw increasingly small triangles with level values of 6,5,4,3,2,1 and 0. The level is the equivalent of the
counting variable in a while loop. This is is called Sierpinski's triangle -after the guy who 'lost' his luggage. Note that
when we call a function recursively it is as though we are making a new copy of the function.
- It sends the coordinates for a 1/4 sized triangle in one of the previous triangles corners.
- The level is reduced by 1.
I want the following:
A more complicated program will get a higher mark but don't neglect the basics. The minimum expected complexity might be a loop (perhaps controlled by user intup
and the question "do you want to do another calculation?". Input and Output and calculation in the loop, and some sort of if statement in the loop. Higher marks for
use of arrays, functions, graphics, nested loops/if statements, evidence of research (don't go bonkers).
- Domain Information: How the program works from the point of view of the user. If the program is about making pizzas,
then its written for a pizza maker. 1 paragraph only!
- System Information: How the program works: How do the loops work? How are you using an array? One paragraph only.
- I want the program e-mailed so that I can see it running
- The program should have good style. This includes:
- Comments saying who wrote the program when and what it does.
- Comments making a "variable dictionary -explain what each variable does.
- Comments on each closing brace saying what it closes.
- Indentation within structures.
- Meaningful variable names where possible
- Blank lines to make readable.
- A good 'user interface, so the user knows what they have to do and what the output means (if necessary).
More Suggestions For Projects
Text file handout
- In graphics mode plot the graph of a function. Draw axes with ticks and number labels. Have a loop to evaluate a (polynomial) and plot the
it as many short lines.
- Experiment with some of the pattern drawing ideas that we have looked at today.
- Using an array, draw a bar or a line graph from input data. Label axes.
Home Page of RDScience