# Graphics Functions

We are going to have some fun with the following graphics functions:
• circle(x,y,radius)
• rectangle(left, top, right, bottom)
• moveto(x, y)
• lineto(x, y)
• getmaxx()
• getmaxy()

## Program 1

1. Declare integer variables midx, midy and size;
2. 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.
3. Since screens are generally smaller in the y direction lets make 'size' into half the value returned.
4. Then make it smaller again by multiplying size by 0.95.
5. Now draw a circle centred on midx and midy of with radius "size".
6. Now draw a box (rectangle) around the circle so that left value will be centrex-size and so on.
7. Go to the start of the main function and decare an integer variable called "count".
8. 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.
9. Somewhere in the loop, after the circle and rectangle calls, divide size by 1.4142.
10. Run and Show me. I'm going to ask you how it works!

## Program 2

Start by entering a program with just:
moveto(44,77);
lineto(111,33);
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.

## Program 3

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.
1. Add some features to the function that maintain this centring on x,y and obey the sizing rule.
2. Change the main function so that "DrawShape" is called in a loop in such a way that the positin and size changes.

## Program 4

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:
1. The function is sent the coordinates of the corners of a triangle and a 'level'.
2. The it draws the triangle with a moveto and three linetos.
3. If the level is below 1 thats all, it 'returns'.
4. Otherwise it calls itself 3 times except that:
• It sends the coordinates for a 1/4 sized triangle in one of the previous triangles corners.
• The level is reduced by 1.
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.

# Project

I want the following:
1. 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!
2. System Information: How the program works: How do the loops work? How are you using an array? One paragraph only.
3. I want the program e-mailed so that I can see it running
4. 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).
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).

## More Suggestions For Projects

1. 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.
2. Experiment with some of the pattern drawing ideas that we have looked at today.
3. Using an array, draw a bar or a line graph from input data. Label axes.
Text file handout
Home Page of RDScience