_setwindow

Description

Defines a graphics window coordinate system.

#include <graph.h>

short __far _setwindow( short finvert, double wx1, double wy1, double wx2,
double wy2 );

finvert Invert flag  
wx1, wy1 Upper-left corner of window  
wx2, wy2 Lower-right corner of window  

Remarks

The _setwindow function defines a window viewport. The arguments (wx1, wy1) specify the upper-left corner of the window, and the arguments (wx2, wy2) specify the lower-right corner of the window.

The finvert argument specifies the direction of the coordinates. If finvert is TRUE, the y axis increases from the screen bottom to the screen top (Cartesian coordinates). If finvert is FALSE, the y axis increases from the screen top to the screen bottom (screen coordinates).

Any window transformation done with the _setwindow function applies only to the viewport and not to the entire screen.

If wx1 equals wx2 or wy1 equals wy2, the function will fail.

Note that this function only affects output functions suffixed with _w or _wxy.

Return Value

The function returns a nonzero value if successful. If the function fails (e.g., if it is not in a graphics mode), it returns 0.

Compatibility

Standards:None

16-Bit:DOS

32-Bit:None

See Also

_arc functions, _ellipse functions, _getwindowcoord, _lineto functions, _pie functions, _setviewport, functions suffixed with _w or _wxy

Example

/* SWINDOW.C: This program illustrates translation between window,

* view, and physical coordinates. Functions used include:

* _setwindow _getwindowcoord

* _getphyscoord _getviewcoord_wxy

*/

#include <conio.h>

#include <stdlib.h>

#include <graph.h>

enum boolean { FALSE, TRUE };

enum display { MOVE, DRAW, ERASE };

void main( void )

{

struct _xycoord view, phys;

struct _wxycoord oldwin, newwin;

struct _videoconfig vc;

double xunit, yunit, xinc, yinc;

short color, key, fintersect = FALSE, fdisplay = TRUE;

/* Find a valid graphics mode. */

if( !_setvideomode( _MAXRESMODE ) )

exit( 1 );

_getvideoconfig( &vc );

/* Set a window using real numbers. */

_setwindow( FALSE, -125.0, -100.0, 125.0, 100.0 );

/* Calculate the size of one pixel in window coordinates.

* Then get the current window coordinates and color.

*/

oldwin = _getwindowcoord( 1, 1 );

newwin = _getwindowcoord( 2, 2 );

xunit = xinc = newwin.wx - oldwin.wx;

yunit = yinc = newwin.wy - oldwin.wy;

newwin = oldwin = _getcurrentposition_w();

color = _getcolor();

while( 1 )

{

/* Set flag according to whether current pixel is on, then

* turn pixel on.

*/

if( _getpixel_w( oldwin.wx, oldwin.wy ) == color )

fintersect = TRUE;

else

fintersect = FALSE;

_setcolor( color );

_setpixel_w( oldwin.wx, oldwin.wy );

/* Get and test key. */

key = _getch();

switch( key )

{

case 27: /* ESC Quit */

_setvideomode( _DEFAULTMODE );

exit( 0 );

case 32: /* SPACE Move no color */

fdisplay = MOVE;

continue;

case 0: /* Extended code - get next */

key = _getch();

switch( key )

{

case 72: /* UP -y */

newwin.wy -= yinc;

break;

case 77: /* RIGHT +x */

newwin.wx += xinc;

break;

case 80: /* DOWN +y */

newwin.wy += yinc;

break;

case 75: /* LEFT -x */

newwin.wx -= xinc;

break;

case 82: /* INS Draw white */

fdisplay = DRAW;

continue;

case 83: /* DEL Draw black */

fdisplay = ERASE;

continue;

}

break;

}

/* Translate window coordinates to view, view to physical.

* Then check physical to make sure we're on screen. Update screen

* and position if we are. Ignore if not.

*/

view = _getviewcoord_wxy( &newwin );

phys = _getphyscoord( view.xcoord, view.ycoord );

if( (phys.xcoord >= 0) && (phys.xcoord < vc.numxpixels) &&

(phys.ycoord >= 0) && (phys.ycoord < vc.numypixels) )

{

/* If display on, draw to new position, else move to new. */

if( fdisplay != MOVE )

{

if( fdisplay == ERASE )

_setcolor( 0 );

_lineto_w( newwin.wx, newwin.wy );

}

else

{

_setcolor( 0 );

_moveto_w( newwin.wx, newwin.wy );

/* If there was no intersect, erase old pixel. */

if( !fintersect )

_setpixel_w( oldwin.wx, oldwin.wy );

}

oldwin = newwin;

}

else

newwin = oldwin;

}

exit( 0 );

}