Anda di halaman 1dari 42

Lab Manual: Subject Computer Graphics:

1. /* C graphics program to draw a line */

#include<graphics.h>

#include<conio.h>

int main()

int gd = DETECT,gm;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

line(200,200,300,300);

getch();

closegraph();

return 0;

2. /* C graphics program to draw a circle */

#include<conio.h>

#include<graphics.h>

void main()

clrscr();

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"C:turboc3\\BGI");

circle(20,15,30);

getch();

closegraph();

compiled by FikruM. RVU 2009


3. /* C graphics program to draw a ellipse */

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

int main()

int gd = DETECT,gm;

int x ,y;

initgraph(&gd, &gm, "c:\\TurboC3\\BGI");

/* Initialize center of ellipse with center of screen */

x = getmaxx()/2;

y = getmaxy()/2;

outtextxy(x-100, 50, "ELLIPSE Using Graphics in C");

/* Draw ellipse on screen */

ellipse(x, y, 0, 360, 120, 60);

getch();

closegraph();

return 0;

4. /* C graphics program to draw a polygon*/

#include <graphics.h>

#include <conio.h>

main()

int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};

compiled by FikruM. RVU 2009


initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

drawpoly(4, points);

getch();

closegraph();

return 0;

5. /* C graphics program to draw a draw pie chart */

#include<graphics.h>

#include<conio.h>

int main()

int gd = DETECT, gm, x, y;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

settextstyle(BOLD_FONT,HORIZ_DIR,2);

outtextxy(220,10,"PIE CHART");

/* Setting cordinate of center of circle */

x = getmaxx()/2;

y = getmaxy()/2;

settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1);

setfillstyle(SOLID_FILL, RED);

pieslice(x, y, 0, 60, 120);

outtextxy(x + 140, y - 70, "FOOD");

setfillstyle(SOLID_FILL, YELLOW);

pieslice(x, y, 60, 160, 120);

outtextxy(x - 30, y - 170, "RENT");

compiled by FikruM. RVU 2009


setfillstyle(SOLID_FILL, GREEN);

pieslice(x, y, 160, 220, 120);

outtextxy(x - 250, y, "ELECTRICITY");

setfillstyle(SOLID_FILL, BROWN);

pieslice(x, y, 220, 360, 120);

outtextxy(x, y + 150, "SAVINGS");

getch();

closegraph();

return 0;

6./* C graphics program to draw a draw House chart */

#include<graphics.h>

#include<conio.h>

int main()

int gd = DETECT,gm;

initgraph(&gd, &gm, "c:\\TurboC3\\BGI");

/* Draw Hut */

setcolor(WHITE);

rectangle(150,180,250,300);

rectangle(250,180,420,300);

rectangle(180,250,220,300);

line(200,100,150,180);

line(200,100,250,180);

line(200,100,370,100);

compiled by FikruM. RVU 2009


line(370,100,420,180);

/* Fill colours */

setfillstyle(SOLID_FILL, BROWN);

floodfill(152, 182, WHITE);

floodfill(252, 182, WHITE);

setfillstyle(SLASH_FILL, BLUE);

floodfill(182, 252, WHITE);

setfillstyle(HATCH_FILL, GREEN);

floodfill(200, 105, WHITE);

floodfill(210, 105, WHITE);

getch();

closegraph();

return 0;

7.sin wave

#include <conio.h>

#include <math.h>

#include <graphics.h>

#include <dos.h>

int main()

int gd = DETECT, gm;

int angle = 0;

double x, y;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

compiled by FikruM. RVU 2009


line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);

/* generate a sine wave */

for(x = 0; x < getmaxx(); x+=3)

/* calculate y value given x */

y = 50*sin(angle*3.141/180);

y = getmaxy()/2 - y;

/* color a pixel at the given position */

putpixel(x, y, 15);

delay(100);

/* increment angle */

angle+=5;

getch();

/* deallocate memory allocated for graphics screen */

closegraph();

return 0;

8.tangent graph on screen

#include <conio.h>

#include <math.h>

#include <graphics.h>

#include <dos.h>

int main()

compiled by FikruM. RVU 2009


{

int gd = DETECT, gm;

int angle = 0;

double x, y;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);

/* generate a sine wave */

for(x = 0; x < getmaxx(); x++)

/* calculate y value given x */

y = 50*tan(angle*3.141/180);

y = getmaxy()/2 - y;

/* color a pixel at the given position */

putpixel(x, y, 15);

delay(50);

/* increment angle */

angle+=2;

getch();

/* deallocate memory allocated for graphics screen */

closegraph();

return 0;

9.Moving fish

compiled by FikruM. RVU 2009


#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <dos.h>

int main()

/* request auto detection */

int gdriver = DETECT, gmode;

int errcode, i;

/* initialize graphic mode */

initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");

errcode = graphresult();

if (errcode != grOk)

/* error occurred */

getch();

printf("Error in graphics!!\n");

return 0;

/* fish movement implementation */

for (i = 0; i <= 300; i = i + 3)

/* sleep for 30 milliseconds */

delay(30);

/* clear graphic screen */

compiled by FikruM. RVU 2009


cleardevice();

/* drawing tail */

setcolor(DARKGRAY);

setfillstyle(SLASH_FILL, DARKGRAY);

pieslice(90 + i, getmaxy()/2, 135, 225, 50);

floodfill(90 + i, getmaxy()/2, DARKGRAY);

/* drawing the body of the fish */

setcolor(LIGHTGRAY);

setfillstyle(SOLID_FILL, LIGHTGRAY);

ellipse(150 + i, getmaxy()/2, 0, 360, 80, 25);

floodfill(150 + i, getmaxy()/2, 7);

/* drawing pelvic fins */

setcolor(DARKGRAY);

setfillstyle(SLASH_FILL, DARKGRAY);

pieslice(170 + i, getmaxy()/2, 205, 250, 40);

floodfill(170 + i, getmaxy()/2, DARKGRAY);

/* draw eyes for the fish */

setcolor(BLACK);

setfillstyle(SOLID_FILL, BLACK);

circle(210 + i, getmaxy()/2 - 1, 2);

floodfill(210 + i, getmaxy()/2 - 1, BLACK);

setcolor(BLACK);

arc(210 + i, getmaxy()/2 - 1, 70, 225, 6);

/* sleep for 120 milliseconds */

delay(120);

compiled by FikruM. RVU 2009


/* clears the graphic screen */

cleardevice();

/* tail of the fish */

setcolor(DARKGRAY);

setfillstyle(SLASH_FILL, DARKGRAY);

pieslice(90 + i, getmaxy()/2, 135, 225, 50);

floodfill(90 + i, getmaxy()/2, DARKGRAY);

/* body of the fish */

setcolor(LIGHTGRAY);

setfillstyle(SOLID_FILL, LIGHTGRAY);

ellipse(150 + i, getmaxy()/2, 0, 360, 80, 25);

floodfill(150 + i, getmaxy()/2, LIGHTGRAY);

/* place fin at different position to get motion */

setcolor(DARKGRAY);

setfillstyle(SLASH_FILL, DARKGRAY);

pieslice(170 + i, getmaxy()/2, 180, 210, 40);

floodfill(170 + i, getmaxy()/2, DARKGRAY);

/* eye of the fish */

setcolor(BLACK);

setfillstyle(SOLID_FILL, BLACK);

circle(210 + i, getmaxy()/2 - 1, 2);

floodfill(210 + i, getmaxy()/2 - 1, BLACK);

setcolor(BLACK);

arc(210 + i, getmaxy()/2 - 1, 70, 225, 6);

/* sleep for 120 milliseconds */

compiled by FikruM. RVU 2009


delay(120);

/* clear the graphic screen */

cleardevice();

/* draw fish tail */

setcolor(DARKGRAY);

setfillstyle(SLASH_FILL, DARKGRAY);

pieslice(90 + i, getmaxy()/2, 135, 225, 50);

floodfill(90 + i, getmaxy()/2, DARKGRAY);

/* body of the fish */

setcolor(LIGHTGRAY);

setfillstyle(SOLID_FILL, LIGHTGRAY);

ellipse(150 + i, getmaxy()/2, 0, 360, 80, 25);

floodfill(150 + i, getmaxy()/2, LIGHTGRAY);

/* locate fin at different position to get motion */

setcolor(DARKGRAY);

setfillstyle(SLASH_FILL, DARKGRAY);

pieslice(170 + i, getmaxy()/2, 125, 170, 40);

floodfill(170 +i, getmaxy()/2, DARKGRAY);

setcolor(BLACK);

setfillstyle(SOLID_FILL, BLACK);

circle(210 + i, getmaxy()/2 - 1, 2);

floodfill(210 + i, getmaxy()/2 - 1, BLACK);

setcolor(BLACK);

arc(210 + i, getmaxy()/2 - 1, 70, 225, 6);

compiled by FikruM. RVU 2009


}

/* clears the graphic screen */

cleardevice();

/* deallocate memory allocated for graphic screen */

closegraph();

return 0;

11.car Animation

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <dos.h>

int main()

/* request auto detection */

int gdriver = DETECT, gmode, err;

int i, maxx, midy;

/* initialize graphic mode */

initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");

err = graphresult();

if (err != grOk) {

/* error occurred */

printf("Graphics Error: %s\n",

grapherrormsg(err));

compiled by FikruM. RVU 2009


return 0;

/* max position in x-axis */

maxx = getmaxx();

/* mid position in y-axis */

midy = getmaxy() / 2;

for (i = 0; i < maxx - 125; i = i + 3)

/* clears the graphic screen */

cleardevice();

/* draw a white road */

setcolor(WHITE);

line(0, midy + 37, maxx, midy + 37);

/* body of the car */

setcolor(YELLOW);

setfillstyle(SOLID_FILL, YELLOW);

line(0 + i, midy + 23, 0 + i, midy);

line(0 + i, midy, 20 + i, midy);

line(20 + i, midy, 40 + i, midy - 20);

line(40 + i, midy - 20, 80 + i, midy - 20);

line(80 + i, midy - 20, 100 + i, midy);

line(100 + i, midy, 120 + i, midy);

line(120 + i, midy, 120 + i, midy + 23);

line(0 + i, midy + 23, 18 + i, midy + 23);

arc(30 + i, midy + 23, 0, 180, 12);

compiled by FikruM. RVU 2009


line(42 + i, midy + 23, 78 + i, midy + 23);

arc(90 + i, midy + 23, 0, 180, 12);

line(102 + i, midy + 23, 120 + i, midy + 23);

line(28 + i, midy, 43 + i, midy - 15);

line(43 + i, midy - 15, 57 + i, midy - 15);

line(57 + i, midy - 15, 57 + i, midy);

line(57 + i, midy, 28 + i, midy);

line(62 + i, midy - 15, 77 + i, midy - 15);

line(77 + i, midy - 15, 92 + i, midy);

line(92 + i, midy, 62 + i, midy);

line(62 + i, midy, 62 + i, midy - 15);

floodfill(5 + i, midy + 22, YELLOW);

setcolor(DARKGRAY);

/* draw wheels of the car */

if (i % 2 == 0)

setfillstyle(SLASH_FILL, DARKGRAY);

else {

setfillstyle(BKSLASH_FILL, DARKGRAY);

circle(30 + i, midy + 25, 9);

circle(90 + i, midy + 25, 9);

floodfill(30 + i, midy + 25, DARKGRAY);

floodfill(90 + i, midy + 25, DARKGRAY);

compiled by FikruM. RVU 2009


delay(250);

getch();

/* deallocate memory allocated for graphic screen */

closegraph();

return 0;

12.Concentric circle

include<stdio.h>

#include<graphics.h>

#include<conio.h>

int main()

int gd = DETECT,gm;

int x ,y;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

/* Initialize center of circle with center of screen */

x = getmaxx()/2;

y = getmaxy()/2;

outtextxy(240, 50, "Concentric Circles");

/* Draw circles on screen */

setcolor(RED);

circle(x, y, 30);

setcolor(GREEN);

circle(x, y, 50);

compiled by FikruM. RVU 2009


setcolor(YELLOW);

circle(x, y, 70);

setcolor(BLUE);

circle(x, y, 90);

getch();

closegraph();

return 0;

13.goal

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void main()

int x=0,gd=DETECT,gm,points[]={0,220,1600,220,1600,900,0,900,0,220};

float y=0;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setcolor(MAGENTA);

settextstyle(6,HORIZ_DIR,6);

outtextxy(200,250,"Hi");

delay(1000);

cleardevice();

settextstyle(7,VERT_DIR,1);

outtextxy(200,50,"GET READY FOR ANIMATION");

compiled by FikruM. RVU 2009


delay(1000);

cleardevice();

setcolor(GREEN);

setfillstyle(SOLID_FILL,GREEN);

fillpoly(5,points);

setcolor(WHITE);

circle(100,100,25);

delay(1000);

line(100,125,100,185);

delay(1000);

line(100,135,125,170);

delay(1000);

line(100,135,75,170);

delay(1000);

line(100,185,125,220);

delay(1000);

line(100,185,75,220);

delay(1000);

setcolor(RED);

setfillstyle(SOLID_FILL,RED);

fillellipse(135+x,210-y,10,10);

for(x=0;x<50;x++)

setcolor(WHITE);

line(100,185,75+x,220-y);

compiled by FikruM. RVU 2009


delay(100);

setcolor(BLACK);

line(100,185,75+x,220-y);

y=y+0.25;

setcolor(WHITE);

line(100,185,125,220);

line(100,185,75,220);

for(x=0,y=0;y<100;x++)

setcolor(RED);

setfillstyle(SOLID_FILL,RED);

fillellipse(135+x,210-y,10,10);

delay(10);

setcolor(GREEN);

setfillstyle(SOLID_FILL,GREEN);

fillpoly(5,points);

setcolor(BLACK);

setfillstyle(SOLID_FILL,BLACK);

fillellipse(135+x,210-y,10,10);

y=y+0.5;

for(;x<490;x++)

setcolor(RED);

compiled by FikruM. RVU 2009


setfillstyle(SOLID_FILL,RED);

fillellipse(135+x,y,10,10);

delay(10);

setcolor(BLACK);

setfillstyle(SOLID_FILL,BLACK);

fillellipse(135+x,y,10,10);

y=y+0.25;

setcolor(RED);

setfillstyle(SOLID_FILL,RED);

fillellipse(135+x,y,10,10);

delay(2000);

cleardevice();

setbkcolor(CYAN);

settextstyle(3,HORIZ_DIR,10);

outtextxy(200,80,"GOAL");

getch();

closegraph();

14.transilation

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

#include<graphics.h>

compiled by FikruM. RVU 2009


int x1,x2,y1,y2,mx,my,depth;

void draw();

void trans();

void main()

int gd=DETECT,gm,c;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

printf("\n\t\t3D Translation\n\n");

printf("\nEnter 1st top value(x1,y1):");

scanf("%d%d",&x1,&y1);

printf("Enter right bottom value(x2,y2):");

scanf("%d%d",&x2,&y2);

depth=(x2-x1)/4;

mx=(x1+x2)/2;

my=(y1+y2)/2;

draw();

getch();

cleardevice();

trans();

getch();

void draw()

bar3d(x1,y1,x2,y2,depth,1);

compiled by FikruM. RVU 2009


void trans()

int a1,a2,b1,b2,dep,x,y;

printf("\n Enter the Translation Distances:");

scanf("%d%d",&x,&y);

a1=x1+x;

a2=x2+x;

b1=y1+y;

b2=y2+y;

dep=(a2-a1)/4;

bar3d(a1,b1,a2,b2,dep,1);

setcolor(5);

draw();

15.Program for creating man object:


#include<stdio.h>

#include<graphics.h>

#include<conio.h>

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

setcolor(9);

circle(150,150,35);

line(150,185,150,300);

line(150,200,120,230);
compiled by FikruM. RVU 2009
line(150,200,180,230);

line(150,300,120,330);

line(150,300,180,330);

outtextxy(230,350,"HI, This is Computer Graphics");

getch();

16.transilation

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

setcolor(9);

circle(150,150,35);

line(150,185,150,300);

line(150,200,120,230);

line(150,200,180,230);

line(150,300,120,330);

line(150,300,180,330);

outtextxy(230,350,"HI, This is Computer Graphics");

getch();

17.flood

compiled by FikruM. RVU 2009


#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void flood(int,int,int,int);

void main()

int gd,gm=DETECT;

clrscr();

detectgraph(&gd,&gm);

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

rectangle(50,50,100,100);

flood(55,55,12,0);

getch();

void flood(int x,int y, int fill_col, int old_col)

if(getpixel(x,y)==old_col)

delay(10);

putpixel(x,y,fill_col);

flood(x+1,y,fill_col,old_col);

flood(x-1,y,fill_col,old_col);

flood(x,y+1,fill_col,old_col);

flood(x,y-1,fill_col,old_col);

compiled by FikruM. RVU 2009


flood(x + 1, y - 1, fill_col, old_col);

flood(x + 1, y + 1, fill_col, old_col);

flood(x - 1, y - 1, fill_col, old_col);

flood(x - 1, y + 1, fill_col, old_col);

}}

18.scaling

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

#include<graphics.h>

int x1,x2,y1,y2,mx,my,depth;

void draw();

void scale();

void main()

int gd=DETECT,gm,c;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

printf("\n\t\t3D Scaling\n\n");

printf("\nEnter 1st top value(x1,y1):");

scanf("%d%d",&x1,&y1);

printf("Enter right bottom value(x2,y2):");

scanf("%d%d",&x2,&y2);

depth=(x2-x1)/4;

mx=(x1+x2)/2;

compiled by FikruM. RVU 2009


my=(y1+y2)/2;

draw();

getch();

cleardevice();

scale();

getch();

void draw()

bar3d(x1,y1,x2,y2,depth,1);

void scale()

int x,y,a1,a2,b1,b2,dep;

printf("\n\n Enter scaling Factors:");

scanf("%d%d",&x,&y);

a1=mx+(x1-mx)*x;

a2=mx+(x2-mx)*x;

b1=my+(y1-my)*y;

b2=my+(y2-my)*y;

dep=(a2-a1)/4;

bar3d(a1,b1,a2,b2,dep,1);

setcolor(5);

draw();

compiled by FikruM. RVU 2009


19.walk

#include<stdio.h>

#include<dos.h>

#include<conio.h>

#include<graphics.h>

#include<stdlib.h>

void main()

int gd = DETECT, gm = DETECT, c = -200, i = 0, x = 40, l = 15, h = 15, ht = 0;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

cleardevice();

setcolor(BROWN);

line(0, 201, 600, 201);

cont:

while (!kbhit()) {

setcolor(4);

ellipse(x, 100, 0, 180, 50, 30);

line(x - 50, 100, x + 50, 100);

line(x, 100, x, 150);

circle(x - 20, 115, 15);

line(x - 20, 130, x - 20, 175);

line(x - 20, 175, x - 20 - l, 200);

line(x - 20, 175, x - 20 + l, 200);

line(x - 20, 140, x, 150);

line(x - 20, 140, x - 20 - h, 160);

compiled by FikruM. RVU 2009


setcolor(0);

delay(50);

ellipse(x, 100, 0, 180, 50, 30);

line(x - 50, 100, x + 50, 100);

line(x, 100, x, 150);

circle(x - 20, 115, 15);

line(x - 20, 130, x - 20, 175);

line(x - 20, 175, x - 20 - l, 200);

line(x - 20, 175, x - 20 + l, 200);

line(x - 20, 140, x, 150);

line(x - 20, 140, x - 20 - h, 160);

line(x + 50, 100, x + 50, 200);

x++;

l--;

if (l == -15)

l = 15;

if (ht == 1)

h++;

else

h--;

if (h == 15)

ht = 0;

else if (h == -15)

ht = 1;

compiled by FikruM. RVU 2009


if (getch() == ' ') {

while (!kbhit());

getch();

goto cont;

}}

20.color:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void flood(int,int,int,int);

void main()

int gd,gm=DETECT;

clrscr();

detectgraph(&gd,&gm);

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

rectangle(50,50,100,100);

flood(55,55,9,0);

getch();

void flood(int x,int y, int fill_col, int old_col)

if(getpixel(x,y)==old_col)

compiled by FikruM. RVU 2009


delay(10);

putpixel(x,y,fill_col);

flood(x+1,y,fill_col,old_col);

flood(x-1,y,fill_col,old_col);

flood(x,y+1,fill_col,old_col);

flood(x,y-1,fill_col,old_col);

}}

21. x shear

include<stdio.h>

#include<conio.h>

#include<dos.h>

#include<graphics.h>

void main()

int gd=DETECT,gm;

float shx,shy;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

printf("Enter shear factor shx along x-axis :");

scanf("%f",&shx);

line(100,0,200,0);

line(200,0,200,200);

line(200,200,100,200);

line(100,200,100,0);

printf("X-shear");

setcolor(12);

compiled by FikruM. RVU 2009


line((100+(0*shx)),0,(200+(0*shx)),0);

line((200+(0*shx)),0,(200+(200*shx)),200);

line((200+(200*shx)),200,(100+(200*shx)),200);

line((100+(200*shx)),200,(100+(0*shx)),0);

getch();

}
22. y shear

#include<stdio.h>

#include<conio.h>

#include<dos.h>

#include<graphics.h>

void main()

int gd=DETECT,gm;

float shx,shy;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

printf("Enter shear factor shy along y-axis :");

scanf("%f",&shy);

line(100,10,200,10);

line(200,10,200,200);

line(200,200,100,200);

line(100,200,100,10);

printf("Y-shear");

setcolor(12);

line(100,10+(shy*100),200,10+(shy*200));

line(200,10+(shy*200),200,200+(shy*200));
compiled by FikruM. RVU 2009
line(200,200+(shy*200),100,200+(shy*100));

line(100,200+(shy*100),100,10+(shy*100));

getch();

closegraph();

23.reflection

# include <stdio.h>

# include <conio.h>

# include <graphics.h>

# include <math.h>

char IncFlag;

int PolygonPoints[3][2] ={{10,100},{110,100},{110,200}};

void PolyLine()

int iCnt;

cleardevice();

line(0,240,640,240);

line(320,0,320,480);

for (iCnt=0; iCnt<3; iCnt++)

line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],

PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%3][1]);

void Reflect()

compiled by FikruM. RVU 2009


{

float Angle;

int iCnt;

int Tx,Ty;

printf("endl");;

for (iCnt=0; iCnt<3; iCnt++)

PolygonPoints[iCnt][1] = (480 - PolygonPoints[iCnt][1]);

void main()

int gDriver = DETECT, gMode;

int iCnt;

initgraph(&gDriver, &gMode, "C:\\TurboC3\\BGI");

for (iCnt=0; iCnt<3; iCnt++)

PolygonPoints[iCnt][0] += 320;

PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];

PolyLine();

getch();

Reflect();

PolyLine();

getch();

24.Pendulum using c in graphics

compiled by FikruM. RVU 2009


#include<graphics.h>

#include<conio.h>

#include<dos.h>

#include<process.h>

int cx=300,cy=100,r=300;

int x=0,y,p;

int path(int dtn)

int x1,y1;

setcolor(15);

if(x>=y)

return 0;

cleardevice();

if(dtn==1)

circle(cx+x,cy+y,20);

line(cx,cy,cx+x,cy+y);

else

circle(cx-x,cy+y,20);

line(cx,cy,cx-x,cy+y);

compiled by FikruM. RVU 2009


delay(10);

if(kbhit())

exit(0);

x++;

if(p<0)

p+=2*x+1;

else

y--;

p+=2*(x-y)+1;

x1=x;

y1=y;

path(dtn);

cleardevice();

if(dtn==1)

circle(cx+x1,cy+y1,20);

line(cx,cy,cx+x1,cy+y1);

else

circle(cx-x1,cy+y1,20);

line(cx,cy,cx-x1,cy+y1);

compiled by FikruM. RVU 2009


delay(10);

if(kbhit())

exit(0);

return(0);

void main()

int gd=DETECT,gm=DETECT;

initgraph(&gd,&gm,"C:/TURBOC3/BGI");

cleardevice();

putpixel(300,100,4);

while(1)

x=0;

y=r;

p=1-r;

path(1);

x=0;

y=r;

p=1-r;

path(0);

25. Biycle

#include<stdio.h>

compiled by FikruM. RVU 2009


#include<conio.h>

#include<graphics.h>

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:/TURBOC3/BGI");

/*create wheels of cycle*/

circle(200,200,50);

circle(400,200,50);

line(400,150,400,250);

line(350,200,450,200);

line(375,156,425,245);

line(356,180,446,222);

line(370,240,425,158);

line(356,220,444,180);

line(200,150,200,250);

line(150,200,250,200);

line(175,156,225,245);

line(156,180,246,222);

line(170,240,225,158);

line(156,220,244,180);

circle(400,200,3);

circle(200,200,3);

circle(200,200,10);

circle(300,200,15);

compiled by FikruM. RVU 2009


line(200,190,300,185);

line(200,210,300,215);

line(300,170,300,230);

/*Draw Paddles*/

line(280,170,300,170);

line(320,230,300,230);

line(200,200,300,200);

line(300,200,220,120);

line(300,200,380,120);

line(220,120,380,120);

line(200,200,220,120);

line(220,120,220,110);

/*Draw Seat*/

circle(220,100,10);

setfillstyle(SOLID_FILL,BLUE);

floodfill(220,100,WHITE);

/*Draw Handle*/

line(400,200,365,50);

ellipse(325,50,280,80,40,40);

arc(200,200,0,180,55);

arc(400,200,80,210,55);

getch();

closegraph();

26. ball

compiled by FikruM. RVU 2009


#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void main()

int gd = DETECT, gm = DETECT;

int x, y = 0, j, t = 400, c = 1;

initgraph(&gd, &gm, "C:/TURBOC3/BGI");

setcolor(RED);

setfillstyle(SOLID_FILL, RED);

for (x = 40; x < 602; x++)

cleardevice();

circle(x, y, 30);

floodfill(x, y, RED);

delay(40);

if (y >= 400)

c = 0;

t -= 20;

if (y <= (400 - t))

c = 1;

y = y + (c ? 15 : -15);

compiled by FikruM. RVU 2009


}

getch();

27. bar

program to draw 3D bar graph using graphics

#include <graphics.h>

#include <conio.h>

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

settextstyle(BOLD_FONT,HORIZ_DIR,2);

outtextxy(275,0,"3D BAR GRAPH");

setlinestyle(SOLID_LINE,0,2);

/* Print X and Y Axis */

line(90,410,90,50);

line(90,410,590,410);

line(85,60,90,50);

line(95,60,90,50);

line(585,405,590,410);

line(585,415,590,410);

outtextxy(65,60,"Y");

outtextxy(570,420,"X");

outtextxy(70,415,"O");

/* Print 3D bars */

compiled by FikruM. RVU 2009


setfillstyle(XHATCH_FILL, RED);

bar3d(150,80,200,410, 15, 1);

bar3d(225,100,275,410, 15, 1);

bar3d(300,120,350,410, 15, 1);

bar3d(375,170,425,410, 15, 1);

bar3d(450,135,500,410, 15, 1);

getch();

closegraph();

return 0;

28.C program to make a digital clock using graphics

#include <conio.h>

#include <graphics.h>

#include <time.h>

#include <dos.h>

#include <string.h>

int main()

int gd = DETECT, gm;

int midx, midy;

long current_time;

char timeStr[256];

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

/* mid pixel in horizontal and vertical axis */

midx = getmaxx() / 2;

compiled by FikruM. RVU 2009


midy = getmaxy() / 2;

while (!kbhit())

cleardevice();

setcolor(WHITE);

setfillstyle(SOLID_FILL, WHITE);

rectangle(midx - 250, midy - 40, midx + 250, midy + 40);

floodfill(midx, midy, WHITE);

/* Get Current epoch time in seconds */

current_time = time(NULL);

/* store the date and time in string */

strcpy(timeStr, ctime(&current_time));

setcolor(RED);

settextjustify(CENTER_TEXT, CENTER_TEXT);

settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 4);

moveto(midx, midy);

/* print current time */

outtext(timeStr);

/* Add delay of 1000 milliseconds(1 second) */

delay(1000);

getch();

closegraph();

return 0;

compiled by FikruM. RVU 2009


}

compiled by FikruM. RVU 2009

Anda mungkin juga menyukai