Friday, 17 January 2014

Graphics in BASIC

BASIC Programming

On micro computers it is possible to produce graphics output in addition to printing letters, numbers and other usual characters. The computers can draw fine dots called pixels on the screen at any location you specify.

14.1 Screen Function

The screen function can be set to either of the following modes.
·         Text mode                                          (Screen 0)
·         Medium resolution mode            (Screen 1)
·         High resolution mode                    (Screen 2)
The format of the Screen statement is
Line # Screen mode
Text Mode:- in the Text mode the characters are displayed on the screen in 40 columns and 25 rows (40×25) or 80 columns and 25 rows (80×25) depending upon the selection of screen width.
Medium Resolution Mode:- in the Medium resolution mode the screen in divided into 320 columns and 200 rows. The individual point on screen is referenced by specifying the column(0 to 319) and row(0 to 199).
High Resolution Mode:- in high resolution mode the screen in divided into 640×200 points.

14.2 Color Statement:- 

the format of the Color statement depends on the screen mode. Color will not function in high resolution. In the text mode resolution the format of color statement is;
Line # Color [foreground], [background]
The foreground color number must be an integer from 0 to 31. The color numbers from 16 to 31 are blinking versions of the colors from 0 to 15.
The background color must be an integer 0 to 7.
In the medium resolution mode Screen 1, the color statement has the following format;
Line # Color [background], [palette]
16 background colors are available having code from 0 to 15.after selecting a background color, select a palette( a palette is the range of colors used by an artist in painting). The palettes are numbered 0 and 1 each with four colors.
Color
Code
Color
Code
 Black
0
Gray
8
Blue
1
Light blue
9
Green
2
Light green
10
Cyan
3
Light cyan
11
Red
4
Light red
12
Magenta
5
Light magenta
13
Brown
6
Yellow
14
white
7
Bright white
15

Color
Code
Palette 0
Background
0
Green
1
Red
2
Brown
3
Palette 1
Background
0
Cyan
1
Magenta
2
White
3

14.3 Medium Resolution Mode:-

 Pset statement is used to specify the coordinates X, Y of the point where it appears on the screen.
Program
10 Rem *Red vertical line of 3 pixels*
20 Screen 1,0
30 Color 1,0
40 Pset (40, 10), 2
50 Pset (40, 11), 2
60 Pset (40, 12), 2
70 Rem * white horizontal line of 3 pixels*
80 Color 1,1
90 Pset (41, 14), 3
100 Pset (42, 14), 3
110 Pset (43, 14), 3
120 end

14.4 High Resolution Mode:-

 High resolution mode allows to draw more fine shapes than those possible on medium resolution mode.
The Pset and Line statements are used to draw points and lines in high resolution graphics. These statements have the same form as in medium resolution except that the number 1 at the end of either statement indicates a white point or line and the number 0 indicates a black point or line.
Program
10 Rem * program to draw a plus shape symbol*
20 screen 2
30 Line (100, 100) – (300, 100), 1
40 Line (200, 50) – (200, 150), 1
50 end
Rectangles or square box can be drawn by adding B using Line statement with points of upper left and lower right corners of the square or rectangle.
10 Rem * Red square box inside a green box*
20 screen 1
30 Line (50,50) – (110,110), 1, B
40 Line (55,55) – (105,105), 2, B
50 End

14.5 Draw Statement:- 

Draw statement is used to draw lines and other shapes. It has the following format;
Line# Draw string
A string consists of single character command followed by a prefix that controls the size, direction etc. of the line and is enclosed in the quotation marks for example :-
50 Draw “E30”
The drawing commands that may be used are given below with the digit “n” as the prefix to describe the number of points moved by command.
Command           Purpose
↑           Un          Upward move through n points.
↓           Dn          Downward move through n points.
→           Rn           Rightward move through n points.
←           Ln           Leftward move through n points.
↗            En           Diagonally Right Upward move through n points.
↘            Fn           Diagonally Right Downward move through n points.
↙            Gn          Diagonally Left Downward move through n points.
↖            Hn          Diagonally Left Upward move through n points.
Program
10 Rem *Rectangle*
20 Screen 1
30 Draw “U80 R80 D80 L80”
40 End

14.6 Circles and Ellipses:- 

in BASIC there is a build-in command that will draw circles, part of a circle or ellipse. In medium graphic resolution it has the following format;
Circle (X,Y), R, C, S, E, A
It will draw a circle of radius R with center at (X, Y) in the color C. S is the start arc position, E is the end arc position and A is the aspect ratio (height/width).
Program
10 Rem *Brown upper and Green lower semi circles*
20 Screen 1, 0
30 Color 7, 0
40 Circle(50, 100), 30, 3, 0, 3.14

50 Circle(120, 100), 30, 1, 3.14
Read More

Monday, 13 January 2014

Sub-Programs and File Handling in BASIC

 BASIC Programming

 

 

 

1. Sub-programs

A sub-program is a group of statements that perform a single operation on execution. They are easy to design, debug, modify and document. Sub-programs are written once in a program but can be used several times in that program
There are two types of sub-programs
1.       Functions
2.       Subroutines

2. functions

functions perform arithmetic and string operations. They make the programming much simple and reduce the size of program and also save time.
A function is a routine that takes a value as input and produce a value as output.
There are two types of functions;

2.1 system defined/ built in functions

these functions are also called library functions, they are included in the BASIC. They are used in programs. Functions are divided into two categories;
1.       Numeric functions
2.       String functions

2.1.1 Numeric Functions

ABS function:- this function provides the absolute value of the expression x(the value without any sign). Its format is ABS(x)
Program
10 input “enter a number”, x
20 print “the absolute value is “ABS(x)
30 end
Output of the program
enter a number -5
the absolute value is 5
CINT function:- this function converts the numeric expression into integer by rounding the fractional part of the program. Its format is CINT(x)
Program
10 input “enter a number”, x
20 print “the integer converted value is “,CINT(x)
30 end
Output of the program
enter a number 5.9
the integer converted value is 6
INT function:- this function prints the positive whole numbers as same regardless of their value and negative whole numbers as rounded down to the next smaller value its format is INT(x).
Program
10 input “enter a number”, x
20 print “the integer value is “,INT(x)
30 end
Output of the program
enter a number 5.9
the integer value is 5
RND function:- this function is also referred as Random Number Generator. It generates a number between 0 and 1. Its format is RND(x).
SGN function:- this function is referred to as Sign function. Which reports the sign of a number which can be equal to -1 (when the value of x is negative),1 (when the value of x is negative)and 0 when x is 0. Its format is SGN(x)
Program
10 print “the value of -5 after using sgn function is”, INT(-5)
20 print “the value of -5 after using sgn function is”, INT(5)
30 print “the value of -5 after using sgn function is”, INT(0)
Output of the program
the value of -5 after using sgn function is -1
the value of -5 after using sgn function is 1
the value of -5 after using sgn function is 0
FIX function:- FIX function is used to obtain an integer value by simply dropping off the decimal point and numbers to the right of it. Its format is FIX(x).
Program
10 input “enter a number” , x
20  print “the integer value is”,FIX(x)
30 end
Output
Enter a number 5.67
the integer value is 5
SQR function:- this function finds the square root of any positive number. Its format is SQR(x)
Program
10 input “enter a positive number to get its square root”, x
20 print “ the square root is”, SQR(x)
30 end
Output
Enter a positive number to get its square root 9
The square root is 3
LOG function:- this function is used to find the value of natural logarithm. Its format is LOG(x).
Program
10 input “type a number”, x
20 print “natural log is”, LOG(x)
30 end
Output
Type a number 10
Natural log is 2.302585
SIN function:- the purpose of SIN function is to find the trigonometric ration called sine of an angle x expressed in radians. Its format is SIN(x)
Program
10 input “type the angle in radians”, x
20 print “radians =”,SIN(x)
30 end
Output
Type the angle in radians 2
Radians=
COS function:- the purpose of COS function is to find the trigonometric ration called cosine of an angle x expressed in radians. The format is COS(x).
Program
10 print COS(1)
20 end
Output
0.540302
TAN function:- the purpose of TAN function is to find the trigonometric ratio called tangent of an angle x expressed in radians. The format is TAN(x).
Program
10 print TAN(4)
20 end
Output
1.157821
13.5 String Functions
HEX$ function:- this function calculates hexadecimal number of given decimal number x. its format is HEX$(x)
Program
10 print HEX$(20)
20 end
Output
14
LEFT$ function:- this function is used to select n left most characters of a given string. Its format is LEFT$($,n)
Program
10 print LEFT$(“Balochistan”,6)
20 end
Output
Baloch
RIGHT$ function:- this function is used to select n right most characters of a given string. Its format is RIGHT$($,n).
Program
10 print RIGHT$(“Baloch Khan”,4)
20 end
Output
Khan
INKEY$ function:- this function responds to any key from the keyboard without pressing the enter key.
Program
10 print “press any key to continue”
20 let A$=INKEY$
30 if A$=”” then goto 10
40 print A$
50 end
Output
Press any key to continue
F
F
SPACE$ function:- the purpose of SPACE$ function is to leave certain number of spaces before a character. Its format is SPACE$(n) or SPC(n)
Program
10 print “Name”, SPACE$(21),”Kit No”
20 end
Output
Name                    Kit No

2.2 User Defined Functions

besides providing the built in functions, BASIC has the statement DEF FN that lets you define your own functions. These are called user defined functions.
Program
10 DEF FNSUM(x,y)=x+y
20 print FNSUM(5,8)
30 end
Output
13

3.Subroutine Subprogram

in programming often large programs are divided into some short programs called subroutines. In BASIC a subroutine is a series of statements that is written once in a program but it may be used a number of times in the program.

GOSUB / RETURN Statement

the GOSUB statement will cause the transfer of control to the subroutines. Its format is Line# GOSUB #.The RETURN statement passes control back to the statement immediately following the SOSUB instruction that has called the SUBROUTINE.
Program
10 print “this is a subroutine program”
20 GOSUB 50
30 print “this print message is written before subroutine but displays after the subroutine”
40 end
50 a=10,b=20,c=a+b
60 print c
70 RETURN
Output
this is a subroutine program
30
This print message is written before subroutine but displays after the subroutine

4.File Handling

a computer can process large amounts of data. It is always useful to store input and output in a data file. A file consists of records stored on a secondary storage.

4.1 Open and Close statements

the Open and Close statements are used for opening file for processing and Close it after necessary processing.
Read More