Q(uick)BASIC Function: PMAP
Quick View
PMAP
A graphics function that maps view-coordinate expressions to physical locations or maps physical expressions to a view-coordinate location
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- PMAP (startcoordinate#, n%)
Description/Parameter(s)
startcoordinate# | A window or viewport coordinate. | ||
n% | A value indicating which coordinate is returned: | ||
startcoordinate# |
n% |
Returns |
|
Window x coordinate | 0 | Viewport x coordinate | |
Window y coordinate | 1 | Viewport y coordinate | |
Viewport x coordinate | 2 | Window x coordinate | |
Viewport y coordinate | 3 | Window y coordinate |
Example
'This example requires a graphics adapter that supports screen mode 1.
SCREEN 1
WINDOW SCREEN (0, 0)-(100, 100)
PRINT "Logical x=50, physical x="; PMAP(50, 0)
PRINT "Logical y=50, physical y="; PMAP(50, 1)
See also:
Syntax
- PMAP (expression, function)
Description/Parameter(s)
The argument expression indicates the coordinate of the point to be mapped. The argument function can have one of the four following values:
Value | Description |
0 | Maps view-coordinate expression to physical x coordinate |
1 | Maps view-coordinate expression to physical y coordinate |
2 | Maps physical expression to view x coordinate |
3 | Maps physical expression to view y coordinate |
The four PMAP functions allow the user to find equivalent point locations between the view coordinates created with the WINDOW statement and the physical coordinate system of the screen or viewport as defined by the VIEW statement.
Example
This example uses PMAP to convert coordinate values from view to screen coordinates and from screen coordinates to view coordinates.
Note: This is not a complete program. Do not run this example in its current form.
SCREEN 2
'Coordinates of upper-left corner of window defined in following
'statement are (80,100); coordinates of lower-right corner
'are 200,200.
WINDOW SCREEN (80,100) - (200,200)
'If physical screen coordinates are (0,0) in the upper-left
'corner and (639,199) in the lower-right corner, then the
'following statements will return the screen coordinates
'equivalent to the view coordinates 80,100.
X = PMAP(80,0) 'X = 0
Y = PMAP(100,1) 'Y = 0
'The following statements will return the screen coordinates
'equivalent to the view coordinates 200,200.
X = PMAP(200,0) 'X = 639
Y = PMAP(200,1) 'Y = 199
'The following statements will return the view coordinates
'equivalent to the screen coordinates 639,199.
X = PMAP(639,2) 'X = 200
Y = PMAP(199,3) 'Y = 200
See also:
Syntax
- PMAP (expression#, n%)
Description/Parameter(s)
expression# | Specifies the known x or y coordinate in the window or viewport. | ||
n% | Specifies the mapping function; an integer expression with a value between 0 and 3: | ||
Value |
expression |
Returns |
|
0 | Window x coordinate | Viewport x coordinate | |
1 | Window y coordinate | Viewport y coordinate | |
2 | Viewport x coordinate | Window x coordinate | |
3 | Viewport y coordinate | Window y coordinate |
PMAP returns the window coordinate equivalent to a specified viewport coordinate, or the viewport coordinate equivalent to a specified window coordinate.
Usage Notes
- The four PMAP functions allow you to find equivalent point locations between the window coordinates created with the WINDOW statement and the absolute screen coordinates or viewport coordinates defined by the VIEW statement.
- If no VIEW statement has been executed, or the most recently executed VIEW statement has no arguments, the viewport coordinates are equivalent to the absolute screen coordinates established by the most recently executed SCREEN statement.
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
See also: