Q(uick)BASIC Statement (Graphics): LINE

Quick View

LINE

A graphics statement that draws a line or box on the screen

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • LINE [[STEP](x1!,y1!)]-[STEP](x2!,y2!) [,[color%] [,[B | BF] [,style%]]]
Description/Parameter(s)
STEP Specifies that coordinates are relative to the current graphics cursor position.
(x1!,y1!),(x2!,y2!) The screen coordinates of the start of the line and of the end of the line.
color% A color attribute that sets the color of the line or rectangle. The available color attributes depend on your graphics adapter and the screen mode set by the most recent SCREEN statement.
B Draws a rectangle instead of a line.
BF Draws a filled box.
style% A 16-bit value whose bits set whether or not pixels are drawn. Use to draw dashed or dotted lines.
Example

This example requires a color graphics adapter.

SCREEN 1 LINE (110, 70)-(190, 120), , B LINE (0, 0)-(320, 200), 3, , &HFF00
Syntax
  • LINE [[STEP] (x1,y1)]-[STEP] (x2,y2) [,[color][,[B[F]][,style]]]
Description/Parameter(s)

The coordinates (x1,y1) and (x2,y2) specify the endpoints of the line; note that the order in which these endpoints appear is unimportant, since a line from (10,20) to (120,130) is the same as a line from (120,130) to (10,20).

The STEP option makes the specified coordinates relative to the most recent point, instead of absolute, mapped coordinates. For example, if the most recent point referred to by the program is (10,10), then

  • LINE -STEP (10,5)

draws a line from (10,10) to the point with x coordinate equal to 10 + 10 and y coordinate equal to 10 + 5, or (20,15).

You may establish a new most recent point by initializing the screen with the CLS and SCREEN statements. Using the PSET, PRESET, CIRCLE, and DRAW statements will also establish a new most recent point.

Variations of the STEP argument are shown below. For the following examples, assume that the last point plotted was (10,10):

Statement Description
LINE -(50,50) Draws from (10,10) to (50,50)
LINE -STEP(50,50) Draws from (10,10) to (60,60); that is, to 10 plus offset 50
LINE (25,25)-STEP(50,50) Draws from (25,25) to (75,75); that is, to 25 plus offset 50
LINE STEP(25,25)-STEP(50,50) Draws from (35,35) to (85,85); that is, from 10 plus offset 25 to that point plus offset 50
LINE STEP(25,25)-(50,50) Draws from (35,35) to (50,50); that is, from 10 plus offset 25 to absolute 50

The color is the number of the color in which the line is drawn. (If the B or BF options are used, the box is drawn in this color.) See the COLOR statement details and the SCREEN statement details for information about how to specify a color number in different screen modes.

The B option draws a box with the points (x1,y1) and (x2,y2) specifying diagonally opposite corners.

The BF option draws a filled box. This option is similar to the B option; BF also paints the interior of the box with the selected color.

The style is a 16-bit integer mask used to put pixels on the screen. Using the style argument is called "line styling." With line styling, LINE reads the bits in style from left to right. If a bit is 0, then no point is plotted; if the bit is 1, a point is plotted. After plotting a point, LINE selects the next bit position in style.

Because a 0 bit in style does not change the point on the screen, you may want to draw a background line before using styling so you can have a known background. Style is used for normal lines and boxes, but has no effect on filled boxes.

When coordinates specify a point that is not in the current viewport, the line segment is clipped to the viewport.

Example

This example uses LINE statements to display a series of screens with different line graphics. To run this example, your screen must be 320 pixels wide by 200 pixels high and support CGA screen mode.

SCREEN 1 'Sets up the screen mode. LINE -(X2, Y2) 'Draws a line (in the 'foreground color) from 'the most recent point 'to X2,Y2. DO LOOP WHILE INKEY$ = "" 'Press any key to continue CLS ' Clear screen LINE(0, 0)-(319, 199) 'Draws a diagonal line across 'the screen (downward). DO LOOP WHILE INKEY$ = "" 'Press any key to continue CLS ' Clear screen LINE(0, 100)-(319, 100) 'Draws a horizontal line 'across the screen. DO LOOP WHILE INKEY$ = "" 'Press any key to continue CLS ' Clear screen LINE(10, 10)-(20, 20), 2 'Draws a line in color 2. DO LOOP WHILE INKEY$ = "" 'Press any key to continue CLS ' Clear screen FOR X = 0 to 319 'Draws an alternating pattern LINE(X, 0)-(X, 199), X AND 1 '(line on/line off) on mono- NEXT 'chrome display. DO LOOP WHILE INKEY$ = "" 'Press any key to continue CLS ' Clear screen LINE (0, 0)-(100, 100),, B 'Draws a box in the fore- 'ground color (note that the 'color is not included). DO LOOP WHILE INKEY$ = "" 'Press any key to continue CLS ' Clear screen LINE STEP(0,0)-STEP(200,200),2,BF 'Draws a filled box in color '2 (coordinates are given as 'offsets with the STEP option). DO LOOP WHILE INKEY$ = "" 'Press any key to continue CLS ' Clear screen LINE(0,0)-(160,100),3,,&HFF00 'Draws a dashed line from 'the upper lefthand corner to 'the center of the screen in 'color 3.
Syntax
  • LINE [[STEP](x1,y1)] - [STEP](x2,y2) [,[color] [,[B[F]] [,style]]]
Description/Parameter(s)
  • The argument color is the number of the color in which the line is drawn. See the COLOR Statement Details and the SCREEN Statement Details for information about how to specify a color number in different screen modes.
  • The argument B draws a box with the points (x1,y1) and (x2,y2) as its diagonally opposite corners.
  • The argument BF draws a filled box in the selected color.
  • The argument style is a 16-bit integer mask used to put pixels on the screen.

Usage Notes

  • When coordinates specify a point that is not within the current viewport, the line segment to that point is drawn to the border of the viewpoint.
  • LINE reads the bits in style from left to right. If a bit is 0, then no point is plotted; if the bit is 1, a point is plotted. After plotting a point, LINE selects the next bit position in style.
  • Using the style argument is called "line styling."
  • Because a zero bit in style does not change the point on the screen, you may want to draw a background line before using styling so you can have a known background.
  • Style is used for normal lines and boxes, but has no effect on filled boxes.
  • STEP makes the specified coordinates relative to the most-recent point. For example, if the most-recent point referred to by the program were (10,10), the following would draw a line from (10,10) to the point with an x coordinate equal to 10 + 10 and a y coordinate equal to 10 + 5, or (20,15):

  • LINE -STEP(10, 5)

  • You can establish a new most-recent point by initializing the screen with the CLS and SCREEN statements. Using other graphics statements, (for example, PSET, PRESET, CIRCLE, or DRAW) will also establish a new most-recent point.
  • Variations of the STEP argument are shown below. For the following examples, assume that the last point plotted was (10,10):
Statement Description
LINE -(50,50) Draws from (10,10) to (50,50)
LINE -STEP(50,50) Draws from (10,10) to (60,60)
LINE (25,25)-STEP(50,50) Draws from (25,25) to (75,75)
LINE STEP(25,25)-STEP(50,50) Draws from (35,35) to (85,85)
LINE STEP(25,25)-(50,50) Draws from (35,35) to (50,50)
Example

This example uses the LINE statement to display a series of screens with different line graphics.
Note: To run this example, your screen must be 320 pixels wide by 200 pixels high and support CGA screen mode.

SCREEN 1 'Sets up the screen mode. LINE -(X2, Y2) 'Draws a line (in the foreground color) from 'the most recent point to X2,Y2. DO LOOP WHILE INKEY$ = "" 'Press any key to continue. CLS 'Clear screen. LINE (0, 0)-(319, 199) 'Draws a diagonal line across the screen. DO LOOP WHILE INKEY$ = "" 'Press any key to continue. CLS 'Clear screen. LINE (0, 100)-(319, 100) 'Draws a horizontal line across the screen. DO LOOP WHILE INKEY$ = "" 'Press any key to continue. CLS 'Clear screen. LINE (10, 10)-(20, 20), 2 'Draws a line in color 2. DO LOOP WHILE INKEY$ = "" 'Press any key to continue. CLS 'Clear screen. FOR X = 0 TO 319 'Draws an alternating pattern (line on/line off). LINE (X, 0)-(X, 199), X AND 1 NEXT DO LOOP WHILE INKEY$ = "" 'Press any key to continue. CLS 'Clear screen. LINE (0, 0)-(100, 100), , B 'Draws a box in the foreground color. DO LOOP WHILE INKEY$ = "" 'Press any key to continue. CLS 'Clear screen. LINE STEP(0, 0)-STEP(200, 200), 2, BF'Draws a filled box in color 2 '(coordinates are given as 'offsets with the STEP option). DO LOOP WHILE INKEY$ = "" 'Press any key to continue. CLS 'Clear screen. LINE (0, 0)-(160, 100), 3, , &HFF00'Draws a dashed line from the upper 'lefthand corner to the center of 'the screen in color 3.