Introduction to BASIC
Foundation of Graphics
Set up the Coordinate System
First, set up the coordinate system with a SET WINDOW statement.
The meaning of four parameters of a SET WINDOW statement is as follows.
SET WINDOW left , right , bottom , top
Example.
SET WINDOW -4,4,0,8
The range of x coordinate and y coordinate are set to be -4~4 and 0~8, respectively.
[Note]
Ordinary, the drawing pane is square.
If you want to draw correct figures, set the coordinate system so that right - left = top - bottom.
Draw the axes
DRAW AXES
draws the axes with ticks.
DRAW GRID
draws the coordinate grid.
DRAW axes(x,y)
draws the axes with tick intervals x, y.
DRAW grid(x,y)
draws the grid with intervals x, y.
Draw curves
Use PLOT LINES statements to draw curves.
The principle of drawing a curve resembles that of a XY-plotter.
Lines or curves are drawn by a pen.
When the pen moves if it is down, a line or curve is drawn.
When the pen moves if it is up, none are drawn.
PLOT LINES: x, y
moves to the point (x, y) and makes the pen up.
PLOT LINES: x,y;
moves to the point (x, y) and makes the pen down.
PLOT LINES
makes the pen up.
The initial pen state is up.
Example.
(1) A program which draws a graph of a function.
10 DEF f(x)=x^3+2*x+1 20 SET WINDOW -5,5,-5,5 30 DRAW grid 40 FOR x=-5 TO 5 STEP 0.01 50 PLOT LINES: x,f(x); 60 NEXT x 70 END
(2) A program which draws two or more curves.
10 SET WINDOW -5,5,-5,5 20 FOR x=-5 TO 5 STEP 0.01 30 PLOT LINES: x, sin(x); 40 NEXT x 50 PLOT LINES 60 FOR x=-5 TO 5 STEP 0.01 70 PLOT LINES: x,cos(x); 80 NEXT x 90 END
The PLOT LINES in line 50 is necessary for disconnecting the curves.
(3) A curve of parametric equation.
10 option angle degrees 20 SET WINDOW -4,4,-4,4 30 DRAW axes 40 FOR t=0 TO 360 50 PLOT LINES: 3*cos(t),2*sin(t); 60 NEXT t 70 END
(4) A curve defined in the polar coordinate.
10 SET WINDOW -2,2,-2,2 20 DRAW axes 30 FOR t=0 TO 2*pi STEP pi/180 40 LET r=1+cos(t) 50 PLOT LINES: r*cos(t),r*sin(t); 60 NEXT t 70 END
[Supplement]
To convert the rectangular coordinates to the polar coordinates, execute
LET r=SQR(x^2+y^2) LET t=ANGLE(x,y)