CG Prac QB
CG Prac QB
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
// Driver Code
int main()
// are stored
setcolor(YELLOW);
// creating circle and fill it with
setfillstyle(SOLID_FILL, YELLOW);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
getch();
// 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");
scanf("%d %d",&x_mid,&y_mid);
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>
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)
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);
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 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);
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();
}
}
}
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 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);
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;
//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);
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");
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");
Traffic signal
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <string.h>
int main() {
int i, j, k, x, y, color;
char str[64];
err = graphresult();
if (err != grOk) {
/* error occurred */
grapherrormsg(err));
return 0;
cleardevice();
setfillstyle(SOLID_FILL, DARKGRAY);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
y = 125;
moveto(x, y);
settextjustify(CENTER_TEXT, CENTER_TEXT);
if (i == 0) {
setcolor(LIGHTRED);
outtext("STOP");
color = LIGHTRED;
y = 90;
} else if (i == 1) {
setcolor(YELLOW);
outtext("READY");
color = YELLOW;
y = 170;
} else {
setcolor(GREEN);
outtext("START");
color = GREEN;
y = 250;
x = 100;
if (color != YELLOW) {
k = 10;
} else {
k = 5;
setfillstyle(SOLID_FILL, BLACK);
circle(x, y, 30);
setcolor(color);
setfillstyle(SOLID_FILL, color);
circle(x, y, 30);
setcolor(BLACK);
moveto(x, y);
outtext(str);
sleep(1);
cleardevice();
getch();
closegraph();
return 0;
}
#include<conio.h>
#include<stdlib.h>
main()
int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
void *p;
setcolor(YELLOW);
setfillstyle(SOLID_FILL, YELLOW);
setfillstyle(SOLID_FILL, RED);
setcolor(WHITE);
setcolor(BLUE);
rectangle(0, 0, 639, 449);
while(!kbhit())
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;
// DETECT macros
// Ground Outline
// Coloring Green
setfillstyle(SOLID_FILL, GREEN);
setfillstyle(SOLID_FILL, BROWN);
getch();
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>
void rainbow()
// auto detection
int x, y, i;
// initgraph function)
delay(100);
setcolor(i/10);
// making of arc with fixed centre and increasing radius
// 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()
// Wheel
// Road
rectangle(getmaxx() - i, 436,
650 - i, 431);
delay(10);
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
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);
circle(30+i,285,12);
circle(130+i,285,12);
if(i>=800)
break;
i=i+2;
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 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 gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
scanf("%d %d",&W_xmin,&W_ymin);
scanf("%d %d",&W_xmax,&W_ymax);
scanf("%d %d",&x,&y);
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;
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)
flag=1;
for(i=0;i<4;i++){
if(region_code[i]==1)
flag=0;
if(flag==0)
else{
slope=(float)(y1-y)/(x1-x);
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;
// end points
{
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;
x1=x1+(float) (W_ymax-y1)/slope ;
y1=W_ymax;
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");
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)
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();