Sunday, November 27, 2016

Coding Project 2 - STEM - Lou Person

STEM Lab by Lou Person: Draw four boxes.

In this lab, you will use the Quite BASIC coding emulator to draw four boxes in a row.  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.

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

Second, run the code.  You'll see the 4 boxes appear.

Third, change the value in the FOR statement from 4 to 10.  What happens?

Fourth, change the word "green" to "red".  What happens when you run the code?

Fifth, change the PLOT statement to say PLOT X+5,Y,C and run the code.  What happens?

Sixth, change the PLOT statement to say PLOT X+Y,Y,C and run the code.  What happens?

The FOR statement gets executed starting at 0 up TO the number of times you enter.  So, when you change it to 10, you should notice that 10 boxes appeared instead of 4.

The statement LET defines a variable.  In line 2140, you defined a string variable which is why the word is in between double quotes.  If you change the color to another color, such as red, the boxes are drawn in red.  In line 5020, the PLOT command uses C to determine what color it should draw the boxes in.  So when you change C to a different color, the PLOT statement will draw the boxes in that color.

The PLOT statement draws a box, or pixel, based on the X and Y coordinates provided.  This is similar to a chart with an X and Y axis.  By adding 5 (X+5) to the X access, the line of boxes starts 5 boxes to the right.  This is also known as offsetting by 5 pixels. 

Each time the FOR loop is executed, the values will be:
X     Y
4      0
4      1
4      2
4      3
4      4

To make the line horizontal, each box will need to move up 1 pixel and right 1 pixel.  The Y access increases 1 each time.  In order to move 1 to the right each time, add Y to X.  The coordinates will look like this:
X     Y
4      0
5      1
6      2
7      3
8      4

These are the coordinates that represent a horizontal line.

No comments:

Post a Comment