0% found this document useful (0 votes)
41 views61 pages

CG Prac QB

Uploaded by

Rick Sanchez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views61 pages

CG Prac QB

Uploaded by

Rick Sanchez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Smiley

#include <conio.h>

#include <dos.h>

#include <graphics.h>

#include <stdio.h>

// Driver Code

int main()

// Initialize graphic driver

int gr = DETECT, gm;

// Initialize graphics mode by passing

// three arguments to initgraph function

// &gdriver is the address of gdriver

// variable, &gmode is the address of

// gmode and "C:\\Turboc3\\BGI" is the

// directory path where BGI files

// are stored

initgraph(&gr, &gm, "C:\\Turboc3\\BGI");

// Set color of smiley to yellow

setcolor(YELLOW);
// creating circle and fill it with

// yellow color using floodfill.

circle(300, 100, 40);

setfillstyle(SOLID_FILL, YELLOW);

floodfill(300, 100, YELLOW);

// Set color of background to black

setcolor(BLACK);

setfillstyle(SOLID_FILL, BLACK);

// Use fill ellipse for creating eyes

fillellipse(310, 85, 2, 6);

fillellipse(290, 85, 2, 6);

// Use ellipse for creating mouth

ellipse(300, 100, 205, 335, 20, 9);

ellipse(300, 100, 205, 335, 20, 10);

ellipse(300, 100, 205, 335, 20, 11);

getch();

// closegraph function closes the

// graphics mode and deallocates

// all memory allocated by

// graphics system

closegraph();
return 0;

Circle
#include<graphics.h>

#include<conio.h>

#include<stdio.h>

void main()

int x,y,x_mid,y_mid,radius,dp;

int g_mode,g_driver=DETECT;

clrscr();

initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");

printf("*********** MID POINT Circle drawing algorithm ********\n\n");

printf("\nenter the coordinates= ");

scanf("%d %d",&x_mid,&y_mid);

printf("\n now enter the radius =");

scanf("%d",&radius);

x=0;

y=radius;

dp=1-radius;

do

putpixel(x_mid+x,y_mid+y,YELLOW);

putpixel(x_mid+y,y_mid+x,YELLOW);
putpixel(x_mid-y,y_mid+x,YELLOW);

putpixel(x_mid-x,y_mid+y,YELLOW);

putpixel(x_mid-x,y_mid-y,YELLOW);

putpixel(x_mid-y,y_mid-x,YELLOW);

putpixel(x_mid+y,y_mid-x,YELLOW);

putpixel(x_mid+x,y_mid-y,YELLOW);

if(dp<0) {

dp+=(2*x)+1;

else{

y=y-1;

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

x=x+1;

}while(y>x);

getch();

Balloons

#include <stdio.h>
#include <conio.h>
#include <graphics.h>

void flood(int x, int y, int interior,int fill)


{
int j =0;
if( getpixel(x,y) == interior)
{
putpixel(x,y,fill);
flood(x+1,y-j,interior,fill);
flood(x-1,y-j,interior,fill);
flood(x,y-1-j,interior,fill);
flood(x,y+1-j,interior,fill);
}
}

int main()
{
int gd =DETECT, gm;
int i;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
for(i=0;i<200;i=i+10)
{
cleardevice();
ellipse(50+i,350-i,0,360,20,50);
arc(100+i,400-i,180,270,50);
flood(50+i,350-i,BLACK,RED);
ellipse(150+i,350-i,0,360,20,50);
arc(200+i,400-i,180,270,50);
flood(150+i,350-i,BLACK,GREEN);
ellipse(250+i,350-i,0,360,20,50);
arc(300+i,400-i,180,270,50);
flood(250+i,350-i,BLACK,BLUE);
delay(1000);
}

getch();
closegraph();
return 0;
}

Kite
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<stdlib.h>
void main()

int gm,gd=DETECT;

int i= 0,j=0,rnd_x=0,rnd_y,stop_me=0;

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

srand(time());

while(stop_me<=1000)

if(i>=180 &&j>=100 ) // controlling kite, so that it wouldn't disappear from screen

rnd_x=rand()%4 -3;

rnd_y=rand()%5 -4;

else{

rnd_x=rand()%3;

rnd_y=rand()%3;

line(200+i,200-j,250+i,250-j);

line(200+i,200-j,150+i,250-j);

line(150+i,250-j,200+i,350-j);

line(200+i,350-j,250+i,250-j);

line(200+i,200-j,200+i,350-j);

arc(200+i,275-j,25,155,50);

line(0,500,200+i,225-j);

i=i+rnd_x;

j=j+rnd_y;
stop_me=5+stop_me;

delay(100);

clearviewport(); // clearing image which would make illusion of flying kite

getch();

closegraph();

Traffic Signal
#include <stdio.h>
#include <conio.h>
#include <graphics.h>

int main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
rectangle(220,50,370,430);
circle(295,240,50);
circle(295,130,50);
circle(295,350,50);

setfillstyle(1, RED);
floodfill(295,130,WHITE);
delay(1000);
setfillstyle(1, BLACK);
floodfill(295,130,WHITE);
setfillstyle(1, YELLOW);
floodfill(295,240,WHITE);
delay(1000);
setfillstyle(1, BLACK);
floodfill(295,240,WHITE);
setfillstyle(1,GREEN);
floodfill(295,350,WHITE);
getch();
closegraph();
return 0;
}

Man
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>

void symmetry(int xc, int yc, int x, int y)


{
putpixel(xc+x,yc+y, WHITE);
putpixel(xc+x,yc-y, WHITE);
putpixel(xc-x,yc+y, WHITE);
putpixel(xc-x,yc-y, WHITE);
putpixel(xc+y,yc+x, WHITE);
putpixel(xc+y,yc-x, WHITE);
putpixel(xc-y,yc+x, WHITE);
putpixel(xc-y,yc-x, WHITE);
}
void drawCircle(int xc, int yc,int r)
{
int x=0;
int y=r;
int p=1-r;
symmetry(xc,yc,x,y);
for(x=0; x<y; x++)
{
if(p<0)
p=p+2*x+1;
else{
p=p+2*x+1-2*y;
y--;
}
symmetry(xc,yc,x,y);
}
}

void main()
{
int gd=DETECT,gm;
int xc=320,yc=120,r=60;
int X,Y;
int x[3],y[3];
double px,py,j;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

x[0]=290;
y[0]=140;
x[1]=320;
y[1]=180;
x[2]=350;
y[2]=140;

//face
drawCircle(xc,yc,r);
//eyes
drawCircle(xc-20,yc-20,8);
drawCircle(xc+20,yc-20,8);
//smile
for(j=0; j<=1; j+=0.001)
{
px=(1-j)*(1-j)*x[0]+2*j*(1-j)*x[1]+j*j*x[2];
py=(1-j)*(1-j)*y[0]+2*j*(1-j)*y[1]+j*j*y[2];

X= (int)px;
Y= (int)py;
putpixel(X,Y,WHITE);
}
//nose
line(xc,yc,xc,yc+20);
//body
line(xc,yc+r,xc,yc+250);
line(xc,yc+250,xc-80,yc+300);
line(xc,yc+250,xc+80,yc+300);
line(xc,yc+120,xc+80,yc+180);
line(xc+80,yc+180,xc+120,yc+120);
line(xc,yc+120,xc-80,yc+180);

getch();
closegraph();
}
Sun With triangles
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>

void rotation(int arr[8], int midx, int midy, int deg, int color)
{
int result[8];
double c = cos(deg * 3.14 / 180);
double s = sin(deg * 3.14 / 180);
result[0] = floor(arr[0] * c - arr[1] * s + midx * (1 - c) + midy * s);
result[1] = floor(arr[0] * s + arr[1] * c + midy * (1 - c) - midx * s);
result[2] = floor(arr[2] * c - arr[3] * s + midx * (1 - c) + midy * s);
result[3] = floor(arr[2] * s + arr[3] * c + midy * (1 - c) - midx * s);
result[4] = floor(arr[4] * c - arr[5] * s + midx * (1 - c) + midy * s);
result[5] = floor(arr[4] * s + arr[5] * c + midy * (1 - c) - midx * s);

result[6] = result[0];
result[7] = result[1];

setcolor(color);
drawpoly(4, result);
// setcolor(14);
}

void main()
{
int gd = DETECT, gm, i = 10, j, k;
double dx, dy;
int arr[] = {290, 370, 350, 370, 320, 440, 290, 370};
initgraph(&gd, &gm, "c:\\tc\\BGI");

setcolor(YELLOW);
circle(320, 240, 100);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(320, 240, YELLOW);

for (i = 0; i <= 360; i += 5)


{
for (j = 0; j <= 360; j += 45)
{
rotation(arr, 320, 240, j + i, 14);
}
delay(500);
for (j = 0; j <= 360; j += 45)
{
rotation(arr, 320, 240, j + i, 0);
}
}

getch();
closegraph();
}

Moving Boat
Twinkling Stars
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void star(int i,int j)
{
line(50+i,50+j,65+i,50+j);
line(50+i,50+j,63+i,58+j);
line(65+i,50+j,70+i,36+j);
line(70+i,36+j,75+i,50+j);
line(75+i,50+j,88+i,50+j);
line(88+i,50+j,75+i,58+j);
line(75+i,58+j,82+i,70+j);
line(82+i,70+j,69+i,62+j);
line(69+i,62+j,58+i,70+j);
line(58+i,70+j,63+i,58+j);
}
void blinkstr()
{
line(0,100,120,120);
line(120,120,240,100);
line(240,100,360,120);
line(360,120,480,100);
line(480,100,560,120);
line(560,120,640,100);
star(0,0);
star(100,-10);
star(200,20);
star(290,10);
star(370,0);
star(480,30);
star(550,25);
setfillstyle(SOLID_FILL,LIGHTRED);
floodfill(158,44,WHITE);
delay(1000);
setfillstyle(SOLID_FILL,BLACK);
floodfill(158,44,WHITE);
setfillstyle(SOLID_FILL,LIGHTRED);
floodfill(258,74,WHITE);
delay(1000);
setfillstyle(SOLID_FILL,BLACK);
floodfill(258,74,WHITE);
setfillstyle(SOLID_FILL,LIGHTRED);
floodfill(348,64,WHITE);
delay(1000);
setfillstyle(SOLID_FILL,BLACK);
floodfill(348,64,WHITE);
setfillstyle(SOLID_FILL,LIGHTRED);
floodfill(538,84,WHITE);
delay(1000);
setfillstyle(SOLID_FILL,BLACK);
floodfill(538,84,WHITE);
setfillstyle(SOLID_FILL,BLACK);
floodfill(608,79,WHITE);
setfillstyle(SOLID_FILL,LIGHTRED);
floodfill(58,54,WHITE);
delay(1000);
setfillstyle(SOLID_FILL,BLACK);
floodfill(58,54,WHITE);
setfillstyle(SOLID_FILL,LIGHTRED);
floodfill(428,54,WHITE);
}
int main()
{
int gd=DETECT,gm;
int i=0,j;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
blinkstr();
line(100,200,80,230);
line(100,200,120,230);
line(
getch();
closegraph();
return 0;
}

Shapes
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

void boundaryFill(int,int,int,int);

int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

ellipse(300,150,0,360,40,10);
boundaryFill(301,151,2,15);
setcolor(WHITE);
rectangle(265,160,325,210);
boundaryFill(266,161,1,15);

setcolor(WHITE);
line(150,250,220,250);
line(150,250,170,300);
line(220,250,200,300);
line(200,300,170,300);
boundaryFill(151,251,4,15);

delay(100);
getch();
closegraph();
}

void boundaryFill(int x,int y,int fill,int boundary)


{
int current;
current=getpixel(x,y);
if((current!=boundary) && (current!=fill))
{
delay(1);
setcolor(fill);
putpixel(x,y,fill);
boundaryFill(x+1,y,fill,boundary);
boundaryFill(x-1,y,fill,boundary);
boundaryFill(x,y+1,fill,boundary);
boundaryFill(x,y-1,fill,boundary);

}
}
Flowers
#include <stdio.h>
#include<conio.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>
int main()
{
int gd = DETECT, gm, i, r;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");

r = 400;
setbkcolor(5);
setcolor(13);
for (i = 0; i < 360; i += 30)
{
arc(320 + floor(r * cos(3.142 * i / 180)), 240 - floor(r * sin(3.142 * i /
180)), 120 + i + 30, 120 + i + 120 - 30, r);
arc(320 + ceil(r * cos(3.142 * i / 180)), 240 - ceil(r * sin(3.142 * i /
180)), 120 + i + 30, 120 + i + 120 - 30, r);
arc(320 + (r * cos(3.142 * i / 180)), 240 - (r * sin(3.142 * i / 180)), 120
+ i + 30, 120 + i + 120 - 30, r);
}
r /= 2;
for (i = 0; i < 720; i += 15)
{
setfillstyle(1, 15);
if (i % 2 == 0)
floodfill(320 + r * cos(3.142 * i / 180), 240 - r * sin(3.142 * i /
180), 13);
}

setfillstyle(1, 14);
fillellipse(320, 240, 50, 50);

getch();
closegraph();
return 0;
}

Moving Ant
#include <stdio.h>
#include <conio.h>
#include <graphics.h>

int main()
{
int gd = DETECT, gm;
int i;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
for(i = 0; i< 100;i = i + 5)
{
cleardevice();
ellipse(320+i,240,0,360,40,30);
ellipse(335+i,250,0,360,5,5);
ellipse(335+i,230,0,360,5,5);
line(280+i,240,250+i,260-i);
line(280+i,240,250+i,200+i);
line(360+i,240,390+i,200+i);
line(360+i,240,390+i,260-i);
line(320+i,270,300+i+i,285);
line(320+i,270,340+i-i,285);
getch();
}
getch();
closegraph();
return 0;
}

Moving fish
#include <stdio.h>

#include <conio.h>
#include <graphics.h>

int main()
{
int gd = DETECT,gm;
int i,j=10,cnt =5;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
for(i=0;i<100;i= i +5)
{
cleardevice();
ellipse(320+i,240,0,360,60,20);
ellipse(350+i,245,0,360,3,4);
ellipse(350+i,235,0,360,3,4);
line(320+i,280,270+i,250);
line(320+i,280,350+i,260);
line(320+i,200,270+i,230);
line(320+i,200,350+i,220);
line(260+i,240,220+i,270-i);
line(260+i,240,220+i,210+i);
line(220+i,270-i,220+i,210+i);
getch();
if( cnt != 0)
{
//cleardevice() cannot figure out how to delete the previous fins

line(320+i,280-j,270+i,250);
line(320+i,280-j,350+i,260);
line(320+i,200+j,270+i,230);
line(320+i,200+j,350+i,220);
cnt--;
getch();
}
else{
cnt= 5;
}

}
getch();
closegraph();
return 0;
}
Christmas Tree
#include <graphics.h>
#include <conio.h>
#include<dos.h>

int main(){
int gd = DETECT, gm, j, i, k;
initgraph(&gd, &gm, "c:\\TURBOC3\\BGI");
line(300, 220, 260, 280);
line(300, 220, 340, 280);
line(260, 280, 280, 280);
line(340, 280, 320, 280);
line(280, 280, 240, 340);
line(320, 280, 360, 340);
line(240, 340, 270, 340);
line(360, 340, 330, 340);
line(270, 340, 220, 420);
line(330, 340, 380, 420);
line(220, 420, 280, 420);
line(380, 420, 320, 420);
line(280, 420, 280, 450);
line(320, 420, 320, 450);
line(280, 450, 320, 450);
setfillstyle(1, GREEN);
floodfill(310, 290, WHITE);
for(i=0; i<400; i= i+150){
line(180+i, 100, 160+i, 130);
line(160+i, 130, 130+i, 130);
line(130+i, 130, 150+i, 160);
line(150+i, 160, 140+i, 190);
line(140+i, 190, 180+i, 170);
line(180+i, 170, 220+i, 190);
line(220+i, 190, 210+i, 160);
line(210+i, 160, 230+i, 130);
line(230+i, 130, 200+i, 130);
line(200+i, 130, 180+i, 100);
}
for(j=0; j<3; j++){
setfillstyle(1, RED);
floodfill(180, 110, WHITE);
floodfill(330, 110, WHITE);
floodfill(480, 110, WHITE);
delay(500);
setfillstyle(1, BLACK);
floodfill(180, 110, WHITE);
floodfill(330, 110, WHITE);
floodfill(480, 110, WHITE);
delay(500);
}
//rectangle(0, 0, 100, 100);

getch();
return 0;
}

Triangular Cone
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>

void rotateT(int arr[],int deg)


{
int midx=320;
int midy=240;
double c=cos(deg*3.14/180);
double s=sin(deg*3.14/180);
int i;
int draw[8];
for(i=0; i<8; i+=2)
{
draw[i]=floor(arr[i]*c-arr[i+1]*s+midx*(1-c)+midy*s);
draw[i+1]=floor(arr[i]*s+arr[i+1]*c+midy*(1-c)-midx*s);
}
setcolor(BLUE);
setfillstyle(SOLID_FILL, 3);
drawpoly(4,draw);
floodfill(draw[0]+5,draw[1]+1,BLUE);

}
void rotateB(int arr[],int deg)
{
int midx=320;
int midy=240;
double c=cos(deg*3.14/180);
double s=sin(deg*3.14/180);
int i;
int draw[10];
for(i=0; i<10; i+=2)
{
draw[i]=floor(arr[i]*c-arr[i+1]*s+midx*(1-c)+midy*s);
draw[i+1]=floor(arr[i]*s+arr[i+1]*c+midy*(1-c)-midx*s);
}
setcolor(BLUE);
setfillstyle(SOLID_FILL, 11);
drawpoly(5,draw);
floodfill(draw[0]+5,draw[1]+1,BLUE);

}
void scalingT(int arr[], int Sx, int Sy)
{
int i;
int draw[8];
int midx=320;
int midy=240;
for(i=0; i<8; i+=2)
{
draw[i]=arr[i]*Sx+ midx*(1-Sx)+100;
draw[i+1]=arr[i+1]*Sy+midy*(1-Sy);
}
setcolor(BLUE);
drawpoly(4,draw);

}
void scalingB(int arr[], int Sx, int Sy)
{
int i;
int draw[10];
int midx=320;
int midy=240;
for(i=0; i<10; i+=2)
{
draw[i]=arr[i]*Sx+ midx*(1-Sx)+100;
draw[i+1]=arr[i+1]*Sy+midy*(1-Sy);
}
setcolor(BLUE);
drawpoly(5,draw);

void main()
{
int gd=DETECT,gm;
int base[]={320,300,360,320,320,340,280,320,320,300};
int trR[]={320,240,360,320,320,300,320,240};
int trL[]={280,320,320,300,320,240,280,320};
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(11);
setfillstyle(SOLID_FILL, WHITE);
setcolor(WHITE);
line(320,240,320,300);
line(320,300,280,320);
line(280,320,320,240);
floodfill(321, 251, WHITE);

setfillstyle(SOLID_FILL, 3);
setcolor(3);
drawpoly(5,base);
floodfill(321,311,3);

setfillstyle(SOLID_FILL,11);
setcolor(11);
drawpoly(4,trR);
floodfill(321,251,11);

setcolor(BLUE);
line(320,240,320,300);
line(320,300,280,320);
line(280,320,320,240);
drawpoly(5,base);
drawpoly(4,trR);

getch();
//rotation
rotateT(trR,180);
rotateT(trL,180);
rotateB(base,180);
getch();
//scaling
scalingT(trR,2,2);
scalingT(trL,2,2);
scalingB(base,2,2);

getch();
closegraph();
}
Cylinder
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

void rotate(int x1,int y1,int x2,int y2,int midx,int midy,int deg,int color){
double c = cos(deg*3.14/180);
double s = sin(deg*3.14/180);
int X1 = floor(x1*c - y1*s + midx*(1-c)+midy*s);
int Y1 = floor(y1*c + x1*s + midy*(1-c)-midx*s);
int X2 = floor(x2*c - y2*s + midx*(1-c)+midy*s);
int Y2 = floor(y2*c + x2*s + midy*(1-c)-midx*s);

int shiftx = 150;


int shifty = 0;
//printf("\nShifting by %d in x and %d in y",shiftx,shifty);
setcolor(color);
line(X1+shiftx,Y1+shifty,X2+shiftx,Y2+shifty);
//line(X1,Y1,X2,Y2);
}

void ff(int x,int y,int oc,int nc){


if(getpixel(x,y)==oc){
putpixel(x,y,nc);
ff(x+1,y,oc,nc);
ff(x-1,y,oc,nc);
ff(x,y+1,oc,nc);
ff(x,y-1,oc,nc);
}
}

void main(){
int gd=DETECT,gm,midx,midy,x[10],y[10],i,oc,nc,deg;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
midx = getmaxx()/2;
midy = getmaxy()/2;
y[0] = midy-60; x[0] = midx-30;
y[1] = midy-55; x[1] = midx-40;
y[2] = midy-50; x[2] = midx-30;
y[3] = midy-50; x[3] = midx+30;
y[4] = midy-55; x[4] = midx+40;
y[5] = midy-60; x[5] = midx+30;
y[6] = midy+45; x[6] = midx-40;
y[7] = midy+50; x[7] = midx-30;
y[8] = midy+50; x[8] = midx+30;
y[9] = midy+45; x[9] = midx+40;

for(i=0;i<5;i++){ //initial cylinder drawing


line(x[i],y[i],x[i+1],y[i+1]);
if(i>=1 && i<=4){
line(x[i],y[i],x[i+5],y[i+5]);
if(i!=4) line(x[i+5],y[i+5],x[i+6],y[i+6]);
}
}
line(x[0],y[0],x[5],y[5]);

//filling color
oc = 0;
nc=9;
ff(midx,midy,oc,nc);
delay(500);
ff(midx+38,midy+43,oc,nc);
delay(500);
ff(midx-38,midy-43,oc,nc);
delay(500);
ff(midx,midy-52,oc,nc);
delay(500);

printf("enter the degree by which it should rotate");


scanf("%d",&deg);
nc=15;

for(i=0;i<5;i++){ //calling rotate function


rotate(x[i],y[i],x[i+1],y[i+1],midx,midy,deg,nc);
if(i>=1 && i<=4){
rotate(x[i],y[i],x[i+5],y[i+5],midx,midy,deg,nc);
if(i!=4) rotate(x[i+5],y[i+5],x[i+6],y[i+6],midx,midy,deg,nc);
}
}
rotate(x[0],y[0],x[5],y[5],midx,midy,deg,nc);

getch();
closegraph();
}
Cube
#include<graphics.h>
#include<conio.h>
#include<stdio.h>

int main()
{
//initilizing graphic driver and
//graphic mode variable
int graphicdriver=DETECT,graphicmode;

//calling initgraph
initgraph(&graphicdriver,&graphicmode,"C:\\Turboc3\\bgi");

//Printing message for user


outtextxy(10, 10 + 10, "Program to draw cubes of diffrent sizes in C
graphics");

//creating both the cubes using bar3d function.


setfillstyle(SOLID_FILL, GREEN);
bar3d(200, 200, 300, 300, 20, 1);

setfillstyle(SOLID_FILL, RED);
floodfill(240, 190, WHITE);

setfillstyle(SOLID_FILL, BLUE);
floodfill(310, 280, WHITE);

getch();
return 0;
}

Indian Flag
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
int main()
{
int gdriver = DETECT,gmode, a,b,i,r,x,y;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");

//draw the bottom rectangle


setfillstyle(SOLID_FILL,1);
rectangle(10,320,200,340);
floodfill(11,321,15);
rectangle(30,300,175,320);
floodfill(31,301,15);
rectangle(50,280,155,300);
floodfill(51,281,15);
//pole
setfillstyle(SOLID_FILL,3);
rectangle(100,38,110,280);
floodfill(101,39,15);
//draw the top rectangle
setfillstyle(SOLID_FILL,RED);
rectangle(110,40,220,58);
floodfill(111,43,15);
setfillstyle(SOLID_FILL,15);
rectangle(110,58,220,78);
floodfill(111,59,15);
setfillstyle(SOLID_FILL,GREEN);
rectangle(110,78,220,98);
floodfill(111,79,15);
//Ashok chakra
//
a=160;
b=68;
r=13;
setcolor(BLUE);
circle(a,b,r);
for(i=0;i<=360;i=i+25)
{
x=r*cos(i*3.14/180);
y=r*sin(i*3.14/180);
line(a,b,a+x,b-y);
}
getch();
return 0;
closegraph();
}

Traffic signal
#include <stdio.h>
#include <conio.h>

#include <graphics.h>

#include <dos.h>

#include <string.h>

int main() {

/* request auto detection */

int gdriver = DETECT, gmode, err;

int i, j, k, x, y, color;

char str[64];

/* initialize graphic mode */

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

err = graphresult();

if (err != grOk) {

/* error occurred */

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

grapherrormsg(err));

return 0;

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

/* clears graphic screen */

cleardevice();

/* draw the signal post */


setcolor(DARKGRAY);

setfillstyle(SOLID_FILL, DARKGRAY);

rectangle(50, 45, 150, 300);

rectangle(90, 300, 110, getmaxy());

floodfill(55, 55, DARKGRAY);

floodfill(91, 301, DARKGRAY);

/* make place for signals */

setcolor(BLACK);

setfillstyle(SOLID_FILL, BLACK);

circle(100, 90, 30);

floodfill(100, 90, BLACK);

circle(100, 170, 30);

floodfill(100, 170, BLACK);

circle(100, 250, 30);

floodfill(100, 250, BLACK);

x = 150 + (getmaxx() - 150) / 2;

y = 125;

moveto(x, y);

settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);

settextjustify(CENTER_TEXT, CENTER_TEXT);

/* signal for stop */

if (i == 0) {

setcolor(LIGHTRED);

outtext("STOP");

color = LIGHTRED;
y = 90;

} else if (i == 1) {

/* yellow signal - to get ready */

setcolor(YELLOW);

outtext("READY");

color = YELLOW;

y = 170;

} else {

/* green signal - to start */

setcolor(GREEN);

outtext("START");

color = GREEN;

y = 250;

x = 100;

/* for red & green timer is 10 seconds */

if (color != YELLOW) {

k = 10;

} else {

/* timer is 5 sec for yellow signal */

k = 5;

for (j = k; j > 0; j--) {

/* clear the signal-to rewrite timer */


setcolor(BLACK);

setfillstyle(SOLID_FILL, BLACK);

circle(x, y, 30);

floodfill(x, y + 25, BLACK);

/* turn on the given signal(color) */

setcolor(color);

setfillstyle(SOLID_FILL, color);

circle(x, y, 30);

floodfill(x, y + 25, color);

/* print the counter inside signal */

setcolor(BLACK);

settextstyle(TRIPLEX_FONT, HORIZ_DIR, 3);

sprintf(str, "%d", j);

moveto(x, y);

outtext(str);

sleep(1);

cleardevice();

getch();

/* deallocate memory allocated for graphic screen */

closegraph();

return 0;
}

Smiley face animations


#include<graphics.h>

#include<conio.h>

#include<stdlib.h>

main()

int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;

void *p;

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

setcolor(YELLOW);

circle(50, 100, 25);

setfillstyle(SOLID_FILL, YELLOW);

floodfill(50, 100, YELLOW); setcolor(BLACK);

setfillstyle(SOLID_FILL, RED);

fillellipse(44, 85, 2, 6);

fillellipse(56, 85, 2, 6);

ellipse(50, 100, 205, 335, 20, 9);

ellipse(50, 100, 205, 335, 20, 10);

ellipse(50, 100, 205, 335, 20, 11);

area = imagesize(left, top, left + 50, top + 50); p = malloc(area);

setcolor(WHITE);

settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 2); outtextxy(155, 451, "Smiling Face


Animation");

setcolor(BLUE);
rectangle(0, 0, 639, 449);

while(!kbhit())

temp1 = 1 + random (588); temp2 = 1 + random (380);

getimage(left, top, left + 50, top + 50, p);

putimage(left, top, p, XOR_PUT);

putimage(temp1 , temp2, p, XOR_PUT);

delay(100);

left = temp1;

top = temp2;

getch();

closegraph();

return 0;

Cricket ground
#include <conio.h>

#include <graphics.h>

#include <stdio.h>

// Driver Code

void main()

{
int gd = DETECT, gm;

// Initialize of gdriver with

// DETECT macros

initgraph(&gd, &gm, "C:\\turboc3\\bgi");

// Ground Outline

circle(700, 350, 300);

// Coloring Green

setfillstyle(SOLID_FILL, GREEN);

floodfill(402, 350, 15);


// 30 Yards Outline

ellipse(700, 350, 0, 360, 150, 200);

// Pitch Outer Line

rectangle(675, 265, 725, 435);

// Pitch Inner Line

rectangle(690, 265, 710, 435);

// Coloring Pitch Brown

setfillstyle(SOLID_FILL, BROWN);

floodfill(695, 300, 15);


// Upper Stump Line

rectangle(690, 265, 710, 280);

line(680, 280, 720, 280);

// Lower Stump Line

rectangle(690, 435, 710, 420);

line(680, 420, 720, 420);

// Hold Screen For A While

getch();

// Close the initialized gdriver

closegraph();
}

Olympic logo

#include <stdio.h>
#include <conio.h>
#include <graphics.h>

int main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
rectangle(220,50,370,430);
circle(295,240,50);
circle(295,130,50);
circle(295,350,50);

setfillstyle(1, RED);
floodfill(295,130,WHITE);
delay(1000);
setfillstyle(1, BLACK);
floodfill(295,130,WHITE);
setfillstyle(1, YELLOW);
floodfill(295,240,WHITE);
delay(1000);
setfillstyle(1, BLACK);
floodfill(295,240,WHITE);
setfillstyle(1,GREEN);
floodfill(295,350,WHITE);

getch();
closegraph();
return 0;
}

Rainbow
#include<stdio.h>

#include<graphics.h>
#include<dos.h>

// function for making of rainbow

void rainbow()

// auto detection

int gdriver = DETECT,gmode;

int x, y, i;

// initialize graphics mode(passed three arguments to

// initgraph function)

// &gdriver is the address of gdriver variable, &gmode is

// the address of gmode and

// "C:\\Turboc3\\BGI" is the directory path where BGI files are stored


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

x = getmaxx() / 2;//finding centre x-ordinate of screen

y = getmaxy() / 2;//finding centre y-ordinate of screen

for (i=30; i<200; i++)

// delay function under dos.h for holding the

// function for some time

delay(100);

// selecting color for making of rainbow

setcolor(i/10);
// making of arc with fixed centre and increasing radius

arc(x, y, 0, 180, i-10);

// driver program

int main()

rainbow();

return 0;

Moving cycle
#include <conio.h>

#include <dos.h>

#include <graphics.h>

#include <stdio.h>
// Driver code

int main()

int gd = DETECT, gm, i, a;

// Path of the program

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

// Move the cycle

for (i = 0; i < 600; i++) {

// Upper body of cycle

line(50 + i, 405, 100 + i, 405);

line(75 + i, 375, 125 + i, 375);

line(50 + i, 405, 75 + i, 375);


line(100 + i, 405, 100 + i, 345);

line(150 + i, 405, 100 + i, 345);

line(75 + i, 345, 75 + i, 370);

line(70 + i, 370, 80 + i, 370);

line(80 + i, 345, 100 + i, 345);

// Wheel

circle(150 + i, 405, 30);

circle(50 + i, 405, 30);

// Road

line(0, 436, getmaxx(), 436);


// Stone

rectangle(getmaxx() - i, 436,

650 - i, 431);

// Stop the screen for 10 secs

delay(10);

// Clear the screen

cleardevice();

getch();
// Close the graph

closegraph();

Moving Car
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

int gm,gd=DETECT;

int i= 0;

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

while(i<=800)

line(0,300,800,300); // Path

//Body of the car

line(50+i,220,100+i,220);

line(50+i,220,30+i,250);

line(100+i,220,120+i,250);

rectangle(0+i,250,160+i,270);

// Tyres of the car

circle(30+i,285,12);
circle(130+i,285,12);

if(i>=800)

break;

i=i+2;

clearviewport(); // clearing image which would make illusion of moving car

getch();

closegraph();

Concentric Circle

#include<stdio.h>
#include<graphics.h>
#include<conio.h>

void main(){
int gd = DETECT,gm;
int x ,y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(240, 50, "Concentric Circles");
setcolor(RED);
circle(x, y, 30);
setcolor(GREEN);
circle(x, y, 50);
setcolor(YELLOW);
circle(x, y, 70);
setcolor(BLUE);
circle(x, y, 90);
getch();
closegraph();
}

Rising Sun
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>

void symmetry(int xc, int yc, int x, int y, int color)


{
putpixel(xc+x,yc+y, color);
putpixel(xc+x,yc-y, color);
putpixel(xc-x,yc+y, color);
putpixel(xc-x,yc-y, color);
putpixel(xc+y,yc+x, color);
putpixel(xc+y,yc-x, color);
putpixel(xc-y,yc+x, color);
putpixel(xc-y,yc-x, color);
}
void drawCircle(int xc, int yc,int r, int color)
{
int x=0;
int y=r;
int p=1-r;
symmetry(xc,yc,x,y,color);
for(x=0; x<y; x++)
{
if(p<0)
p=p+2*x+1;
else{
p=p+2*x+1-2*y;
y--;
}
symmetry(xc,yc,x,y,color);
}
}

void main()
{
int gd=DETECT,gm,i,n=5;
int arr[]={0,300,100,250,250,390,440,200,640,400,0,300};
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

for(i=0;i<250;i+=30)
{
//Mountains
setcolor(BROWN);
setfillstyle(SOLID_FILL, BROWN);
drawpoly(n,arr);
floodfill(150,300,BROWN);
//sky
setcolor(BROWN);
setfillstyle(SOLID_FILL, CYAN);
drawpoly(n,arr);
floodfill(90,100,BROWN);

// getch();

//sun
setfillstyle(SOLID_FILL, YELLOW);
drawCircle(250,320-i,50,YELLOW);
floodfill(251,321-i,YELLOW);
getch();
//redrawing od two lines
line(100,250,250,390);
line(250,390,440,200);
setfillstyle(SOLID_FILL,CYAN);
drawCircle(250,320-i,50,CYAN);
floodfill(251,321-i,CYAN);
}
setfillstyle(SOLID_FILL, YELLOW);
drawCircle(250,320-i,50,YELLOW);
floodfill(251,321-i,YELLOW);

getch();
closegraph();
}

DDA
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void drawlinesolid(){
int x1=0,x2=200,y2=200,y1=0;
int dx, dy, xi, yi, l, x, y, i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>= abs(dy))
l=dx;
else
l = dy;
xi = dx/l;
yi = dy/l;
x=x1;
y=y1;
for(i=1;i<=l;i++)
{
putpixel(320+x,240-y,15);
x=x+xi;
y=y+yi;
}
}

void drawlinedotted(){
int x1=0,x2=200,y2=-200,y1=0;
int dx, dy, xi, yi, l, x, y, i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>= abs(dy))
l=dx;
else
l = dy;
xi = dx/l;
yi = dy/l;
x=x1;
y=y1;
for(i=1;i<=l;i++)
{
x=x+xi;
y=y+yi;
if(i%2 == 0)
putpixel(320+x,240-y,15);
}
}

void drawlinedashed(){
int x1=-200,x2=0,y2=0,y1=200;
int dx, dy, xi, yi, l, x, y, i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>= abs(dy))
l=dx;
else
l = dy;
xi = dx/l;
yi = dy/l;
x=x1;
y=y1;
for(i=1;i<=l;i++)
{
x=x+xi;
y=y+yi;
if(i%8 != 0)
putpixel(320+x, 240-y, 15);
}
}

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\TURBOC3\\bgi");
line(0, 240, 640, 240);
line(320, 0, 320, 480);

drawlinesolid();
drawlinedotted();
drawlinedashed();
getch();
}

Bresenham
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int sign(int a){
if(a>0)
return 1;
if(a<0)
return -1;
if(a==0)
return a;
return 0;
}
void drawline()
{
int x1=0, x2=200, y1=20, y2=20;
int dx,dy,p,x,y, exc, s1, s2,i;
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
s1 = sign(dx);
s2 = sign(dy);
if(dy>dx){
int temp = dx;
dx = dy;
dy = temp;
exc = 1;
}
else
exc = 0;
p=2*dy-dx;
i =1;
while(dx>i)
{
putpixel(320+x, 240-y, 15);
while(p>=0)
{
if(exc == 1){
x = x+s1;
}
else
y += s2;
p = p-2*dx;
}
if(exc == 1)
y += s2;
else
x += s1;
p = p+2*dy;
i++;
}
}

void drawdottedline()
{
int x1=-200, x2=0, y1=20, y2=20;
int dx,dy,p,x,y, exc, s1, s2,i;
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
s1 = sign(dx);
s2 = sign(dy);
if(dy>dx){
int temp = dx;
dx = dy;
dy = temp;
exc = 1;
}
else
exc = 0;
p=2*dy-dx;
i =1;
while(dx>i)
{
if(i%2 == 0)
putpixel(320+x, 240-y, 15);
while(p>=0)
{
if(exc == 1){
x = x+s1;
}
else
y += s2;
p = p-2*dx;
}
if(exc == 1)
y += s2;
else
x += s1;
p = p+2*dy;
i++;
}
}

void drawthickline()
{
int x1=-200, x2=0, y1=-20, y2=-20;
int dx,dy,p,x,y, exc, s1, s2,i;
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
s1 = sign(dx);
s2 = sign(dy);
if(dy>dx){
int temp = dx;
dx = dy;
dy = temp;
exc = 1;
}
else
exc = 0;
p=2*dy-dx;
i =1;
while(dx>i)
{
putpixel(320+x, 240-y, 15);
putpixel(321+x, 241-y, 15);
putpixel(322+x, 242-y, 15);
while(p>=0)
{
if(exc == 1){
x = x+s1;
}
else
y += s2;
p = p-2*dx;
}
if(exc == 1)
y += s2;
else
x += s1;
p = p+2*dy;
i++;
}
}

int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C://TURBOC3//bgi");
line(0, 240, 640, 240);
line(320, 0, 320, 480);
printf("Mahek Katariya C21-77");
drawline();
drawdottedline();
drawthickline();
getch();
return 0;
}

Cohen Sutherland
#include<graphics.h>

#include<conio.h>

#include<stdio.h>

#include<math.h>

void main()

int rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];

int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;

float slope;

int x,y,x1,y1,i, xc,yc;

int gr=DETECT,gm;

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

printf("\n****** Cohen Sutherlsnd Line Clipping algorithm ***********");

printf("\n Now, enter XMin, YMin =");

scanf("%d %d",&W_xmin,&W_ymin);

printf("\n First enter XMax, YMax =");

scanf("%d %d",&W_xmax,&W_ymax);

printf("\n Please enter intial point x and y= ");

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

printf("\n Now, enter final point x1 and y1= ");

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

cleardevice();

rectangle(W_xmin,W_ymin,W_xmax,W_ymax);

line(x,y,x1,y1);

line(0,0,600,0);
line(0,0,0,600);

if(y>W_ymax) {

rcode_begin[0]=1; // Top

flag=1 ;

if(y<W_ymin) {

rcode_begin[1]=1; // Bottom

flag=1;

if(x>W_xmax) {

rcode_begin[2]=1; // Right

flag=1;

if(x<W_xmin) {

rcode_begin[3]=1; //Left

flag=1;

//end point of Line

if(y1>W_ymax){

rcode_end[0]=1; // Top

flag=1;

if(y1<W_ymin) {

rcode_end[1]=1; // Bottom

flag=1;
}

if(x1>W_xmax){

rcode_end[2]=1; // Right

flag=1;

if(x1<W_xmin){

rcode_end[3]=1; //Left

flag=1;

if(flag==0)

printf("No need of clipping as it is already in window");

flag=1;

for(i=0;i<4;i++){

region_code[i]= rcode_begin[i] && rcode_end[i] ;

if(region_code[i]==1)

flag=0;

if(flag==0)

printf("\n Line is completely outside the window");

else{

slope=(float)(y1-y)/(x1-x);

if(rcode_begin[2]==0 && rcode_begin[3]==1) //left


{

y=y+(float) (W_xmin-x)*slope ;

x=W_xmin;

if(rcode_begin[2]==1 && rcode_begin[3]==0) // right

y=y+(float) (W_xmax-x)*slope ;

x=W_xmax;

if(rcode_begin[0]==1 && rcode_begin[1]==0) // top

x=x+(float) (W_ymax-y)/slope ;

y=W_ymax;

if(rcode_begin[0]==0 && rcode_begin[1]==1) // bottom

x=x+(float) (W_ymin-y)/slope ;

y=W_ymin;

// end points

if(rcode_end[2]==0 && rcode_end[3]==1) //left

{
y1=y1+(float) (W_xmin-x1)*slope ;

x1=W_xmin;

if(rcode_end[2]==1 && rcode_end[3]==0) // right

y1=y1+(float) (W_xmax-x1)*slope ;

x1=W_xmax;

if(rcode_end[0]==1 && rcode_end[1]==0) // top

x1=x1+(float) (W_ymax-y1)/slope ;

y1=W_ymax;

if(rcode_end[0]==0 && rcode_end[1]==1) // bottom

x1=x1+(float) (W_ymin-y1)/slope ;

y1=W_ymin;

delay(1000);

clearviewport();

rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(0,0,600,0);

line(0,0,0,600);

setcolor(RED);

line(x,y,x1,y1);

getch();

closegraph();

Liang Barsky
#include<stdio.h>

#include<graphics.h>

#include<math.h>

#include<dos.h>

void main()

int i,gd=DETECT,gm;

int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;

float u1,u2,a[4],b[4],temp;

initgraph(&gd,&gm,"..\\BGI");

printf("Vanshika Kaurani C21 78");

printf("enter the coordinates of line");

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

printf("enter the values of xmin,ymin and xmax,ymax");

scanf("%d %d %d %d" ,&xmin,&ymin,&xmax,&ymax);

rectangle(xmin,ymin,xmax,ymax);
setcolor(GREEN);

line(x1,y1,x2,y2);

getch();

cleardevice();

rectangle(xmin,ymin,xmax,ymax);

dx=x2-x1;

dy=y2-y1;

a[0]=-dx;

a[1]=dx;

a[2]=-dy;

a[3]=dy;

b[0]=x1-xmin;

b[1]=xmax-x1;

b[2]=y1-ymin;

b[3]=ymax-y1;

for(i=0;i<4;i++)

if(a[i]==0)

printf("line is parallel to one of the clipping boundary");

if(b[i]>=0)

if(i<2)

if(y1<ymin)

{
y1=ymin;

if(y2>ymax)

y2=ymax;

line(x1,y1,x2,y2);

if(i>1)

if(x1<xmin)

x1=xmin;

if(x2>xmax)

x2=xmax;

line(x1,y1,x2,y2);

u1=0;

u2=1;

for(i=0;i<4;i++)
{

temp=b[i]/a[i];

if(a[i]<0)

if(u1<=temp)

u1=temp;

else

if(u2>temp)

u2=temp;

if(u1<u2)

xx1 = x1 + u1 * a[1];

xx2 = x1 + u2 * a[1];

yy1 = y1 + u1 * a[3];

yy2 = y1 + u2 * a[3];

line(xx1,yy1,xx2,yy2);

getch();

delay(5000);

closegraph();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy