 Welcome back, in the previous segment we talked about computer aided design in general and graphical user interfaces and what we want to do in this lecture which is designing a computer aided design program for solving, for designing and solving resistive circuits. So we will begin this segment with a demo of the program. So the program is in the filecircuit.cpp which I will show you but we will discuss the program a little bit more in detail later on. What I want you to observe when we run this program is that there are buttons in it and then these buttons enable you to add nodes, conductances, current sources and then the buttons also allow you to solve and I would like you to observe the solver output. So I am going to talk about conductances over here. So the conductance of an element is the inverse of its resistance. So these are just two terms used to denote the parameter of a circuit component. So it makes more sense to talk in terms of the conductance rather than the resistance and that is why we will be using that term. So let us do the demo. So this is our program, I am not going to show you, show it to you at great detail right now but let us compile it and let us run it towards the center. So this is the main canvas created by the program and you can see these four rectangles which are buttons. They are called buttons because if I click on them then some action will happen. And each button typically has a label. So here the labels are node, conductance, source, solve and quit and then labels are there just to tell you what the function of the button is. So this button called node allows us to create nodes in this which will be parts of a circuit. So let us start with that. So I am going to click on node and now I am allowed to click on the canvas and then that will create a node. So for example this has created a node which has been numbered 0. I want more nodes so I will click once more, now I get a node 1, I will click one more time and I will get node 2. So I am going to have a very simple circuit just 3 nodes and 3 components so I do not need any more nodes. So now I want to add conductances. So I will click on conductance and now I am expected to click on 2 nodes which have been created earlier. So I will click on this node and I will click on this node and as a result we will have a conductance inserted between these 2 nodes and furthermore you can see here that I am expected to type in the value of the conductance. So let us say I type in 5 as the value. So you can see that that value has appeared over here. Now in the circuit diagram I showed you there was a very specific standard symbol of a resistance drawn over here. So we can do that, we can do that but just to keep this program simple and because I want to illustrate the main ideas rather than the details I have kept this quite simple. Anyway so what has happened is that we have been able to insert a conductance between these 2 nodes. I want one more conductance and let us say I put down a conductance again so I click conductance and now I can click 2 nodes again so I click this node and I click this node. So another conductance has been entered and I am asked to give its value again. So let us say I will give the value again as 5. So you can see that 5 has appeared over here. Now I want to put in a source after all unless we have a source no current will flow. So let us put down a source over here and let us say I am going to make this source push current from vertex 2 to vertex 1. So I will click here first then I will click here and now I am going to I have to type in the source strength. So let us say my source strength is 5 or this is going to force 5 amperes of current through the circuit. And by the way the conductance values wormholes are 1 over 5 ohms if you like. So what has happened now is that this current source has been placed over here its value is 5 and as you can see the direction has been pointed out. So the direction is going from 2 to 1 and you may recognize this as our turtle. So I have put down the turtle over here but I really should put down something more consistent with a voltage source like a current source picture like the one we saw earlier. But again I wanted to illustrate the main ideas and this was sort of the easiest way of doing things. So we will see this how exactly this has been done but what I have done is that I have imprinted the turtle so as to give the direction of the flow of the current. So now say we ask this to be solved. So if we solve this so I have to click on this. So if I click on this what happens is I get the potentials at the various nodes. I guess I clicked twice and therefore I got the potentials 2 times. But anyway so what has happened? So this says that node 0 has single volt, 1 volt potential, 2 has 0 potential 0 and 1 has potential 2. So I think I said that these values are in most but they are actually resistances and so I am pushing a current of 5 amperes through this, sorry they are Mohs and so if I push a current through 5 amperes so let us just calculate what happens. So I have a 5 ampere current source and it flows through 1 over 5 ohms resistance and over here also 1 over 5 ohms resistance. So what does this do? So the total resistance is 2 over 5 ohms and so the voltage equal to sorry so the current equal to voltage upon the resistance and so we have 5 equals the voltage upon resistance and so the resistance and the resistance is 2 upon 5 so the total voltage drop is 2 upon 5 times 5 or equal to 2 volts. So what has happened over here? The total drop from here to here is 2 volts as we calculated and as you can see the potential at 1 is 2 and the potential at 2 is 0. So indeed there is a total drop of 2 volts going from here to here and furthermore the potential in the middle is exactly half and that is to be expected because we have half the resistance over here and half the resistance over here as well. So our program has worked, it has solved a circuit, it has allowed us to design the circuit and solve it and it has solved it as per our expectation. So at this point we can quit the program for that we press the quit button. So now I can go back to our presentation. So let us continue. So what are the main ingredients of the program? Well the immediate ingredient to hit you is the on-screen buttons and we observed that there is a label and the label is meant to indicate the functionality. This means of course that in our program we need code to decide whether the user clicked inside a button. So if the user clicked inside a button we need to take a certain action depending upon which button the user clicked. Then there were these circuit elements nodes. So these appear as circles on the canvas and we also put down their number inside the node itself. And again we had to click the buttons, click the nodes when we were inserting components. So again we need code to decide whether a click is inside a node or where is it. And nodes will also appear in the mathematical representation of the circuit. So after all we just do not want to do a drawing, we want to solve the circuit. So what is going to happen? Well a drawing will get made but as the drawing is made we will be building up a representation of the circuit itself. So collecting all its data and putting it into a nice convenient data structure. So nodes will appear in that data structure as well and exactly how they appear I will talk about in a minute. Then there are two major circuit elements, resistors or conductances and current sources. Again they should appear suitably on the screen. And for now in our baby program we are not going to have any occasion to click them. But otherwise in general we may want to have make them clickable. If we do that then we would need to have code to check whether clicks are lying inside these components as well. And these components will of course appear in the mathematical representation. So let me say a little bit about how our program seems to be required to be organized based on what we just said. Well as you saw it had a main loop. In the loop the program was waiting for the user to click and based on what was clicked the subsequent actions were different. So if I clicked the node button then clearly the program did something so that subsequently a node was created. So presumably it called a function and that function was the one which was responsible to enable the user to create nodes. And similarly if you create, if you clicked conductance then a function would have to be called in order for the user to be able to create conductances. Then we needed to do some management of the canvas. So what do I mean by that? Well we need to draw the circuit elements. I already said that we need to be able to decide whether the clicks are inside the circuit elements. So we need to decide what is clicked and then there is the mathematical representation. So the mathematical representation is what is needed for solving the circuit. So this is sort of what we can say based on observing the program that we just ran. So let me talk about the main program. So the main program will create the main entities. So these are the buttons and it will create a data structure that manages the screen and it will create a data structure that keeps track of the mathematical representation of the circuit and indeed it will go into a loop. So inside the loop it will call appropriate functionality depending upon what button the user is clicking. So now we are going to take a look at the main program. So here is the main program. So as you saw, as we discussed it sort of creates the main things, so what are the main things? Well first of all obviously it has to create the canvas and then it has to create these buttons which you saw and then it has to create the mathematical representation and a data structure which manages the drawing on the canvas. As this data structure in the process of managing the canvas we also have to modify the math representation. And therefore we will pass it a pointer to the math representation as well so that once it modifies the content on the screen it will also add relevant information to a math representation. Then there is the main loop. So the main loop has the program waiting for a click and at this point a click is expected only in one of these buttons. So if you click button node then you are going to ask your canvas content data structure to get a node added. Similarly if you click in B2 which is this button the user is telling the program that look I want a conductance added and therefore this function will be called. Similarly a source addition function will be called. If you click this before button then you want things solved, you want the circuit to be solved. Well if you want the circuit to be solved then we may directly call the solve member function in the math representation. And finally if button B5 is called which is the quit button then we want to break and after breaking the entire program will terminate. So what has, so some remarks about this main program as you can see the main program is at a very high level. So it is creating buttons, how exactly the buttons are created. So R is the rectangle being drawn, some text being put inside it that is not discussed at this level. So the constructor is called and we will see the constructor in a minute but the constructor is going to handle the job. Yes the constructor needs to be passed some values and those values are being passed and we will see what exactly those values mean. And then again, so the main program is creating all these entities and again as I said the creation is, the actual creation is in the constructor which are in the definitions of the entities. So at this level we are staying quite high, we are staying at a high level and we are just setting things up and the details we are not worrying about and similarly over here exactly what happens, what needs to happen in order to add a conductance that is not discussed over here that is a part of this member function. So let us get back to the presentation. So next we are going to discuss the class button. So this has to appear as a rectangle with text inside it and we are going to take the following approach. So the rectangle and text could be made graphic objects. So we could create a graphics object of type rectangle and of type text but in this program that is not needed because once we create that rectangle we are not going to have any occasion to move it around or similarly the text is not going to change. If that were changing then it would make sense to create these as graphics objects. So to keep the functionality simple we are simply going to imprint. So we are simply going to imprint the rectangle and the text on the canvas. So you will see this in a minute and then we will have a member function in which will say whether a click point is inside the rectangle. So that is how we will get to know whether a button has been clicked. So let us now view the class button from our program. So this was our main program and as you can see the main program is creating each button and it is passing some numbers and then the button is going to be called with the member function in. So in is going to be passed what value the click returns. So let us see how all this is going to work. So this is our class for representing buttons. So it has data members x, y, w and h and these data members are going to keep track of the center of the button. So that is the x, y will be the center of the button and w will be the width, h will be the height. Now here is the constructor which is going to be called with the title that should appear and then the x, y, w and h are passed as parameters. So these parameters are stored into the data members and then we create the rectangle. As you might remember to create the rectangle we need to specify the center coordinates and the width and the height and then we need to and we said we are going to imprint it. So by the way this is also a constructor call in case you have not realized it. So I do not have to construct an object by writing rectangle, space, name and then parameters. I can write it as rectangle of the parameter list and this will give me constructed object right here but it does not have a name but I do not want a name I just want to imprint it. So I can just dot imprint over in this manner. And similarly the text is also going to be created and imprinted. Then the member function in is quite simple. So we are getting the click value returned by get click and as you remember we have to get the most significant 16 bits or divide by 65536 to get the x value of the click position and take the remainder modulo 65536 to get the y value of the click position. And now if we want to know whether this click is inside, well for the click to be inside we need it to be the case that the distance, the x distance from the click position to the center, the x coordinate of the center has to be smaller than w and the y distance between the center and the click position should also be smaller than the height. So if both these conditions are satisfied and only if both these conditions are satisfied then we know that the click is inside the rectangle and therefore we check this condition and return the result. So if this condition is true we return true, if this condition is false we return false which is exactly what we wanted. So this is how the button class works. So again the important point is that the functional details are inside the class and not in the main program. So this way in the main program we can think at a high level. So let us go back to our presentation. So this is what we saw and now let us take a look at the class canvas content. So the canvas content class manages the canvas drawing and input, drawing and input for all components other than the buttons. So buttons are special because buttons have this control like function so we have kept them separately but the other circuit components how they appear on the screen and what needs to be drawn will be managed by this class. So what operations will it perform? Well it will have a member function add node which will be called in response to click on the node button. So we already saw that in the main program. And so the main program waits for the user to click, sorry so this function is going to wait for the user to click and then after the user clicks it is going to imprint a circle at the click point and not only just the circle but it is also going to put in the node number. So if you remember our execution of the program this is exactly what happened and it is going to keep track of the circle position in this node class why? Because later on we may want to know whether a click is inside a node and we also need to add this node into the mathematical representation. So these are the functions that our add node member function needs to perform in response to the user saying that I want a node and clicking on the canvas to get that node. Then there are other member functions add source and add conductance and these also act in response to button clicks. So here the program waits for users to select two nodes and this is handled by a member function select node and the program gets the conductance and current values from the keyboard and it imprints appropriate representation on the screen. So once the data is obtained, once the canvas has been painted with the right representation we have to add these components into the object math rep as well. So let us take a look at this canvas content object. So here is our canvas content object. So what does it keep track of? Well it has to keep track of what nodes are present on the screen. So therefore it needs this pointer to nodes and it also needs to know about the math representation. So it is going to have a member MRP which is going to be a pointer to the math representation. Here is the constructor class, the constructor sorry here is the constructor. The constructor is just going to be passed with a pointer to the math representation and that pointer is going to be stored over here. So let us take a look at add node. So add node is going to ask the user to click and once the user clicks we are going to create a new node at that click position. So we will see the node class in a second but the constructor of the node will expect a click value to be passed and it has to decide what its ID is. So we will pass NPTR dot size because currently we have had nodes going from 0 to NPTR dot size minus 1. So NPTR stores all the pointers to all the nodes created. So the next node will indeed be created at this position. At this index using this index and so that is what we passed and then we add this created node sorry we add a node to our math representation as well. So let us take a quick look at the node class. So the node class is over here it receives at the constructor for the constructor it receives a click value and the index it calculates where the click actually happened and then it creates a circle and imprints it at that position. So r is some 20 that we fixed and it imprints the text. So the text is just the index. So if you remember inside the node we had the node number printed. So this is what does it. Then the select node, so let us first look at add conductance. So add conductance is another member function inside our class campus content and this is going to be called when the user clicked the conductance button and this is the function which is responsible in helping the user to add a conductance. So how does it do it? It calls the select node function twice. So let us see now what the select node function does. So here is the select node function. So each call of select node waits for the user to click and then it checks whether the click is inside a node. If it is inside a node it will return the node index. So i1 is expected to be the index of the click node, i2 is expected to be the index of the second click node and this is just testing whether the click was inside a node actually if it is inside the node now we are going to actually do the processing that means the user has selected the two nodes between which the conductance has to be added. So what do we do here? So here we are going to have to draw the line connecting those two nodes. So we get the x coordinate and the y coordinate of the first click position, x coordinate and y coordinate of the second click position and then we actually draw this line. And then on our shell window, in our shell window we type out the message, give the conductance value and so then the conductance value is received. So then we can print the text at the center of that line of that edge with the value g. So if you might remember if the user added a conductance between say this vertex and this vertex then the conductance value was imprinted over here. So this is what is done in this text statement. And then we want to add this conductance into our math representation as well and that is what is being called over here. The source addition is very similar. So let me just quickly observe that again we are going to do select node and then we are going to draw the line, we are going to get the source strength and we are going to add the source into our math representation, we are going to put down the magnitude of the source and then we put that turtle. So the turtle is going to be created, when a turtle is created it is always created at the center of the screen. We want it to come to the center of that line, the line that we just draw. So this is the center of that line that we just drew. And here is a command, here is I guess an undocumented command on the turtle and this command says make the turtle face the second endpoint and then we make it move forward. So what this is going to do is that if this is the conductance and if this is the conductance value the turtle will first come to this point the center and then it will move in the direction of the second endpoint and it will come over here and it will get imprinted. So the purpose of all of this is to imprint, imprint but also imprint it a little bit away from the source strength value, that is it. So that is what happens in the conductance, our conductance code. So let us get back. So we just saw this and a few remarks all objects on the screen are imprinted because we do not need to move them and but we do need to keep track of their positions and extends to check if the user clicks on them. If we wanted the user to adjust the drawing then we would need to create and move around graphics objects and if you want to do dragging which is really the nicer way of doing things you can do it in simple CPP and that is described in chapter 20. So it will need a little bit more work but we really could do very nice pictures, we could draw a circuit diagram exactly like the one that I showed you in the very beginning but it will need more work. So what have we discussed in this segment? We said we discussed the organization of the main program, we discussed the canvas content class which manages the graphics and it draws components on the canvas and adds them to the math rep and the next we are going to talk about how we represent the circuit mathematically and how the class math rep is going to be designed but before that we will take a quick break.