You are on page 1of 15

1)/* Program to initialize the graph and draw a line

* /
#include<graphics.h>
#include<conio.h>
void main()
{
int gd =DETECT: /*Detect the graph driver dynamically*/
int gm;
/*for graph mode*/
initgraph (&gd,&gm,’’’); /* graph driver, graph mode and
path has to be passed as parameters. The empty path is specified means
the path will be taken dynamically after searching in the computer.
Otherwise we need to spcify the path where bgi directory is stored in the
computer */
line(10,10,200,200);
/* this function draws a line from
starting co-ordinates(10,10) to the target co-ordinates (200,200). These
co-ordintes are specified in terms of pixels */
getch();
closegraph();
/* close the graph mode */
}

2)
/* program to draw a rectangle , lines as its diagonals and a circle */
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,””);
rectangle(10,10,200,200);
/* draws a rectangle */
line(10,10,200,200);
/* draw a line on the main diagonal*/
line(10,200,200, 10);
/* draws a line on off diagonal */
circle(105,105,95);
/* draws a circle taking (105,105)
as center co-ordinates and 95 as radius
all the dimensions are in pixels */
getch();
closegraph();
}

/*closes the graph mode */

50.90. 0 as starting angle and 360 as ending angle and 80 pixel as Y radius */ setcolor(4).50). /* draws an ellipse taking center of the screen as its center .80.””).h> # include<conio.360. 80). Circle (getmaxx ()/2. /*sets the drawing color as green */ ellipse(getmaxx()/2.h> # include<dos. Initgraph (&gd. /*draws midpoint of the screen as center co-ordinates and 100 as radius */ a circle taking setcolor(2). /*sets the drawing color as red */ ellipse(getmaxx()/2. rand() % getmaxy(). rand()%16). int gm. /* closes the graph mode */ } 4) /* program to demonstrate put pixel */ # include<graphics.h> void main() { int gm.h> #include<conio.3) /* program to draw a circle and ellipses */ #include<graphics.h> # include <stdlib.I. gd=DETECT. /*draws half the ellipse starting from 90 angle and ending at 27o angle with 50 pixels as X-radius and 80 pixels as Y-radius in red color */ getch(). &gm. while(!kbhit())/* until pressing any key this loop continues */ { putpixel(rand()%getmaxx(). getmaxy()/2. .getmaxy ()/2. initgraph(&gd.0.h> void main() { int gd=DETECT.’’’’).&gm. closegraph().270.100).

while(!kbhit()) /* until pressing any key this loop continues */ { /* To draw rectangle co-ordinatinates are taken randomly */ x1=rand()%getmaxx(). /* exchange of x1 and x2 when x1 is >x2 */ /* exchange of y1 and y2 when y1 is > y2 */ . if (x1>x2) { c=x1. /* just to draw the pixels slowly*/ } getch ().h> # include <dos. x2=rand()%getmaxx().gd= DETECT.y1.x2. } if(y1>y2) { c=y1.&gm.c.I.h> # include <stdlib./*x and y co-ordinates and the color are taken randomly*/ delay(2).’’’’). y1=y2.h> # Include <conio. /* closes the graph mode */ } 5) /* Program to demonstrate rectangles using putpixel and filling them with different fill effects */ # Include <graphics. x2=c. x1=x2.y2. y2=rand()%getmaxy(). initgraph(&gd. int x1. y1=rand()%getmaxy().h> void main() { int gm. closegraph().

y2.i>=x1.c). /* setting the random fill styles and colors * floodfill(x1+1. delay(1). rand()%8).i.++i) { putpixel(I. } c=rand()%16.h> #include<dos.c).I>=y1.++i) { putpixel(x2.c).h> #include<conio.i<=x2.h> #include<stdlib.h> . delay (1). i) { putpixel(x1.I. delay(200). /* closes the graph mode */ } 6) /* Program to demonstrate lines with different colors and co-ordinates */ #include<graphics. closegraph().I<=y2.c). i) { putpixel(i.y1+1. } setfillsytyle(rand()%12. delay(1). } for(i=y1. } for(i=x2.y1.y2=c. /* for rectangle using putpixel */ for(I=x1 . delay(1). /* to draw the pixels slowly */ } getch(). } for(i=y2.c).

/* draw the pixels slowly */ } getch(). /*to set the line color */ line(x1.x2.&gm.y1. while(kbhit()) /* until pressing any key this loop continues */ { /* to draw rectangle co-ordinates are taken randomly */ x1=rand()%getmaxx(). closegraph(). int x1.&y1).y2. gd=DETECT.i.&x1. line styles and co. initgraph(&gd. /*closes the graph mode */ } 7) /* Program to demonstrate viewport.c. y2=rand()%getmaxy().ordinates */ #include<graphics. clrscr(). int x1.h> #include<stdio.h> void main() { int gm.void main() { int gm.I.h> #include<conio.c. scanf(“%d%d”. /* to draw the line */ delay(200).x2. y1=rand()%getmaxy(). clipping and lines with different colors.y1.h> #include<dos.x2. printf(“enter starting co-ordinates of viewport (x1. setcolor(rand ()%16).h> #include<stdlib. gd=DETECT. x2=rand()%getmaxx().y1)/n”).y2.””).y1.y2). .

x2.h> #include<stdlib.y1.&x2. gd=DETECT. initgraph(&gd. setlinestle(rand()%10.y2. initgraph(&gd.&gm.””). setcolor(rand()%16). /*to show the boundary of viewport */ setviewport(x1. rectangle(x1.x2.y2).&gm.y2)/n”).printf(“enter ending co-ordinates of viewport(x2.y2).y1. scanf(“%d%d”. /*to set the line color */ line(x1.””). y1=rand()%getmaxy(). } getch(). rand()%20). /*closes the graph mode */ } 8) /* Program to demonstrate text and its setting */ #include<graphics.x2. closegraph().&y2).h> #include<stdio. /*to draw the line */ delay(200).h> void main() { int gm. setcolor(5). /* view port is set and any drawing now onwards must be drawn within the viewport only */ while(1kbhit()) /*until pressing any key this continues */ { /* Rectangle coordinates are taken randomly */ x1=rand()%getmaxx(). x2=rand()%getmaxx(). .h> #include<dos. y2=rand()%getmaxy().y1.1).h> #include<conio.

0.5). /*sets the text style with font.” “). } 9) /* Program to draw a car. getch(). outtext(“silicon”). &gm. sertextstyle(1. outtextxy(150. direction and char size */ moveto(100.h> #include<graphics.settextstyle(4. outtext(“Valley”).0. int gd=DETECT.50.y.6).200).0.100 */ outtext(“Bangalore is”).5).h> main() { int x. moveto(300. do . initgraph(&gd.choice. settextstyle(3. void*car. setcolor(1) settextstyle(5. moveto(200. /*takes the CP to 100.100). unsigned int size.1.”Bangalore is”). setcolor(2).i.6).300).gm. The different graphical functions are used to draw different parts of the car */ #include<stdio. setcolor(4).

35).345. break.275).65.85.300).300).175.180.220.300.300.250.215.25).300.250).275).300.250).35). circle(100. line(140. circle(300.&gm. getch(). printf(“3:CAR\n”).200.300. line(220. circle(100.250).270.” “). line(120.215). line(65. switch(choice) { case 1 : initgraph (&gd. line(345. line(180.”MARUTI 800”).&gm.270).220). scanf(“%d”. line(135.180. printf(“1:BODY OF THE CAR\n”).180.220.13). line(65. line(110.0.220. case 3: initgraph (&gd.” “).13).220).220.&gm.{ cleardevice().””).300.140.300. printf(“\nEnter your choice\n”). cleardevice().215). arc(300.220).300.300. .220.345.40.25).265. circle(300. outtextxy(150. cleardevice().110. break. getch(). arc(100. line(335.&choice).300.345. line(260. line(260. case 2: initgraph(&gd. line(175. printf(“4:QUIT”).0. printf(“2:WHEELS OF THE CAR\n”).300.260.250.

85.250. line(220.300. line(140.300).250.300.345.25).175.13).300. getch().345.345.215).220. line(135.25).float y2) { differential .220.200.35).circle(100.260.300.300. line(335.h> /*function for plotting the point and drawing the line */ void ddaline(float x1. line(345. } } while(choice!=4).220.180. cleardevice().300.300.float x2. circle(100.250).220). case 4 : exit(0).220.180.0.220).h> # include <graphics.110. line(175. line(260.220).h> # include<math.300). circle(300. line(180.270). arc(100. line(110.300. line(260. circle(300.140.300. line(65.270.0.65. line(65. break.265. line(120.180.220.215).300.h> # include<conio.250).35).215. } 10) /*Program to Generate a Line using Digital Algorithm(DDA) */ # include<stdio.float y1.215).275).13).275). getch(). arc(300.300.

x2.&gm.h> #include<stddlib. getch().y2). y=y=yinc.h> # include <graphics.color). yinc=(y2-y1)/steps.&y1.y1.x2. “c:\\tc\\bgi”).y1.color: float x1.color). printf(“\n enter the end points:\n”). putpixel((int)x. steps.(int)y.&x2.gm. x=x1. initgraph(&gd. } 11) /* Program to generate a line using Bresenham’s algorithm */ # include<stdio.&y2). yinc. steps=ads(x2-x1).y. /* plots the points with specified color */ x=x+xinc. xinc.int i.y2.int). } { /* The main program that inputs line end point and passes them to ddaline() function */ void main() { int gd=DETECT. clrscr().i<=steps.int. ddaline(x1. color=5. for(i=1.(int)y. closegraph(). scanf(“%f %f %f %f”. float x. xinc=(x2-x1)/steps. steps=abs(y2-y1).h> # include<conio.&x1.h> void brline(int. y=y1.int. . i++) { putpixel((int)x. if (steps<abs(y2-y1).

float dx. clrscr(). dx=abs(x2-x1). int color. brline(x1.y2 :”). initgraph(&gdriver. dy=abs(y2-y1). putpixel((int)x. getch().(int)y.xend.y2. &gmode.y1. closegraph().int x2. y=y1.y2).&y1).color). xend=x2. y=y2. scanf(“%d%d”.&x1.int y2) { int e.l. if(x1>x2) { x=x2.y1 :”).int y1.x2.y1. while(x<xend) { color=random(getmaxcolor()).y.&y2).&x2. } else { x=x1.color: int x1. } /* Function for Bresenham’s line */ void brline(int x1.””). if(e>0) .x.x2.dy. scanf(“%d%d”.void main() { int gd=DETECT. xend=x1.gm. printf(“\n enter the starting point x2. printf(“\n enter the starting point x1. } e=2*dy-dx.

gd=DETECT.I.142/180)+y*sin(i*3.y1+getmaxy()/2.y.h> #include<alloc.&gm. x1=x*cos(i*3. e=e+2*(dy-dx).i+=10) { setcolor(i+1). for(i=0.x1.h> #include<math. y1=x*sin(i*3. /*The c0-ordinate values for calculating radius */ y=40. x=40.h> # include<dos.h> # include<conio. /* center of the circle is center of the screen*/ delay(10).142/180). int x. circle(x1+getmaxx()/2.””).{ y++. initgraph(&gd.5).142/180)-y*cos(I*3.142/180). } getch(). x++.i<=360.h> void main() { int gm. } } 12) /* Program to demonstrate circle using DDA algorithm */ # include <graphics.. } .j.y1. } else e=e+2*dy.

13) /* program to implement Bresenham’s Circle Drawing Algorithm */ # include<stdio.ycevtre-x. while(x<y) { if(p<0) p=p+2*x+1: else { y--. p=1-radius. putpixel(xcentre-y. */ void cir(int xcentre.h> # include <graphics.y)co-ordinates.ycevtre+x.color). putpixel(xcentre+y.ycentre. ycentre.h> /* Function for plotting the co-ordinates at four different angles that are placed at egual distences */ void plotpoints(int xcentre.ycevtre-x.h> #include<dos.y.ycevtre+x.color). putpixel(xcentre+x. int ycentre. putpixel(xcentre+x. putpixel(xcentre-x. x=0.y). putpixel(xcentre+y. plotpoints(xcentre.ycevtre+y.ycevtre+y. y=radius.color).x. putpixel(xcentre-x. } /* Function for calculating the new points for(x. int radius) { int x.color).color).int y) { int color=5.ycevtre-y. } .ycevtre-y.color).h> # include<math.color).color).p.int x. putpixel(xcentre-x.h> # include<conio. p=p+2*(x-y)+1.

005) { .&gm. } } /* The main function that takes (x.h> void main() { int gm.ycentre=150.””).5). &radius).h> # include<dos. putpixel(xcentre. scanf(“%d%d%d”.ycentre. ycentre.i+=0.redius=5.y1. delay(100). closegraph().y.x1. initgraph(&gd. clrscr(). cir(xcentre.x++.i.ycentre. getch().y) and ‘r’ the radius from keyboard and activates other functions for drawing the circle */ void main() { intgd=DETECT.i<=360.y).h> # include<conio. for(i=0.h> #include<alloc.gd=DETECT.redius).&gm.””).x. &xcentre. plotpoints xcentre. y=100.gm. &ycentre. x=100. initgraph(&gd.xcentre=200. float x.h> #include<math. } 14) /* Program to demonstrate spiral */ # include <graphics. printf(“\n enter the center points and radius :\n”).

142/180)+y*sin(i*3.h> #include<conio.&gm.142/180). for(j=0.142/180). gd=DETECT.100.++i) { if (i%20==0) setcolor(rand()%16+1).j<200. } 15) /* Program to demonstrate surfaces using lines and colors */ #include<graphics.h> #include<alloc.i.h> void main() { int gm. putpixel((int)x+200.i<=200. setviewport(100. } getch().142/180)+y*cos(i*3. } delay(100).j+20). } getch().(int)y+200. line(i.x=x*cos(i*3.h> #include<math. initgraph(&gd.15).j=j+20) { for(i=0.h> #include<dos.300.0). } .300. y=x*sin(i*3.j.””).