You are on page 1of 4

#include

#include
#include
#include
#include
#include
int
int
int
int

<graphics.h>
<stdlib.h>
<stdio.h>
<conio.h>
<dos.h>
<math.h>

line_mat(int
line_dda(int
line_bre(int
line_mid(int

x1,int
x1,int
x1,int
x1,int

y1,int
y1,int
y1,int
y1,int

x2,int
x2,int
x2,int
x2,int

y2,int
y2,int
y2,int
y2,int

color);
color);
color);
color);

int circ_mat(int xc,int yc,int r,int color);


int circ_bre(int xc,int yc,int r,int color);
int circ_mid(int xc,int yc,int r,int color);
int main(void)
{
int elapse;
struct time t1,t2;
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();
// get start time
gettime(&t1);
printf("The Start time is: %2d:%02d:%02d.%02d\n",
t1.ti_hour, t1.ti_min, t1.ti_sec, t1.ti_hund);
//
//
//
//
//

draw line graphics


line_mat(0,0,xmax*1,ymax*1,1);
line_dda(0,0,xmax*1,ymax*1,1);
line_bre(0,0,xmax*1,ymax*1,1);
line_mid(0,0,xmax*1,ymax*1,1);

//
//
//
//

draw circle graphics


circ_mat(320,240,480*50,1);
circ_bre(320,240,480*50,1);
circ_mid(320,240,480*50,1);

// get finish time

gettime(&t2);
printf("The Finish time is: %2d:%02d:%02d.%02d\n",
t2.ti_hour, t2.ti_min, t2.ti_sec, t2.ti_hund);
// elapse time
elapse = (t2.ti_hour*60*60*100+t2.ti_min*60*100+t2.ti_sec*100+t2.ti_hund)- \
(t1.ti_hour*60*60*100+t1.ti_min*60*100+t1.ti_sec*100+t1.ti_hund);
printf("The Elapse time is: %d x 1/100 second \n",elapse);
/* clean up */
getch();
closegraph();
return 0;
}
int line_mat(int x1,int y1,int x2,int y2,int color)
// Algorithm y = m * x + b
// It is assumed that x1 < x2 and the gradient is less than 1.
{ float m = ((float) (y2 - y1)) / ((float) (x2 - x1));
float c = y1 - m * x1;
for ( int x = x1; x <= x2; x++)
{ float fy = m * x + c;
int y = fy + 0.5;
putpixel(x,y,color);
}
return(0);
}
int line_dda(int x1,int y1,int x2,int y2,int color)
// Algorithm digital differential analyzer
{ int dx,dy,step,k;
float x_increment,y_increment,x,y;
dx = x2-x1; dy = y2-y1;
// determine maximum step
if (abs(dx) > abs(dy)) step=abs(dx); else step=abs(dy);
x_increment = float(dx) / float(step);
y_increment = float(dy) / float(step);
x = x1; y = y1;
putpixel(int (x+0.5),int(y+0.5),color);
for (k=1;k<=step;k++)
{ x = x+x_increment;
y = y+y_increment;
putpixel(int(x+0.5),int(y+0.5),color);
}
return(0);
}
int line_bre(int x1,int y1,int x2,int y2,int color)
// Algorithm Bresenham
{ int dx,dy,x,y,x_end;
int p,const1,const2;
dx = x2-x1; dy = y2-y1;
p = 2*dy-dx; y = y1;
const1 = 2*dy; const2 = 2*(dy-dx);
// determine which point to use as start, which as end
if (x1 > x2)
{ x = x2; y = y2; x_end = x1; }
else
{ x = x1; y = y1; x_end = x2; }
putpixel(x,int(y+0.5),color);
while ( x < x_end )

{ x++;
if ( p < 0 )
p = p+const1;
else
{ y = y+1;
p = p+const2;
}
putpixel(x,int(y+0.5),color);
}
return(0);
}
int line_mid(int x1,int y1,int x2,int y2,int color)
// Algorith midpoint
// It is assumed that x1 < x2 and the gradient is less than 1.
{ int x,y=y1;
int a = y2 - y1;
int b = x2 - x1;
int G = 2 * a - b; // Decision variable
int DeltaG1 = 2 * (a - b);
int DeltaG2 = 2 * a;
for (x = x1; x <= x2; x++)
{ if (G > 0)
{ G += DeltaG1;
y++; ;
// Next column and row.
putpixel(x,y,color);
}
else
{ G += DeltaG2;
// y not changed
// Next column.
putpixel(x,y,color);
}
}
return (0);
}
int circ_mat(int xc,int yc,int r,int color)
// Algorithm y = yc +/- sqrt(r^2-(x-xc)^2)
// It is assumed that theta 45 degrees
{ int x, y, dy;
for ( x = xc; x <= xc+int (r/2); x++)
{ dy = int( sqrt( double(r)*double(r)- double ((x-xc)*(x-xc))) + 0.5);
putpixel(x,yc+dy,color);
}
return(0);
}
int circ_bre(int xc,int yc,int r,int color)
// Algorithm y = yc +/- sqrt(r^2-(x-xc)^2)
// It is assumed that theta 45 degrees
{ int x, y, p;
x = 0; y = r; p = 3 - 2 * r;
while ( x < y )
{ putpixel(xc+x,yc+y,color);
if ( p < 0 ) p = p + 4 * x + 6;
else
{ p = p + 4 * ( x-y) + 10;
y = y -1;
}

x++;
}
return (0);
}
int circ_mid(int xc,int yc,int r,int color)
/* Draw the second octant of a circle centred on the top
left corner of the screen. This particular algorithm will only draw
an arc whose inclination is between 0 and 45 degrees.
*/
{ int x, y, G, DeltaG1, DeltaG2;
x = 0; y = r;
G = 1 - r;
DeltaG1 = 3; DeltaG2 = -2 * r + 5;
while (x < y)
{ if (G < 0)
{ G += DeltaG1;
DeltaG1 += 2;
DeltaG2 = DeltaG2 + 2;
}
else
{ G += DeltaG2;
DeltaG1 += 2;
DeltaG2 += 4;
y--;
}
x++;
putpixel(xc+x,yc+y,color);
}
return(0);
}

You might also like