 Hello and welcome to another lecture of the course on computer programming. I am Dr. Abhiram Ranade also at the department of computer science and engineering at IIT Bombay. I am the guest lecturer for today. My topic is going to be elementary graphics. As they say a picture is worth a thousand words. What this means is that pictures, graphs, charts, diagrams are often easier to understand than text. This is especially true in science and technology where the information is often geometrical and animation can also be very useful. If you are talking about collisions or if you are talking about moving parts and obviously if you see them in motion that is very helpful. Graphical input for example, selecting an object on the screen or deciding the coordinates of a certain, the size of a certain object that is much more directly given by clicking on the screen rather than by specifying it numerically. What we are going to do in the next few sessions is study how all these things can be done. Here is a quick outline of this session. The graphics package that we are going to use is called Simple CPP and I will give you a quick introduction to this graphics package. Simple CPP supports two broad ways of doing graphics. One is turtle graphics and the other one is the so-called coordinate geometry based graphics, coordinate based graphics. In this session I will be talking about turtle graphics. Simple CPP is a graphics package developed for use with the book written by me called An Introduction to Programming through C++ which was published by McGraw Hill earlier this year. This package is available for Unix as well as for as well as Windows at www.cac.iitb.ac.n slash tilde runnade slash Simple CPP. It supports two kinds of graphics as I said turtle graphics and coordinate based graphics. Let me say something about turtle graphics. This is an idea and a package and a language invented in 1960s by Seymour Papert. Turtle graphics appeared first as a part of the logo programming language which he developed for teaching children how to program. As you can see it is extremely simple. The important object in turtle graphics is obviously a turtle and a turtle is a symbolic animal that lives on the screen. It simply appears as a small triangle but there are implementations of logo where an actual turtle appears but in our case you will see it as a small triangle. The turtle is to be commanded by the program. So the program that you write, the C++ program that you write will have facilities by which you can command the turtle and move it. The turtle has a pen which draws on the screen as the turtle moves. So that is the exciting part. The goal of turtle graphics you could say is to draw interesting pictures on the screen. Let me begin with a direct introduction. Here is a very simple program fragment which will draw something on the screen and I will have you guess what it is. The first command is turtle sin. This merely says start the turtle graphics functionality. This will cause a window to appear on the screen and inside that the turtle which is just a small triangle will appear at the center. Forward 100 as you might guess will cause the turtle to move 100 pixels forward. Right will cause to turn right. The argument supplied to right decides by how many degrees the turtle is to turn. So in this case we are asking the turtle to turn 90 degrees. So we do forward 100, then a right 90, another forward 100 and a right 90, another forward 100, a right 90 and a forward 100. Can you guess what this draws? The simplest way to guess is to pretend that you are the turtle and maybe walk forward or turn as you execute the commands. If you guess correctly and I am sure you will, you will observe that this is going to be a square. The full program that I am going to show you running is actually slightly different. So let me show that to you now. The first line here says include simple cpp. This just includes a header file called simple cpp. This causes the graphics functionality to be included. Then you have the main program, the first line of which is turtle sin. This creates the graphics window or what we call the graphics canvas with the turtle inside it. Now the rest of the program is really similar to what I wrote. So I have forward and right, but you will notice that I have interspersed that with the wait command. The wait command simply causes the program to wait for the specified number of seconds. If we do not put in wait statements, the execution will just happen too fast and we will not see anything. So often when you do graphics using turtle sin or in fact any graphics, since computers execute very fast and you actually want to look at what is going on and perhaps enjoy what is going on, then you might have to slow it down and the wait statement comes in quite handy for that. So let me now give you a demonstration of this. So as you can see, I have taken the program that I showed to you and I have loaded it into the code blocks ID along with the wait statements as I mentioned. So now I am going to compile and run it. As you can see, the turtle went forward and then turn and did this and indeed drew a square on the screen. Now I will talk about other functions besides the ones that you have seen, which can also be used to command the turtle. Well, you have already seen forward, but an important point over here is that D may be given negative. How much to move forward can be given negative in which case the turtle moves back. Right is also something that you saw. So the argument to write tells the turtle how much to turn, so in degrees. You have a similar command which tells the turtle to turn, but this time the turtle is going to turn left. There is another useful command which is called the pen up command. This causes the pen to be raised. After this pen up command is given, drawing is going to stop because the turtle is going to move, but the pen is not going to touch the screen and this will happen until the pen is lowered again. The pen down command lowers the pen. So as a result of this, the drawing will resume. Wait as we saw, waits for s seconds where s is the argument. So now I am going to take the ideas that I have discussed and do something slightly more interesting and complicated. I will show you how we can make the turtle draw a decorative plate border. I am not going to tell you exactly how this is done, but only show what the turtle does. There is an obvious repetitive pattern over here and I would like you to figure out how exactly you could write the program. The pattern has curvatures and it repeats a certain number of times. So I would like you to think about writing a program which will make it repeat a different number of times if need be. Although the program that I show you will make it repeat a certain fixed number of times. Furthermore, the lines and the curves that are drawn merge into each other smoothly and that is what makes the picture look nice and I hope you will spend the time to get it right in that manner as well. So as you can see, the turtle is drawing the border in a circular manner. You will see that it closes very nicely. I want to give you another demo. This is a demo in which we are going to draw a tree. The key observation in this is that a tree can be viewed as a trunk on top of which there are two trees, not growing straight in the direction of the trunk, but they are growing at an angle. So a tree is a trunk with two small trees on the top. So let me show you what I mean. So this is a tree. This is a trunk and then I will draw two small trees on top. So that makes a tree. That makes a bigger tree. This portion is a small tree, so is this portion and the whole thing is a big tree. So the whole contains some things which are similar, not exactly the same but similar as its parts and this is exactly where or this is sort of a very solid use of recursion and in fact that is what we are going to do. We are going to write a program to draw trees like this and we are going to use recursion for it. So here is the entire program. Let me first zoom in on to the main program. The main program is reasonably simple. We first call turtle sim. Then after turtle sim is called the turtle faces the right side and we want the tree to grow upwards and therefore we will first make the turtle turn left. So now the turtle is facing upwards and we do not want the tree to start from the center of the canvas but we want it to start a little bit below. So we will take the turtle downwards and for this we will use the forward command but we have given the argument minus 125. So the turtle will go down by 125 pixels but it will still keep facing upwards and now we will make the recursive call to draw a tree. The first argument to it tells how many times the tree is going to branch. So in this case it will branch six times. So it will branch first, then it will call another tree to be drawn but this time the new tree that is going to be called is only going to branch five times. So there will be six levels of branching overall. The trunk of the first tree that is drawn will have length 125 pixels and the third argument tells you what the angle between the two trunks is. Finally after the tree is drawn we are going to wait for five seconds so that we can see what the turtle has drawn. So now I will turn to the function which draws the tree. As I said it has three arguments H, L and A and then it satisfies an invariant. The invariant is as follows. This function then called will draw a tree in the direction in which the turtle is pointing. At the end the turtle will be left pointing in the same direction as at the beginning. So the invariant is really in two parts. There is a precondition that this function requires and then there is a post condition that this function is guaranteeing. The post condition is required so that after making one call you know where the turtle is pointing and therefore you can make other calls as needed. To begin with the turtle waits for 0.1 seconds. Again this wait is put in just so that the drawing does not happen very fast but we get to see the drawing as it happens. If H is 0 in which case it is saying that no more branching needs to happen. That means we have come to the end our tree is really nothing. I mean so we have finished drawing everything. So in that case we will just return. However if H is bigger than 0 then it means we have to draw a tree. So the first thing we will do is we will go forward by L. So this is drawing the trunk of the tree. Now we are going to turn to draw our two trunks, our two small trees. So for this we will turn left by A degrees. So A is the degrees is that angle made by the first subtree that we are drawing with the direction that the trunk was facing. The trunk of the main tree is facing and then we are just going to ask that a small tree be drawn in this new direction. So the small tree, the tree is small because its H parameter is one smaller and we have also made the length parameter slightly small by this constant SL which we have defined over here. Notice that this is like what happens in the real trees. The length of the branches shrinks as you go from the bottom towards the top. We have also made the angle slightly smaller. You can play around with these parameters but I have just arbitrarily selected 0.8 and 0.7 as the values of SL and SA. So this will cause a tree to be drawn at an angle to the main tree, to the outer tree. Then after this tree finishes, this small tree is drawn. Notice that the post condition will guarantee that a turtle is pointing in the same direction in which it was pointing when the tree drawing started. So now we want to draw the second tree. The second tree is at an angle A but on the opposite side. So while drawing this tree, the turtle is angled at A to the left. We want to go A to the right. So we will turn the right by twice A degrees and then we will make another tree at that point. So this draws the right side of the tree. After that the turtle is facing the direction A degrees to the right of the vertical line and we wanted to come back to facing upwards again and so we make it turn left. Then we go backwards and we go to the base, the root of the tree so to say. That is where we are supposed to leave the turtle and then we return. So let me compile and execute this for you. As you can see the turtle is drawing the left side first because the left recursive call has been made first. As you can see the invariant was satisfied, the turtle came back to the root and it also was pointing in the vertical direction. So now I will just make remarks on what we have seen so far. Let me just let you know that turtle graphics is discussed in the first chapter of the book. The most interesting use of turtle graphics is for drawing pictures which have interesting symmetry. You have to figure out the symmetry and represent it suitably in your code. We have seen two kinds of symmetry. One is iterative symmetry in which the same pattern is repeated. This for example happened in drawing the decorative design for the plate. We have also seen recursive symmetry. In this case the part of the picture is similar to the whole as was the case for the tree. Let me just remark that drawing highly patterned pictures where you can supply parameters to change the amount of iteration or recursion can be a very challenging project. That concludes this session in which we have seen turtle graphics and we have seen two interesting somewhat elaborate uses of the facilities available in turtle graphics.