Lab 7 C++ graphics

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.

Here is an image of a robot toy to give you an idea what we want to construct:

../../_images/SimpleRobot.png

Figuring our the data

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.

Note

This will not be so bad, just follow my lead!

Building the drawing program

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:

  • 0 = circle
  • 2 = triangle
  • 3 = rectangle

Now, we can write out our data as a set of numbers, line so:

0, 50, 50, 25
1, 200, 125, 100, 275, 200, 375
9

Note

That last number is bigger than any number we use for a shape.

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.

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!

The main program code

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.

We will use a new C++ statement to control the actual drawing. Here is the code we need to generate our drawing:

int data[12] = {
    0, 50, 50, 25,
    1, 200, 125, 100, 275, 200, 375,
    9
};


void drawScene(void) {

    int index = 0;
    int shape_type;
    int done = 0;

    clearWindow();
    setColor(YELLOW);
    index = 0;
    for(int i=0; i<2;i++) {
        shape_type = data[index];     // this should be the shape code
        index = index + 1;      // now index point to the first parameter
        switch(shape_type) {
            case 0: // circle
                drawFilledCircle(
                   data[index], data[index+1], data[index+2]);
                index = index + 3;
                break;
            case 1: // triangle
                drawFilledTriangle(
                    data[index], data[index+1], 
                    data[index+2], data[index+3], 
                    data[index+4], data[index+5]);
                index = index + 6;
                break;
            default: // done with shape loop
                done = 1;
        }
        if (done == 1) break;
    }
    glEnd();
    glFlush();
    glutSwapBuffers();
    glEnd();
}

The switch statement

There is a new statement in this code, the&nbsp;switch&nbsp;statement. Basically, this statement is a replacement for what would be a complicated nested if-then-else statement, It works like this:

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.

The&nbsp;switch(shape_type)&nbsp;part of this statement examines the variable (shape_type&nbsp;in this example). What comes after the&nbsp;switch&nbsp;line&nbsp;is a C++&nbsp;block, which is a series of things inside curly braces. In this statement, each possible value we want to check is identified by a&nbsp;case&nbsp;value: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).

After each&nbsp;case&nbsp;value:&nbsp;line, we place a series of statements (indented) that will be processed only if the value of the variable matches the&nbsp;value&nbsp;on the&nbsp;case&nbsp;line.

For our program, the last line in this set of statements should be a&nbsp;break&nbsp;line. The&nbsp;break&nbsp;“breaks” us out of the&nbsp;switch&nbsp;statement, and we continue processing on the statement following the close curly bracket. In special cases, programmers leave the&nbsp;break&nbsp;out, but what happens if beyond this course, so make sure you remember to put this statement in as shown.

If the value in&nbsp;shape_type&nbsp;does not match any of the values listed on the set of&nbsp;case&nbsp;lines you include, we can do something by default. We just include a final&nbsp;default:&nbsp;line, followed by the code we want to process. In this set of code, we do not need a final&nbsp;break, since this chunk of code comes at the end of the&nbsp;switch&nbsp;statement.

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!

How this code works

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!)

The main&nbsp;for-loop&nbsp;uses the counter to track the&nbsp;shapes&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&nbsp;index&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&nbsp;data[0]). Since the number in the&nbsp;data&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:

data[0] = 0 - draw a circle
data[1] = 50 - the x coordinate
data[2] = 50 - the y coordinate
data[3] = 25 - the circle radius

The&nbsp;case&nbsp;0:&nbsp;line is where the code for drawing the circle needs to go. Notice that I am using theindex&nbsp;variable to send the values from the&nbsp;data&nbsp;array into the drawing function.

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&nbsp;data[4]. If we add 4 to Index, then&nbsp;index&nbsp;will be “pointing” to the next shape number in the array. In&nbsp;data[index]&nbsp;(data[4]&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.

Each&nbsp;case&nbsp;block needs to add something to the&nbsp;index&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).

Previous answers to this question


This is a preview of an assignment submitted on our website by a student. If you need help with this question or any assignment help, click on the order button below and get started. We guarantee authentic, quality, 100% plagiarism free work or your money back.

order uk best essays Get The Answer
Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *

Lab 7 C++ graphics

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.

Here is an image of a robot toy to give you an idea what we want to construct:

../../_images/SimpleRobot.png

Figuring our the data

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.

Note

This will not be so bad, just follow my lead!

Building the drawing program

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:

  • 0 = circle
  • 2 = triangle
  • 3 = rectangle

Now, we can write out our data as a set of numbers, line so:

0, 50, 50, 25
1, 200, 125, 100, 275, 200, 375
9

Note

That last number is bigger than any number we use for a shape.

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.

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!

The main program code

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.

We will use a new C++ statement to control the actual drawing. Here is the code we need to generate our drawing:

int data[12] = {
    0, 50, 50, 25,
    1, 200, 125, 100, 275, 200, 375,
    9
};


void drawScene(void) {

    int index = 0;
    int shape_type;
    int done = 0;

    clearWindow();
    setColor(YELLOW);
    index = 0;
    for(int i=0; i<2;i++) {
        shape_type = data[index];     // this should be the shape code
        index = index + 1;      // now index point to the first parameter
        switch(shape_type) {
            case 0: // circle
                drawFilledCircle(
                   data[index], data[index+1], data[index+2]);
                index = index + 3;
                break;
            case 1: // triangle
                drawFilledTriangle(
                    data[index], data[index+1], 
                    data[index+2], data[index+3], 
                    data[index+4], data[index+5]);
                index = index + 6;
                break;
            default: // done with shape loop
                done = 1;
        }
        if (done == 1) break;
    }
    glEnd();
    glFlush();
    glutSwapBuffers();
    glEnd();
}

The switch statement

There is a new statement in this code, the&nbsp;switch&nbsp;statement. Basically, this statement is a replacement for what would be a complicated nested if-then-else statement, It works like this:

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.

The&nbsp;switch(shape_type)&nbsp;part of this statement examines the variable (shape_type&nbsp;in this example). What comes after the&nbsp;switch&nbsp;line&nbsp;is a C++&nbsp;block, which is a series of things inside curly braces. In this statement, each possible value we want to check is identified by a&nbsp;case&nbsp;value: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).

After each&nbsp;case&nbsp;value:&nbsp;line, we place a series of statements (indented) that will be processed only if the value of the variable matches the&nbsp;value&nbsp;on the&nbsp;case&nbsp;line.

For our program, the last line in this set of statements should be a&nbsp;break&nbsp;line. The&nbsp;break&nbsp;“breaks” us out of the&nbsp;switch&nbsp;statement, and we continue processing on the statement following the close curly bracket. In special cases, programmers leave the&nbsp;break&nbsp;out, but what happens if beyond this course, so make sure you remember to put this statement in as shown.

If the value in&nbsp;shape_type&nbsp;does not match any of the values listed on the set of&nbsp;case&nbsp;lines you include, we can do something by default. We just include a final&nbsp;default:&nbsp;line, followed by the code we want to process. In this set of code, we do not need a final&nbsp;break, since this chunk of code comes at the end of the&nbsp;switch&nbsp;statement.

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!

How this code works

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!)

The main&nbsp;for-loop&nbsp;uses the counter to track the&nbsp;shapes&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&nbsp;index&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&nbsp;data[0]). Since the number in the&nbsp;data&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:

data[0] = 0 - draw a circle
data[1] = 50 - the x coordinate
data[2] = 50 - the y coordinate
data[3] = 25 - the circle radius

The&nbsp;case&nbsp;0:&nbsp;line is where the code for drawing the circle needs to go. Notice that I am using theindex&nbsp;variable to send the values from the&nbsp;data&nbsp;array into the drawing function.

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&nbsp;data[4]. If we add 4 to Index, then&nbsp;index&nbsp;will be “pointing” to the next shape number in the array. In&nbsp;data[index]&nbsp;(data[4]&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.

Each&nbsp;case&nbsp;block needs to add something to the&nbsp;index&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).

Previous answers to this question


This is a preview of an assignment submitted on our website by a student. If you need help with this question or any assignment help, click on the order button below and get started. We guarantee authentic, quality, 100% plagiarism free work or your money back.

order uk best essays Get The Answer
Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *