{"id":23655,"date":"2018-02-20T23:50:30","date_gmt":"2018-02-20T23:50:30","guid":{"rendered":"https:\/\/writemyessayfree.com\/lab-7-c-graphics"},"modified":"2017-08-03T16:09:57","modified_gmt":"2017-08-03T16:09:57","slug":"lab-7-c-graphics","status":"publish","type":"post","link":"https:\/\/www.benedictsol.com\/blogs\/lab-7-c-graphics\/","title":{"rendered":"Lab 7 C++ graphics"},"content":{"rendered":"<div class=\"the_content_wrapper\">\n<p>For this lab, we will draw a simple robot. However, instead of going about this problem the way we did earlier, we will set up a data array with all the information we need. I will show you basically how to do this, you will need to come up with the numbers that build your robot.<\/p>\n<p>Here is an image of a robot toy to give you an idea what we want to construct:<\/p>\n<p><img decoding=\"async\" class=\"align-center\" src=\"http:\/\/www.co-pylit.org\/courses\/spring2017\/cosc1315\/_images\/SimpleRobot.png\" alt=\"..\/..\/_images\/SimpleRobot.png\"><\/p>\n<div id=\"figuring-our-the-data\" class=\"section\">\n<h3>Figuring our the data<\/h3>\n<p>As before, you will need to sketch your robot and figure out the approximate values you need for parameters to the shape drawing routines you will need to use. We will record those numbers in a big array of integer numbers, and trick our program into fetching the data needed for each function call from the array.<\/p>\n<div class=\"admonition note inline-title\">\n<p class=\"first admonition-title\">Note<\/p>\n<p class=\"last\">This will not be so bad, just follow my lead!<\/p>\n<\/p><\/div>\n<\/p><\/div>\n<div id=\"building-the-drawing-program\" class=\"section\">\n<h3>Building the drawing program<\/h3>\n<p>To create our drawing, we need to write down (on paper, not code) the sequence of shapes we want to draw, and for each shape, write down the numbers we will pass in as parameters on the appropriate function call line. Next, we will assign a simple number for each shape. Something like this:<\/p>\n<blockquote>\n<div>\n<ul class=\"simple\">\n<li>0 = circle<\/li>\n<li>2 = triangle<\/li>\n<li>3 = rectangle<\/li>\n<\/ul><\/div>\n<\/blockquote>\n<p>Now, we can write out our data as a set of numbers, line so:<\/p>\n<div class=\"highlight-text\">\n<div class=\"highlight\">\n<pre>0, 50, 50, 25\n1, 200, 125, 100, 275, 200, 375\n9\n<\/pre>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"admonition note inline-title\">\n<p class=\"first admonition-title\">Note<\/p>\n<p class=\"last\">That last number is bigger than any number we use for a shape.<\/p>\n<\/p><\/div>\n<p>This set of numbers should draw a circle at x = 50, y = 50, with radius = 25, followed by a triangle drawn from x1 = 200, y1 = 125, and so on.<\/p>\n<p>We will record all of these numbers , in order, in a simple data array, then write code to pull these numbers back out as needed to generate our drawing!<\/p>\n<\/p><\/div>\n<div id=\"the-main-program-code\" class=\"section\">\n<h3>The main program code<\/h3>\n<p>You will need to create a loop that works through the entire data array. Your loop will need to be coded so it checks the shape value, then pulls out the numbers needed for the right function call.<\/p>\n<p>We will use a new C++ statement to control the actual drawing. Here is the code we need to generate our drawing:<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre><span class=\"nb\">int<\/span> <span class=\"n\">data<\/span><span class=\"p\">[<\/span><span class=\"mi\">12<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n    <span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">50<\/span><span class=\"p\">,<\/span> <span class=\"mi\">50<\/span><span class=\"p\">,<\/span> <span class=\"mi\">25<\/span><span class=\"p\">,<\/span>\n    <span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"mi\">200<\/span><span class=\"p\">,<\/span> <span class=\"mi\">125<\/span><span class=\"p\">,<\/span> <span class=\"mi\">100<\/span><span class=\"p\">,<\/span> <span class=\"mi\">275<\/span><span class=\"p\">,<\/span> <span class=\"mi\">200<\/span><span class=\"p\">,<\/span> <span class=\"mi\">375<\/span><span class=\"p\">,<\/span>\n    <span class=\"mi\">9<\/span>\n<span class=\"p\">};<\/span>\n\n\n<span class=\"n\">void<\/span> <span class=\"n\">drawScene<\/span><span class=\"p\">(<\/span><span class=\"n\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n    <span class=\"nb\">int<\/span> <span class=\"n\">index<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n    <span class=\"nb\">int<\/span> <span class=\"n\">shape_type<\/span><span class=\"p\">;<\/span>\n    <span class=\"nb\">int<\/span> <span class=\"n\">done<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"n\">clearWindow<\/span><span class=\"p\">();<\/span>\n    <span class=\"n\">setColor<\/span><span class=\"p\">(<\/span><span class=\"n\">YELLOW<\/span><span class=\"p\">);<\/span>\n    <span class=\"n\">index<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n    <span class=\"k\">for<\/span><span class=\"p\">(<\/span><span class=\"nb\">int<\/span> <span class=\"n\">i<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">;<\/span> <span class=\"n\">i<\/span><span class=\"o\">&lt;<\/span><span class=\"mi\">2<\/span><span class=\"p\">;<\/span><span class=\"n\">i<\/span><span class=\"o\">++<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"n\">shape_type<\/span> <span class=\"o\">=<\/span> <span class=\"n\">data<\/span><span class=\"p\">[<\/span><span class=\"n\">index<\/span><span class=\"p\">];<\/span>     <span class=\"o\">\/\/<\/span> <span class=\"n\">this<\/span> <span class=\"n\">should<\/span> <span class=\"n\">be<\/span> <span class=\"n\">the<\/span> <span class=\"n\">shape<\/span> <span class=\"n\">code<\/span>\n        <span class=\"n\">index<\/span> <span class=\"o\">=<\/span> <span class=\"n\">index<\/span> <span class=\"o\">+<\/span> <span class=\"mi\">1<\/span><span class=\"p\">;<\/span>      <span class=\"o\">\/\/<\/span> <span class=\"n\">now<\/span> <span class=\"n\">index<\/span> <span class=\"n\">point<\/span> <span class=\"n\">to<\/span> <span class=\"n\">the<\/span> <span class=\"n\">first<\/span> <span class=\"n\">parameter<\/span>\n        <span class=\"n\">switch<\/span><span class=\"p\">(<\/span><span class=\"n\">shape_type<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n            <span class=\"n\">case<\/span> <span class=\"mi\">0<\/span><span class=\"p\">:<\/span> <span class=\"o\">\/\/<\/span> <span class=\"n\">circle<\/span>\n                <span class=\"n\">drawFilledCircle<\/span><span class=\"p\">(<\/span>\n                   <span class=\"n\">data<\/span><span class=\"p\">[<\/span><span class=\"n\">index<\/span><span class=\"p\">],<\/span> <span class=\"n\">data<\/span><span class=\"p\">[<\/span><span class=\"n\">index<\/span><span class=\"o\">+<\/span><span class=\"mi\">1<\/span><span class=\"p\">],<\/span> <span class=\"n\">data<\/span><span class=\"p\">[<\/span><span class=\"n\">index<\/span><span class=\"o\">+<\/span><span class=\"mi\">2<\/span><span class=\"p\">]);<\/span>\n                <span class=\"n\">index<\/span> <span class=\"o\">=<\/span> <span class=\"n\">index<\/span> <span class=\"o\">+<\/span> <span class=\"mi\">3<\/span><span class=\"p\">;<\/span>\n                <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n            <span class=\"n\">case<\/span> <span class=\"mi\">1<\/span><span class=\"p\">:<\/span> <span class=\"o\">\/\/<\/span> <span class=\"n\">triangle<\/span>\n                <span class=\"n\">drawFilledTriangle<\/span><span class=\"p\">(<\/span>\n                    <span class=\"n\">data<\/span><span class=\"p\">[<\/span><span class=\"n\">index<\/span><span class=\"p\">],<\/span> <span class=\"n\">data<\/span><span class=\"p\">[<\/span><span class=\"n\">index<\/span><span class=\"o\">+<\/span><span class=\"mi\">1<\/span><span class=\"p\">],<\/span> \n                    <span class=\"n\">data<\/span><span class=\"p\">[<\/span><span class=\"n\">index<\/span><span class=\"o\">+<\/span><span class=\"mi\">2<\/span><span class=\"p\">],<\/span> <span class=\"n\">data<\/span><span class=\"p\">[<\/span><span class=\"n\">index<\/span><span class=\"o\">+<\/span><span class=\"mi\">3<\/span><span class=\"p\">],<\/span> \n                    <span class=\"n\">data<\/span><span class=\"p\">[<\/span><span class=\"n\">index<\/span><span class=\"o\">+<\/span><span class=\"mi\">4<\/span><span class=\"p\">],<\/span> <span class=\"n\">data<\/span><span class=\"p\">[<\/span><span class=\"n\">index<\/span><span class=\"o\">+<\/span><span class=\"mi\">5<\/span><span class=\"p\">]);<\/span>\n                <span class=\"n\">index<\/span> <span class=\"o\">=<\/span> <span class=\"n\">index<\/span> <span class=\"o\">+<\/span> <span class=\"mi\">6<\/span><span class=\"p\">;<\/span>\n                <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n            <span class=\"n\">default<\/span><span class=\"p\">:<\/span> <span class=\"o\">\/\/<\/span> <span class=\"n\">done<\/span> <span class=\"k\">with<\/span> <span class=\"n\">shape<\/span> <span class=\"n\">loop<\/span>\n                <span class=\"n\">done<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">1<\/span><span class=\"p\">;<\/span>\n        <span class=\"p\">}<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">done<\/span> <span class=\"o\">==<\/span> <span class=\"mi\">1<\/span><span class=\"p\">)<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n    <span class=\"n\">glEnd<\/span><span class=\"p\">();<\/span>\n    <span class=\"n\">glFlush<\/span><span class=\"p\">();<\/span>\n    <span class=\"n\">glutSwapBuffers<\/span><span class=\"p\">();<\/span>\n    <span class=\"n\">glEnd<\/span><span class=\"p\">();<\/span>\n<span class=\"p\">}<\/span>\n<\/pre>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div id=\"the-switch-statement\" class=\"section\">\n<h3>The switch statement<\/h3>\n<p>There is a new statement in this code, the&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">switch<\/span>&amp;nbsp;<span class=\"pre\">statement<\/span><\/code>. Basically, this statement is a replacement for what would be a complicated nested if-then-else statement, It works like this:<\/p>\n<p>Suppose you have an integer variable that can hold one of a defined set of values and you want to process different code depending on the actual value in that variable.<\/p>\n<p>The&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">switch(shape_type)<\/span><\/code>&amp;nbsp;part of this statement examines the variable (<code class=\"docutils literal\"><span class=\"pre\">shape_type<\/span><\/code>&amp;nbsp;in this example). What comes after the&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">switch<\/span>&amp;nbsp;<span class=\"pre\">line<\/span><\/code>&amp;nbsp;is a C++&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">block<\/span><\/code>, which is a series of things inside curly braces. In this statement, each possible value we want to check is identified by a&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">case<\/span>&amp;nbsp;<span class=\"pre\">value:<\/span><\/code>line. In the example above, I have set up simple numbers for each possible shape I want to draw (well not all of them, this is just an example).<\/p>\n<p>After each&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">case<\/span>&amp;nbsp;<span class=\"pre\">value:<\/span><\/code>&amp;nbsp;line, we place a series of statements (indented) that will be processed only if the value of the variable matches the&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">value<\/span><\/code>&amp;nbsp;on the&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">case<\/span><\/code>&amp;nbsp;line.<\/p>\n<p>For our program, the last line in this set of statements should be a&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">break<\/span><\/code>&amp;nbsp;line. The&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">break<\/span><\/code>&amp;nbsp;\u201cbreaks\u201d us out of the&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">switch<\/span><\/code>&amp;nbsp;statement, and we continue processing on the statement following the close curly bracket. In special cases, programmers leave the&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">break<\/span><\/code>&amp;nbsp;out, but what happens if beyond this course, so make sure you remember to put this statement in as shown.<\/p>\n<p>If the value in&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">shape_type<\/span><\/code>&amp;nbsp;does not match any of the values listed on the set of&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">case<\/span><\/code>&amp;nbsp;lines you include, we can do something by default. We just include a final&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">default:<\/span><\/code>&amp;nbsp;line, followed by the code we want to process. In this set of code, we do not need a final&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">break<\/span><\/code>, since this chunk of code comes at the end of the&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">switch<\/span>&amp;nbsp;<span class=\"pre\">statement<\/span><\/code>.<\/p>\n<p>Phew, sounds complicated, but it is not too bad, and can be handy. Like everything in programming, you only use statements if you feel that they will help you get your job done. IN this case, it is just what we need to control our drawing!<\/p>\n<\/p><\/div>\n<div id=\"how-this-code-works\" class=\"section\">\n<h3>How this code works<\/h3>\n<p>The code shown above is a bit tricky, but it gets the job done! I figured this out a million years ago when I was trying to draw things in a language called FORTRAN! (Wait!, I am not really that old!)<\/p>\n<p>The main&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">for-loop<\/span><\/code>&amp;nbsp;uses the counter to track the&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">shapes<\/span><\/code>&amp;nbsp;we want to draw. In the example data set, there are two shapes, plus another number that will stop the loop, in case I mess up the data somehow! I set up another variable named&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">index<\/span><\/code>&amp;nbsp;that I will use to point to the first number for a shape in the data array. Initially, this variable is initialized to zero (remember, the first number in the array, which is at&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">data[0]<\/span><\/code>). Since the number in the&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">data<\/span><\/code>&amp;nbsp;array is a zero in our example, that means we will draw a circle, and we need three additional numbers. Those numbers will be at:<\/p>\n<div class=\"highlight-text\">\n<div class=\"highlight\">\n<pre>data[0] = 0 - draw a circle\ndata[1] = 50 - the x coordinate\ndata[2] = 50 - the y coordinate\ndata[3] = 25 - the circle radius\n<\/pre>\n<\/p><\/div>\n<\/p><\/div>\n<p>The&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">case<\/span>&amp;nbsp;<span class=\"pre\">0:<\/span><\/code>&amp;nbsp;line is where the code for drawing the circle needs to go. Notice that I am using the<code class=\"docutils literal\"><span class=\"pre\">index<\/span><\/code>&amp;nbsp;variable to send the values from the&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">data<\/span><\/code>&amp;nbsp;array into the drawing function.<\/p>\n<p>What happens at after the call to the drawing function is key to getting this all to work. For the next shape we want to draw, the shape number is in&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">data[4]<\/span><\/code>. If we add 4 to Index, then&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">index<\/span><\/code>&amp;nbsp;will be \u201cpointing\u201d to the next shape number in the array. In&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">data[index]<\/span><\/code>&amp;nbsp;(<code class=\"docutils literal\"><span class=\"pre\">data[4]<\/span><\/code>&amp;nbsp;now), we find our next shape number, The parameters for that shape are passed in as parameters exactly as we did for the circle.<\/p>\n<p>Each&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">case<\/span><\/code>&amp;nbsp;block needs to add something to the&amp;nbsp;<code class=\"docutils literal\"><span class=\"pre\">index<\/span><\/code>&amp;nbsp;variable, but exactly what that something is depends on the shape being drawn. For the circle, it was 3, for the triangle, it will be 6 (can you see why).<\/p>\n<\/p><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>For this lab, we will draw a simple robot. However, instead of going about this problem the way we did earlier, we will set up a data array with all the information we need. I will show you basically how <a href=\"https:\/\/www.benedictsol.com\/blogs\/lab-7-c-graphics\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-23655","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/23655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/comments?post=23655"}],"version-history":[{"count":0,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/23655\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/media?parent=23655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/categories?post=23655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/tags?post=23655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}