Wednesday, February 2, 2011

Basic Programming 101: Session IV

For-Loop and Counting

The for-loop construct allows us control specific execution of a programs actions. It says: "do this for this many times." The program can count up, count down, count by fractions, whole numbers or whichever scalar value we choose. Recursion, although powerful, may have resource limitations (every function call takes memory). If the algorithm doesn't require "state" or memory, a for-loop does nicely.
We have two examples here, a very short program that shows the for-loop and a second program that graphs functions.
10 REM Simple for-loop prints nums 1..10
20 for i = 1 to 10
30 print i
40 next i
Function Grapher
In this next example, we build a function graphing program that steps through a range of values and plots each of the points. We could do this in whole number steps, but the plotted functions may not appear continuous; they'll look more like points than connected curves/lines. The finer the grain of step, the more likely the function appears continuous.
Lines 10 & 100 set up some constants and open a graphics window of the appropriate size. In this case, we keep two constants which represent the center of our graphics screen.
10 xc = 250 : yc = 200
100 graphics window 25,25,xc*2,yc*2
Next, Line 110 contains our for-loop. It has a range of [-250..250] and granularity (step) of 1/10. You might need to make this finer (smaller) depending upon the kind of function you have. In this case, it could be step 1, because the function is a simple line.
110 for t = -250 to 250 step 0.1
Lines 120 and 130 contain the function. We set variables x and y to t. This will graph the line y=x. You might wonder why we didn't just make x the for-loop variable ("for x = -250 to 250"). There's a good reason for this, I will explain below.
120 x = t
130 y = t
Line 140 does our work: set a point at . Not really because we have xc and yc in the calculations. Remember, is the center of the graphics window (the size of the window is 2*xc, 2*yc). Also, note that we subtract y from yc instead of add. That's because graphics windows are upside-down when compared to Euclidean graphs, which have positive Y going up the page. Computer graphics windows have positive Y going down the page and the origin (0,0) in the upper-left corner, a natural consequence of the way information is displayed on a computer's screen.
140 graphics pset xc+x,yc-y
Finally, we close the for-loop in Line 150 and, as I like to be disciplined in my coding, Line 160 ends the program. This line is not strictly needed, but if we ever implement a subroutine, you would want the program to end here, so make it a habit.
150 next t
160 end
Why is X not the loop variable?
We could have made x the loop variable, but didn't. Why? Because, not every equation to be plotted has only one value of y for every x. What does that mean? Consider the equation y^2 = x. This is a parabola, but tilted on its side 90 degrees. In all but one case, there are two values of y that satisfy any particular x. If x = 4, y = 2 or -2. To plot this equation, use y = t and x = t^2.
Making a circle would be a challenge, since the equation for a circle is x^2 + y^2 = r^2. Fortunately, BASIC, like all good programming languages, provides a math library to assist us. Some of you may not have had trigonometry, yet, but there are well known functions from trig that address this problem: sine and cosine. In BASIC, these are sin and cos. They take an angle and return the Y or X value appropriate for that angle. In fact, cos(t)^2 + sin(t)^2 = 1 for every angle t. The math library uses radians, instead of degrees.
To plot a circle, use x = 100*cos(t) and y = 100*sin(t). This paints a circle with radius 100. If you want an oval, change the constant (100) in either or both of the equations.
In fact, playing around with programs is a great way to understand how they work. Feel free to play with this code and see what types of shapes and patterns you can draw. I've included some of the interesting ones I've found below. Use the comments section to add your own and tell us what it makes.
Infinity: [-250..250],1/10,x=110*cos(t),y=100*sin(t)*cos(t)
Spiral: [0..250],1/1000,x=t^2*cos(t)/2,y=t^2*sin(t)/2

No comments:

Post a Comment