Q(uick)BASIC Statement: WINDOW

Quick View

WINDOW

A graphics statement that defines the dimensions of the current viewport

Worth knowing

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

Syntax
  • WINDOW [[SCREEN] (x1!,y1!)-(x2!,y2!)]
Description/Parameter(s)
SCREEN Inverts the normal Cartesian direction of the y screen coordinates so that y values increase from the top of the screen to the bottom.
(x1!,y1!) Logical coordinates that map to the upper-left screen coordinates of the viewport.
(x2!,y2!) Logical coordinates that map to the lower-right screen coordinates of the viewport.
WINDOW with no arguments disables the logical coordinate system.
Use the VIEW statement to change the size of the viewport.
Example
'This example requires a color graphics adapter. SCREEN 1 FOR i% = 1 TO 10 STEP 2 WINDOW (-160 / i%, -100 / i%)-(160 / i%, 100 / i%) CIRCLE (0, 0), 10 NEXT i%
Syntax
  • WINDOW [[SCREEN] (x1,y1)-(x2,y2)]
Description/Parameter(s)

The WINDOW statement allows the user to create a customized coordinate system to draw lines, graphs, or objects without being constrained by the screen's physical coordinates (the dimensions of the screen). This is done by redefining the screen-border coordinates with the "view coordinates" (x1, y1) and (x2, y2). These view coordinates are single-precision numbers.

WINDOW defines the section of the view coordinate system that is mapped to the physical coordinates of the screen. All subsequent graphics statements use these new view coordinates and are displayed within the current viewport. (The size of the viewport can be changed with the VIEW statement.)

The RUN statement, or WINDOW with no arguments, disables the window transformation.

The WINDOW SCREEN variant inverts the normal Cartesian direction of the y coordinate, so y values go from negative to positive from top to bottom.

Example

WINDO_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the WINDOW statement. To look at the program in the View window and, optionally, to run it, load it using the File menu's Open Program command.
The program shows how changing the window size changes the size of a figure drawn on the screen. The effect is one of zooming in and out; as the window gets smaller, the figure appears larger on the screen, until parts of it are finally clipped because they lie outside the window. As the window gets larger, the figure appears smaller on the screen.

Syntax
  • WINDOW [[SCREEN] (x1!,y1!)-(x2!,y2!)]
Description/Parameter(s)
If all arguments are omitted, the window coordinates will be the same as the viewport coordinates.
SCREEN If omitted, the origin of the window y-axis is at the lower-left corner of the window (as in the Cartesian coordinate system); if used, the origin is at the upper-left corner.
(x1!,y1!)-(x2!,y2!) The coordinates y1!, y2!, x1!, and x2! are single-precision expressions specifying the top, bottom, left, and right sides of the window, respectively.

Usage Notes

  • The WINDOW statement allows the user to create a customized coordinate system to draw lines, graphs, or objects without being constrained by the graphics viewport physical coordinate values and orientation.
  • WINDOW defines a coordinate system that is mapped to the physical coordinates of the viewport. All subsequent graphics statements use the window coordinates and are displayed within the current viewport. The viewport may be the entire screen or a clipping area defined in physical coordinates by the VIEW statement.
  • A WINDOW statement with no arguments, or a RUN statement, disables the window coordinate system.
Example

This example uses the VIEW and WINDOW statements to define a graphics viewport and window. The PMAP function is used to convert viewport coordinates to window coordinates. The program generates a fractal that shows a subset of the complex numbers called the "Mandelbrot Set."

DEFINT A-Z 'Default variable type is integer. DECLARE SUB ScreenTest (EM%, CR%, VL%, VR%, VT%, VB%) 'Set maximum number of iterations per point. CONST MAXLOOP = 30, MAXSIZE = 1000000 CONST FALSE = 0, TRUE = NOT FALSE 'Boolean constants. 'Set window paramters. CONST WLeft = -1000, WRight = 250, WTop = 625, WBottom = -625 'Call ScreenTest to find out if this is an EGA machine, 'and get coordinates of viewport corners. ScreenTest EgaMode, ColorRange, VLeft, VRight, VTop, VBottom 'Define viewport and corresponding window. VIEW (VLeft, VTop)-(VRight, VBottom), 0, ColorRange WINDOW (WLeft, WTop)-(WRight, WBottom) LOCATE 24, 10: PRINT "Press any key to quit."; XLength = VRight - VLeft YLength = VBottom - VTop ColorWidth = MAXLOOP \ ColorRange 'Loop through each pixel in viewport and calculate whether or not 'it is in the Mandelbrot Set. FOR Y = 0 TO YLength 'Loop through every line in the viewport. LogicY = PMAP(Y, 3) 'Get the pixel's logical y coordinate. PSET (WLeft, LogicY) 'Plot leftmost pixel in the line. OldColor = 0 'Start with background color. FOR X = 0 TO XLength 'Loop through every pixel in the line. LogicX = PMAP(X, 2) 'Get the pixel's logical x coordinate. MandelX& = LogicX MandelY& = LogicY 'Do the calculations to see if this point is in the Mandelbrot Set. FOR I = 1 TO MAXLOOP RealNum& = MandelX& * MandelX& ImagNum& = MandelY& * MandelY& IF (RealNum& + ImagNum&) >= MAXSIZE THEN EXIT FOR MandelY& = (MandelX& * MandelY&) \ 250 + LogicY MandelX& = (RealNum& - ImagNum&) \ 500 + LogicX NEXT I 'Assign a color to the point. PColor = I \ ColorWidth 'If color has changed, draw a line from the last point 'referenced to the new point, using the old color. IF PColor <> OldColor THEN LINE -(LogicX, LogicY), (ColorRange - OldColor) OldColor = PColor END IF IF INKEY$ <> "" THEN END NEXT X 'Draw the last line segment to the right edge of the viewport. LINE -(LogicX, LogicY), (ColorRange - OldColor) NEXT Y DO LOOP WHILE INKEY$ = "" SCREEN 0, 0 'Restore the screen to text mode, WIDTH 80 '80 columns. END BadScreen: 'Error handler that is invoked if EgaMode = FALSE 'there is no EGA graphics card. RESUME NEXT 'This procedure tests to see if user has EGA hardware with SCREEN 8. 'If this causes an error, the EM flag is set to FALSE, and the screen 'is set with SCREEN 1. The procedure also sets values for the corners 'of the viewport (VL = left, VR = right, VT = top, VB = bottom), 'scaled with the correct aspect ratio so viewport is a perfect square. SUB ScreenTest (EM, CR, VL, VR, VT, VB) STATIC EM = TRUE ON ERROR GOTO BadScreen SCREEN 8, 1 ON ERROR GOTO 0 IF EM THEN 'No error, so SCREEN 8 is OK. VL = 110: VR = 529 VT = 5: VB = 179 CR = 15 '16 colors (0 - 15). ELSE 'Error, so use SCREEN 1. SCREEN 1, 1 VL = 55: VR = 264 VT = 5: VB = 179 CR = 3 '4 colors (0 - 3). END IF END SUB