Because of this, you should be careful with declaring variables with similar names. AppleSoft BASIC will not warn you about the name conflict; instead it will simply assign a value to a variable where you don't expect it, resulting in errors that are hard to find. Take a look at this example:.
To switch between them use PR 0 and PR 3 commands. Why do we need two different text modes? Sometimes we want to be able to fit more information in one screen. Most examples in this tutorial don't depend specifically on which text mode they run in; however in cases where it matters you should not forget to set the proper mode.
To switch to text mode from graphics mode, use TEXT command. These commands are often combined in one line, separated by colon.
Coordinates increase as we move right and down, the bottom right corner having position 24,40 in characters-per-line mode and 24,80 in characters-per-line mode.
Text modes don't support colors; they are black and white in our case black and green , however you can swap these colors to print black text on green background with INVERSE command. A common question asked about the TEXT mode is whether it is possible to place text at a specific position on screen rather than in the top left corner. So in order to print something starting on third line from top of the screen, at the leftmost position, you set cursor position with "VTAB 3 : HTAB 1".
The following program takes input string from user and outputs the same string in the center of the screen. If string is less than 30 character long, it uses 40x24 mode, otherwise it switches to 80x24 mode. Keep in mind that valid values for VTAB are Each of them has two sub-modes: for full-screen graphics and partial screen graphics also called "mixed" mode, where upper part of the screen contains graphics, and lower part of the screen can contain text, which can be outputted with PRINT command, for example; essentially in the mixed mode the bottom part of the screen works in TEXT mode.
Mixed graphics mode is 40x40 pixels, full-screen mode 40x48 pixels; both lo-res submodes support 16 colors including black, which has index of zero. HGR2 resolution is x pixels. Hi-res modes have just eight colors including black, which has index of zero. As we move right, X coordinate increases; as we move down the screen, Y coordinates increases. To start printing on them, move text cursor first to one of the four bottom lines for example: VTAB To fill quickly entire screen in hi-resolution mode with current color, use "CALL " Note: you must plot at least one pixel with HPLOT after setting current color in order for this to work.
Here X and Y are coordinates of the pixel you want to plot. The following tiny program draws a hyperboloid , a complex 3-dimensional geometric shape, using only straight lines:. Don't try to parse everything in this program unless you are familiar with trigonometric functions COS and SIN, which are used in this program.
We will get down to the details of this program later. Then we call HGR2 to initialize Hi-res full-screen mode with x resolution. HGR2 also clears the screen with black. Unless we explicitly set the current color, it will be black and we would not be able to see anything we draw on a black background. Low-resolution mode has resolution of 40 by 48 pixels in full-screen sub-mode and supports 16 colors.
Let's check how these colors and pixels actually look:. Let's look at this program line by line. C will be changing from 0 to LINE 30 sets current color to C. In low-res mode we have 48 vertical coordinates; we have 16 colors, so I want to draw 16 horizontal lines each using its own color, 3 pixels in height 16 times 3 is LINE 80 starts another loop, to draw individual pixels of all 16 colors along the horizontal line I just painted.
I use another FOR loop with variable X. X is used to both set current color and control the coordinate of a pixel I plot in this loop. From the resulting image you can see that GR mode indeed supports 16 different colors, except gray colors 6 and 11 are not distinguishable one from another; also the size of a single pixel is very large.
Plus, the pixels are not square: the visible pixel's width is almost twice as big as its height. We will look at colors of the hi-res mode later in this tutorial. What we already can conclude, however, is that for any graphical images requiring a level of detail we need to use BASIC's hi-res mode. The low-res mode has two advantages: twice as many colors, and ability to fill rectangular areas on screen quickly with a solid color.
But the low-res graphics are not as refined, plus it lacks the function to draw a line between two arbitrary points. If you are serious about learning programming, you know that computers save all information as ones and zeros. Individual ones and zeros are called bits, and bits are grouped in bytes, 8 bits per byte.
So how computer stores number 45, for example? To store a number, it converts it into special format having only 1s and 0s. This format is called binary, and numbers written in this format are called binary numbers. Decimal number 5 is written as in binary format. The system most of us familiar with for writing numbers is called decimal. This is because its base is ten: we use ten different symbols digits from 0 to 9 to write the value of a number.
When we want to write a value greater than 9, we use a sequence if decimal digits, for example 57 means a number equal to "five, multiplied by ten, plus seven".
The binary system uses 2 as the base. It means that any natural number or zero in it can only use combination of 0s and 1s. In this lesson we look at BASIC program which converts any natural number we enter in decimal form into binary format. Then we will write a program which converts a binary to decimal. The method to convert a number A into binary format looks as follows: Is A even or odd?
If A is odd, write number 1. If A is even, write 0. Remember the number we just written 1 or 0 as B. Subtract B from A and divide the result in half. Then assign the result of the division to A. If A is equal to zero, we are finished. If A is greater than zero, repeat step 1, using new value of A, but write the next 1 or 0 to the left of the bits already written. Five is odd, so we write 1 at the right end of a piece of paper.
We subtract 1 from A and divide the result by 2. We now assign A the new value of 2. Two is even, so we write 0 to the left of 1 we already have 01 become 2 right binary digits of decimal number 5.
B is now 0. Now we have 3 binary digits: We stop the algorithm, since A is now zero. Decimal number 5 is written as in binary form. In this implementation I am playing a trick: even though X denotes a binary number, I internally store it as decimal.
Variable P holds a multiplier which is used to multiply B, the current bit. The product of this multiplication is then added to X. P is initially set to 1, but with each next bit it grows by 10 times: 10, , and so on. At the same time, it has its drawbacks: when A is large enough, X quickly runs out of valid bits to hold a gigantic decimal value:. In order to fix this issue, let's instead use a string variable, which will hold the string representation of the binary.
This way the program is much smaller and also easier to understand:. Now let's look at the program which does the opposite: it takes a binary number as string containing only ones and zeros and converts it to a decimal number. The program looks as follows:. In this implementation, we determine the length of the entered string LINE When FOR.. We assign variables P and S to zero. Is this really necessary? BASIC assigns all numeric variables to zero by default anyway, so why this line is needed?
It is needed in case user entered a string containing characters other than 0 or 1 somewhere in the middle, for example "ABC" and program discovers this on line , when variables S and P already have some values different from zero. But before this is done, we need to reset values of P and S to zero.
We also can use CLEAR command for this, which is a better way to reset all variables to their default values! Here's the sample output of converting a binary to decimal:. The following program demonstrates this:. The rest of the program is very similar to one in the previous example. Recall an earlier example where we were converting a decimal number to binary form, but were using a number to hold the value? We did see that when we entered sufficiently large number, the output was not quite what was expected.
Worse that this, if we try entering even larger number as input by filling 2 lines with 9, for example , we get an overflow error. Overflow error means that computer cannot store the number we are asking it to store because it's too big. ONERR takes a line number which we direct the program to in case of error. Additionally, PEEK gets the code of the latest error code for overflow is This way we can handle multiple errors in a more graceful way:.
There are different ways to draw a circle. We start with a simple method, then move to more advanced ways to do this. This method of drawing circle works fine when X is small, but as it gets closer to R, the line appears broken. The circle is symmetrical, so in order to draw the full circle we can repeat the process for all four quarters, only changing the sign of X and Y components accordingly:. We have a complete circle. However, we are doing a lot of repeating calculations.
Because of this, we can speed up the process even more by plotting all eight points given a single pair of X and Y:. We made a few changes in our code.
First, we ask the user to enter desired radius, between 1 and Second, we added a special value R2, which will hold the square of R, so that we don't calculate this value over and over as part of calculating Y on LINE Lines This program is about six times faster than our previous version.
Below is a circle with radius of 96 created by our latest program:. This method can be easily applied to draw an ellipse. All we need to do is multiply one of the coordinates with a smaller span by a coefficient K where K must be between -1 and 1, to keep the ellipse line solid.
The following example shows an ellipse with vertical axis being semi-major smaller and horizontal axis being the major axis:. So the variable we will be looping though is not X, but angle we will call it A.
To draw the full circle, A will change from 0 to degrees. The above program plots points along the circle. The important decision about how many points to use with this method depends on the size of the circle itself. The following program demonstrates this logic, by drawing a number of concentric circles with radiuses ranging from 3 to 96, each having different number of points:.
So, which of the two methods we presented should be preferred when drawing a circle? The method using trigonometric functions looks much simpler. It also allows more control over granularity of the circle: we can control how many points along the degrees we want to use.
When circle radius is only a few pixels in size, we don't need to plot hundreds of pixels to draw the circle: it would be on overkill; at the same time when circle radius gets bigger, circle's curvature decreases and we can allow more space between individual points without losing much quality.
Which in turn means less points to calculate and draw, so it can be done comparatively quicker. To finish the subject on drawing circles, we need to look at another algorithm, which is actually used in many modern-day programs to draw a circle or part of a circle very quickly. The algorithm derives from the circle equation, but is written to avoid calling slow functions such as square root.
It is called the Midpoint circle algorithm. Just like the method using SQR , which we analyzed above, it draws the circle in octants we calculate a pair of X and Y, and then draw eight points along the circle ; the trick is to know when to move to the next Y coordinate as we move along another coordinate, X, without calling SQR or trigonometric functions.
More examples with trigonometric functions. Trigonometric functions can also be used to draw ellipses; the idea is similar to drawing ellipses using SQR function as we did before: all that we need is to multiply one of the coordinates the one with smaller axis by a coefficient K between -1 and 1.
This method was in fact used to calculate where to draw the lines in the hyperboloid picture presented earlier. They start and end along points that belong to two ellipses.
This is actually the case where other methods we used for drawing a circle don't work. Spiral is drawn similar to a circle, except the distance to its center increases proportionally to the angle we use in COS and SIN. I increase R by 0. NEXT loop using R as loop variable. What happens when you increase the step for R from 0. String and type conversion functions. The following program takes strings from the user and outputs reversed version of them:.
Let's take a closer look at this program. In all examples until now, all our programs consisted of a single code block where the last line was calling END. Subroutines are called from the main program block the can also be called from subroutines using GOSUB command, which takes their first line number. This tells the program to go to a subroutine starting on line The same subroutine can be called in many places , so we can separate a piece of program logic we use frequently in a subroutine, and call it anywhere in the program we want.
Subroutines also make the program more structured by logically separating its parts. Why include two subroutines on lines and in our example? But they do this a little differently. As you can see, in our example the subroutine starting on LINE is never called. In your programs, sometimes you want to include several subroutines which accomplish the same thing differently, to test which one works better faster, uses less memory, etc.
Another computer number format: hexadecimal numbers. Earlier in this tutorial we looked at binary numbers. They are helpful for understanding how computers store and communicate information, but typically are not used explicitly in computer programs because writing numbers in binary format is not very convenient.
Instead, another, more compact format, called hexadecimal or Hex, for short , directly related to binary format, is often used when we want to work with individual bits. The base of this format is 16, which corresponds to 4 binary bits. It means that every byte or 8 bits can be written as 2-symbol hexadecimal number. The hex format uses 16 symbols to represent 16 different values the 4 bits can represent: numbers to represent values from zero to nine, letters A,B,C,D,E,F to represent values from ten to fifteen.
The following program prints numbers 0 to 20 in decimal and hexadecimal format:. The procedure of converting a decimal number to hex format is moved to a subroutine starting at LINE The conversion algorithm is similar to the one we used for binary numbers , except instead of base 2 we use base Divide the digital number A by 16 and take the remainder of the division.
Let's call this remainder B. Possible values of B are zero to fifteen. Subtract B from A. Lets call the result of this subtraction C. Number C will always be a multiple of Divide C by 16 and assign the result to variable A.
If A equals to zero, we are finished converting the number to hex format. If A is greater than zero, go to step 1 with new value of A and write the next hexadecimal letter to the left of the letters you already written.
Sometimes we want to display text information which does not fit in one screen. A table with rows, for example, will not fit in screen with 24 rows. In this case we can split it into several "pages", letting the user to move between pages by pressing a key often different keys are utilized to move up or down the document. The following program uses "paging": it prints every next 20 numbers in decimal and hex format, refreshing the screen with new set of numbers when user presses a key.
It is using a subroutine beginning at LINE to do the decimal to hex conversion. Suggested exercise : another number format used in computers, called Octal , has base of 8. The symbols used in this format are digits zero to seven. Modify the paging program shown above to add a new subroutine, which converts decimal numbers to octal format, and print a third column showing octal numbers, as shown in the picture below:.
Converting numbers from one base format to another is a good way to practice your programming skills in BASIC. This is why I encourage you to do more: Suggested exercises : Write a program to convert Hex number to decimal. As input, you have a string containing only digits 0 to 9 and letters A to F. Use a subroutine for this and call it each time you need to convert hex string to a decimal format. You may recall that when we converted binary string to decimal , we used 2 different approaches: parsing input string from right to left, and parsing it from left to right.
You can use the same 2 methods when converting a hex number to decimal. Which of the two methods is easier to do on paper? Which one is faster? Convert hex string to binary string by calling two subroutines in a row: the first one converts hex to decimal, and second one converts the decimal we got to a binary. Is this the most efficient way of converting a string representing a hex number to binary format? Drawing Fibonacci spiral with arcs advanced. The Fibonacci numbers we looked at earlier can be also represented visually in a two-dimensional space, by drawing adjacent squares with sides equal to the subsequent Fibonacci numbers : 1,1,2,3,5,8 and so on.
The method is the following:. We draw the first square with side equal to 1. Then just above it we draw another square with side of 1; the two squares must border each other. Then we move left and draw a third square with side of 2, which borders the two squares with already have. Then we move down and draw another square with side 3, having a common border with squares 1 and 3. Fibonacci spiral is a line consisting of a sequence of arcs each being one quarter of a circle drawn within each of the Fibonacci squares.
The radius of each arc equals to the side of the corresponding square. As user presses a key, it draws a new square and next part of the Fibonacci spiral. We draw squares in purple and spiral in white. As spiral grows, eventually we will go outside of the allowed screen coordinates. E gets checked on lines and - in case it is set to 1, we clear the screen and all variables LINE 10 and start the program all over.
Until now we used variables to store individual values. What if we need to store a number of values, for example, we want to store 12 values, each containing a number of days in a given month of the year let's leave issue of number of days in February aside for now?
Do we need to declare 12 individual variables? I am Bhupendra Patidar, full-stack Java developer, and Automation engineer. I am enjoying programming since last 6 years and sharing my experiences with the community. Thank You to visit this article:. Reach me over the Skype: jcodebun, Mail: jcodebun gmail. I am trying to write a program to calculate suspension movement of my racing cars rear suspension either in qbasic or visual basic.
Can u please help. Not sure if anybody else had to do this, but to clear the style completely, I had to remove the at the end of the header. Module Module1 Sub Main Console. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Skip to content. Change Language. Related Articles. Table of Contents. Data Types. Control Flow. Python OOP. Exception Handling. File handling. Python Regex. Python Collections.
0コメント