home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ddjmag
/
ddj8605.arc
/
GRAPHCS.MAY
< prev
next >
Wrap
Text File
|
1986-05-31
|
8KB
|
271 lines
/*
* SINE.C
*
* Plot sin(x) vs. x on an enhanced color display with
* an IBM enhanced graphics adapter in the high resolution mode.
* Purpose is to test the video routines.
*
* by Nabajyoti Barkakati, Silver Spring, MD 20904.
*
*/
#include <stdio.h>
#include <math.h>
#define BLUE 1 /* Color number 1 is BLUE */
#define RED 4 /* Color number 4 is RED */
#define BOTTOM 4 /* Bottom margin */
#define TOP 345 /* Top margin */
#define LEFT 4 /* Left margin */
#define RIGHT 635 /* Right margin */
#define TWOPI 6.283 /* Approximate value of 2 Pi */
#define MAXPNT 100 /* Points on the sinusoid */
main()
{
int i, x, y, oldx, oldy, midpoint;
double xd, yd, ampl;
v_init (RED); /* Initialize the display */
midpoint = (TOP - BOTTOM)/2;
ampl = (double)midpoint - (double)BOTTOM;
oldx = LEFT;
oldy = midpoint;
for (i=0; i<=MAXPNT; i++)
{
yd = ampl * sin(TWOPI * ((double)i)/((double)MAXPNT));
xd = ((double)RIGHT - (double)LEFT)* (double)i / (double)MAXPNT;
x = LEFT + (int)xd;
y = midpoint + (int)yd;
/* Draw a line from the old point to the new point */
v_draw (oldx, oldy, x, y, BLUE);
/* Save the new coordinates */
oldx = x;
oldy = y;
}
/* Draw a box around the plot */
v_draw (LEFT, BOTTOM, RIGHT, BOTTOM, BLUE);
v_draw (RIGHT, BOTTOM, RIGHT, TOP, BLUE);
v_draw (RIGHT, TOP, LEFT, TOP, BLUE);
v_draw (LEFT, TOP, LEFT, BOTTOM, BLUE);
/* Done */
}
/*
* VIDEO.C
*
* This file contains the video display modules. Uses the int86
* function of Lattice C 2.14 to draw graphs on an enhanced color
* display with the IBM enhanced graphics adapter.
*
* by N. Barkakati, Silver Spring, MD 20904.
*
*/
#include <dos.h>
#define void int
#define EGAMODE 16 /* EGA in high resolution */
#define MAXROW 24
#define MAXCOL 79
#define MAXYDOT 349 /* Max. columns and rows on */
#define MAXXDOT 639 /* enhanced color display */
#define BIOS_VIDEO 16 /* BIOS Video service int. no. */
#define SETMODE 0 /* Service: set video mode */
#define SETCOLOR 11 /* Service: set color pallette */
#define WRITE_PIX 12 /* Service: write pixel */
static union REGS xr,yr; /* See dos.h for explanation */
/* v _ i n i t
*
* Initialize the display. Put it in EGA hi-resolution mode.
* Set background color.
*
*/
void v_init(bgcolor)
int bgcolor;
{
/* ROM BIOS Video functions -- mode 16 is EGA in high-resolution
* (640x350 pixels)
*/
xr.h.ah = SETMODE;
xr.h.al = EGAMODE;
int86 (BIOS_VIDEO, &xr, &yr);
/* Set color.
*/
xr.h.ah = SETCOLOR;
xr.h.bh = 0;
xr.h.bl = bgcolor;
int86 (BIOS_VIDEO, &xr, &yr);
/* Done */
}
/*-------------------------------------------------------------------*/
/*
* v _ d r a w
*
* Draw a line of specified color between the two points (x1,y1)
* and (x2,y2). Uses Bresenham's Algorithm described in:
* J.D. Foley and A. Van Dam, FUNDAMENTALS OF INTERACTIVE
* COMPUTER GRAPHICS, Addison-Wesley, 1982, pp.433-435.
*
*/
void v_draw(x1, y1, x2, y2, color)
int x1, y1, x2, y2, color;
{
int dx, dy, incr1, incr2, incr3, d, x, y, xend, yend;
dx = abs(x2-x1);
dy = abs(y2-y1);
if (dy<=dx)
{ /* Absolute value of slope of line is less than 1 */
if (x1>x2)
{ /* Start at point with smaller x coordinate */
x = x2;
y = y2;
xend = x1;
dy = y1-y2;
}
else
{
x = x1;
y = y1;
xend = x2;
dy = y2-y1;
}
d = 2*dy-dx;
incr1 = 2*dy;
incr2 = 2*(dy-dx);
incr3 = 2*(dy+dx);
putdot (x, y, color);
while (x < xend)
{
x += 1;
if (d >= 0)
{
if (dy<=0)
{ /* Negative or zero slope */
d += incr1;
}
else
{ /* Positive slope */
y += 1;
d += incr2;
}
}
else
{
if (dy>=0)
{ /* Negative or zero slope */
d += incr1;
}
else
{ /* Positive slope */
y -= 1;
d += incr3;
}
}
putdot (x, y, color);
} /* end while */
} /* end if */
else /* Absolute value of slope is greater than 1 */
{
if (y1>y2)
{ /* Start with point with samller y coordinate */
y = y2;
x = x2;
yend = y1;
dx = x1-x2;
}
else
{
y = y1;
x = x1;
yend = y2;
}
d = 2*dx-dy;
incr1 = 2*dx;
incr2 = 2*(dx-dy);
incr3 = 2*(dx+dy);
putdot (x, y, color);
while (y < yend)
{
y += 1;
if(d >= 0)
{
if (dx <= 0)
{ /* Negative or zero slope */
d += incr1;
}
else
{ /* Positive slope */
x += 1;
d += incr2;
}
}
else
{
if (dx >= 0)
{ /* Negative or zero slope */
d += incr1;
}
else
{ /* Positive slope */
x -= 1;
d += incr3;
}
}
putdot (x, y, color);
} /* end while */
} /* end else */
/* Done */
}
/*-------------------------------------------------------------------*/
/*
* p u t d o t
*
* Put a dot of specified color at location (x,y) on screen.
* Check if dot coordinates are within screen bounds.
*
* ^ y-axis
* |_________
* Convention: | | Origin at lower left corner of screen.
* | |
* (0,0)->|________| x axis
*
*/
void putdot(x, y, color)
int x, y, color;
{
if ( x<0 | x>MAXXDOT | y<0 | y>MAXYDOT ) return;
xr.x.dx = MAXYDOT - y;
xr.x.cx = x;
xr.h.ah = WRITE_PIX;
xr.h.al = color;
int86 (BIOS_VIDEO, &xr, &yr);
/* Done */
}