Sunday, December 4, 2016

Coding Project 3 - STEM - Lou Person

STEM Lab by Lou Person: Draw a Box.

In this lab, you will use the Quite BASIC coding emulator to draw four lines next to each other and then modify the code to draw a box.  You will use a FOR statement to loop through code.  You will also work with the PLOT command, which has the following syntax: PLOT X, Y, C  where X is the X axis, Y is the Y axis and C is a color.  Also, you will use subroutines to plot the four lines.

First, enter this code into the Quite BASIC emulator to draw a line 10 blocks high:
2000 CLS
2110 LET X = 0
2115 LET L=10
2140 LET C = "green"
5000 REM Subroutine -- draw a line
5010 FOR Y = 0 TO L
5020 PLOT X,Y, C
5030 NEXT Y

Second, you will use a subroutine to call the common code which will draw a second line. 
5000 is the subroutine.  Add a line 2150 to call it the first time:
2150 GOSUB 5000
You will then need to add a RETURN at the end of the subroutine so it can return to the next line after 2150 once it completes:
5500 RETURN

You need to offset the second line to the right, so you will need to increase X by L (L is the length of each side of the square).  You will see it in this code:
2160 LET X=X+L
Then call the subroutine again after increasing X by L.
2170 GOSUB 5000
Also, add a program end after 2170 so the program stops after the last time the subroutine is called:
4000 END

Your code should now look like this:
2000 CLS
2110 LET X = 0
2115 LET L=10
2140 LET C = "green"
2150 GOSUB 5000
2160 LET X=X+L
2170 GOSUB 5000
4000 END
5000 REM Subroutine -- draw a line
5010 FOR Y = 0 TO L
5020 PLOT X,Y, C
5030 NEXT Y
5500 RETURN

Third, now you need to draw your horizontal lines.  Here we will draw the top.  Since the FOR loop with the PLOT command adds the blocks up and down (it changes Y, which is the vertical axis), you need a different FOR loop which adds blocks side to side (so it changes X).  This means, you will need to add a second subroutine and call it for the 2 horizontal lines.

Start the subroutine on line 6000.  You will create a FOR loop which changes the X access so your blocks go side to side within the PLOT statement:
6000 FOR X=0 TO L (since it is a square, L will be 10 because all sides are the same size).
Now put the same plot statement
6010 PLOT X,Y,C
6020 NEXT X (This increases the X access by 1 so blocks go side to side)
6030 RETURN (This returns to the point in the code where it was called).

Since you already called the subroutine twice to print the sides of the square, you now need to call it to print the top and bottom.  Add 2190 to call the new subroutine by changing 5000 to 6000
2190 GOSUB 6000
You also need to move the line up 10 spaces (offset the Y access by 10 so it starts at the top of the square).
2180 LET Y=10

Fourth, you will draw the bottom line.  Since we are at the bottom, the offset is 0 because it is the bottom.  In this case, the Y offset is 0. 
3000 LET Y=10
3010 GOSUB 6000

And your program will stop on line 4000 with the END

Your code should now look like this:
2000 CLS
2110 LET X = 0
2115 LET L=10
2140 LET C = "green"
2150 GOSUB 5000
2160 LET X=X+L
2170 GOSUB 5000
2180 LET Y=10
2190 GOSUB 6000
3000 LET Y=0
3010 GOSUB 6000
4000 END
5000 REM Subroutine -- draw a line
5010 FOR Y = 0 TO L
5020 PLOT X,Y, C
5030 NEXT Y
5500 RETURN
6000 FOR X=0 TO L
6010 PLOT X,Y,C
6020 NEXT X
6030 RETURN

Fifth, now that you are done, test what you have learned by making each line in the square a different color.  You will do this by setting C to blue, red or white before you call each subroutine by adding another line.  If you have trouble comment on this post.

Lab by Lou Person