Kajal
Kajal
Kajal
if(p<0)
{
x=x+1;
y=y;
p=p +2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy-dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
OUTPUT:-
{
x++;
if(p<0)
p+=2 * x+1;
else
{
y--;
p+=2 * (x-y)+1;
}
putpixel(x1 +x,y1 +y,RED);
putpixel(x1 -x,y1 +y,RED);
putpixel(x1 +x,y1 -y,RED);
putpixel(x1 -x,y1 -y,RED);
putpixel(x1 +y,y1 +x,RED);
putpixel(x1 -y,y1 +x,RED);
putpixel(x1 +y,y1 -x,RED);
putpixel(x1 -y,y1 -x,RED);
}
}
OUTPUT:-
Question 3:- Write a program to show the implementation of circle drawing using
bresenhams circle drawing algorithm ?
Solution :#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void breshcir(int xc,int yc,int r);
void main()
{
int r,xc,yc,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\nEnter the x and y coorinates of the circle :");
scanf("%d%d",&xc,&yc);
printf("Enter the radius of the circle\n :");
scanf("%d",&r);
breshcir(xc,yc,r);
getch();
}
void breshcir(int xc,int yc,int r)
{
int p,x,y;
x=0;
y=r;
p=3 - 2*r;
while(x<=y)
{
putpixel(xc+x,yc-y,2);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,2);
putpixel(xc-x,yc+y,2);
putpixel(xc+y,yc-x,2);
putpixel(xc-y,yc-x,2);
putpixel(xc+y,yc+x,2);
putpixel(xc-y,yc+x,2);
If (p<0)
p=p + 4*x + 6;
else
{
p=p + 4*(x-y) +10;
y=y-1;
}
x=x+1;
}
}
OUTPUT:-
Question 4:- Write a program to show the implementation of Ellipse drawing using
Mid Point algorithm?
Solution :#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
int main(void)
{
int gd=DETECT,gm;
int cenx,ceny;
float Pk,a,b,x,y;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
cenx=getmaxx()/2;
ceny=getmaxy()/2;
Pk=b*b-b*a*a+0.25*a*a;
x=0;
y=b;
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
while (2*x*b*b <= 2*y*a*a)
{
if (Pk<0)
{
x=x+1;
y=y;
Pk=Pk+2*x*b*b+3*b*b;
}
else
{
x=x+1;
y=y-1;
Pk=Pk+2*x*b*b+3*b*b-2*y*a*a+2*a*a;
}
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
delay(40);
}
Pk=(x+0.5)*(x+0.5)*b*b+(y-1)*(y-1)*a*a-a*a*b*b;
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
while (y>0)
{
if (Pk>0)
{
x=x;
y=y-1;
Pk=Pk-2*y*a*a+3*a*a;
}
else
{
x=x+1;
y=y-1;
Pk=Pk-2*y*a*a+3*a*a+2*x*b*b+2*b*b;
}
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
delay(40);
}
gotoxy(1,25);
printf ("\npress any key to exit.");
getch();
closegraph();
return 0;
}
OUTPUT:
cleardevice();
switch(ch)
{
case 1: translate();
break;
case 2: scale();
break;
case 3: rotate();
break;
default: printf("you have enterd wrong choice");
break;
}
getch();
closegraph();
}
void translate()
{
int tx,ty;
setcolor(2);
outtextxy(240,10,"TRANSLATION");
outtextxy(238,20,"------------");
printf("\nEnter tx: ");
scanf("%d",&tx);
printf("\nEnter ty: ");
scanf("%d",&ty);
cleardevice();
rectangle(100,150,150,100);
printf("\nAfter Translation");
rectangle(100+tx,150+ty,150+tx,100+ty);
}
void scale()
{
int sx,sy;
setcolor(2);
outtextxy(240,10,"SCALING");
outtextxy(238,20,"--------");
printf("\nEnter sx: ");
scanf("%d",&sx);
printf("\nEnter sy: ");
scanf("%d",&sy);
cleardevice();
rectangle(100,150,150,100);
printf("\nAfter Scaling");
rectangle(100*sx,150*sy,150*sx,100*sy);
}
void rotate()
{
float theta;
int x1,x2,x3,x4;
int y1,y2,y3,y4;
int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;
int refx,refy;
printf("\nEnter the angle for rotation: ");
scanf("%f",&theta);
theta=theta*(3.14/180);
cleardevice();
setcolor(2);
outtextxy(240,10,"ROTATE");
outtextxy(238,20,"-------");
refx=100;
refy=100;
x1=100;
y1=100;
x2=150;
y2=100;
x3=150;
y3=150;
x4=100;
y4=150;
ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);
ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);
ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);
ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);
ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);
ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);
ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);
rectangle(100,150,150,100);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax4,ay4);
line(ax4,ay4,ax1,ay1);
}
OUTPUT:
(B)
#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp(int n,float c[][3])
{
float maxx,maxy;
int i;
maxx=getmaxx();
maxy=getmaxy();
maxx=maxx/2;
maxy=maxy/2;
i=0;
while(i<n-1)
{
line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]);
i++;
}
i=n-1;
line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]);
setcolor(GREEN);
line(0,maxy,maxx*2,maxy);
line(maxx,0,maxx,maxy*2);
setcolor(WHITE);
}
void mul(int n,float b[][3],float c[][3],float a[][3])
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
a[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
a[i][j] = a[i][j] + (c[i][k] * b[k][j]);
}
}
void reflection(int n,float c[][3])
{
float b[10][3],a[10][3];
int i=0,ch,j;
cleardevice();
printf("\n\t* * MENU * *");
printf("\n\t1) ABOUT X-AXIS");
printf("n\t2) ABOUT Y-AXIS");
printf("\n\t3) ABOUT ORIGIN");
printf("\n\t4) ABOUT X=Y");
break;
case 4:
b[0][0]=0;
b[1][1]=0;
b[0][1]=1;
b[1][0]=1;
break;
case 5:
b[0][0]=0;
b[1][1]=0;
b[0][1]=-1;
b[1][0]=-1;
break;
case 6:
break;
default:
printf("\n\tINVALID CHOICE ! ");
break;
}
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void shearing(int n,float c[][3])
{
float b[10][3],sh,a[10][3];
int i=0,ch,j;
cleardevice();
printf("\n\t* * * MENU * * *");
printf("\n\t1) X SHEARING");
printf("\n\t2) Y SHEARING");
printf("\n\t3) EXIT ");
printf("\n\tENTER YOUR CHOICE : ");
scanf("%d",&ch);
if(ch==3)
return;
printf("\n\tENTER THE VALUE for SHEARING:
scanf("%f",&sh);
clrscr();
cleardevice();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
for(i=0;i<3;i++)
b[i][i]=1;
switch(ch)
{
case 1:
b[1][0]=sh;
break;
");
case 2:
b[0][1]=sh;
break;
case 3:
break;
default:
printf("\n\tINVALID CHOICE ! ");
break;
}
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void main()
{
int i,j,k,cho,n,gd=DETECT,gm;
float c[10][3],tx,ty,sx,sy,ra;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\nEnter the number of vertices : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the co-ordinates of the %d vertex :",i+1);
scanf("%f%f",&c[i][0],&c[i][1]);
c[i][2]=1;
}
do
{
clrscr();
cleardevice();
printf("\n\t\t\t * * * MENU * * *");
printf("\n\t 1) REFLECTION ");
printf("\n\t 2) SHEARING");
printf("\n\t 3) EXIT");
printf("\n\t ENTER YOUR CHOICE: ");
scanf("%d",&cho);
switch(cho)
{
case 1:
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
reflection(n,c);
getch();
break;
case 2:
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
shearing(n,c);
getch();
break;
case 3 :
exit(0);
break;
default:
printf("\n\tInvalid choice !!");
break;
}
}
while(cho!=3);
getch();
closegraph();
}
OUTPUT:-
pixels[0][3]=1;
if(y2>ymax)
pixels[1][0]=1;
if(y2<ymin)
pixels[1][1]=1;
if(x2>xmax)
pixels[1][2]=1;
if(x2<xmin)
pixels[1][3]=1;
for(j=0;j<4;j++)
{
if(pixels[0][j]==0&&pixels[1][j]==0)
continue;
if(pixels[0][j]==1&&pixels[1][j]==1)
{
fl=3;
break;
}
fl=2;
}
switch(fl)
{
case 1:
line(320+x1,240-y1,320+x2,240-y2);
break;
case 3:
printf("\n\n\a\ Line Is Not Visible:");
break;
case 2:
m=(y2-y1)/(x2-x1);
xn1=x1;
yn1=y1;
xn2=x2;
yn2=y2;
if(pixels[0][0]==1)
{
xn1=x1+(ymax-y1)/m;
yn1=ymax;
}
if(pixels[0][1]==1)
{
xn1=x1+(ymin-y1)/m;
yn1=ymin;
}
if(pixels[0][2]==1)
{
yn1=y1+(xmax-x1)*m;
xn1=xmax;
}
if(pixels[0][3]==1)
{
yn1=y1+(xmin-x1)*m;
xn1=xmin;
}
if(pixels[1][0]==1)
{
xn2=x2+(ymax-y2)/m;
yn2=ymax;
}
if(pixels[1][1]==1)
{
xn2=x2+(ymin-y2)/m;
yn2=ymin;
}
if(pixels[1][2]==1)
{
yn2=y2+(xmax-x2)*m;
xn2=xmax;
}
if(pixels[1][3]==1)
{
yn2=y2+(xmin-x2)*m;
xn2=xmin;
}
line(320+xn1,240-yn1,320+xn2,240-yn2);
break;
}
}
void cohen()
{
clearviewport();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
line (320+x1,240-y1,320+x2,240-y2);
getch();
cleardevice();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
su_co(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
}
void main()
{
int gd=DETECT,gm,i,j;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n\n\t\t\ Enter The Co-Ordinates Of The Clipping Window");
OUTPUT :-
printf("\n 1.TRANSLATION");
printf("\n 2.SCALING");
printf("\n 3.ROTATION");printf("\n\n Enter your choice(1/2/3)");
scanf("%d",&ch);
switch(ch)
{
case 1:
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("\Enter the translation factor");
scanf("%d%d",&x,&y);
axis();
printf("After translation");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
//draw a bar
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("Enter the rotation angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);
x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After rotating about Z-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,10,1);
axis();
printf("After rotating about x-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1); //draw a bar
bar3d(midx+100,midy-x1,midx+60,midy-x2,10,1);
axis();
printf("After rotating about Y-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1); //draw a bar
bar3d(midx+x1,midy-150,midx+x2,midy-100,10,1); //draw a bar
}
getch();
closegraph();
}
OUTPUT:-
getch();
closegraph();
return;
}
void main()
{
int x[4], y[4];
int i;
OUTPUT :-
void fan(int p)
{
int xf,yf;
int x1,y1,x2,y2,x3,y3,x4,y4;
int j=0,i;
float t;
setcolor(12);
xf=150; //Center of circle
yf=150;
t=3.14/180;
while(j==0 )
{
for(;;i++)
{
cleardevice();
circle(150,150,25);
circle(150,150,1);
line(150,126,150,100);
x1=xf+(175-xf)*cos(120*t+i)-(130-yf)*sin(120*t+i);
y1=yf+(175-xf)*sin(120*t+i)+(130-yf)*cos(120*t+i);
x2=xf+(275-xf)*cos(120*t+i)-(130-yf)*sin(120*t+i);
y2=yf+(275-xf)*sin(120*t+i)+(130-yf)*cos(120*t+i);
x3=xf+(175-xf)*cos(120*t+i)-(150-yf)*sin(120*t+i);
y3=yf+(175-xf)*sin(120*t+i)+(150-yf)*cos(120*t+i);
x4=xf+(275-xf)*cos(120*t+i)-(150-yf)*sin(120*t+i);
y4=yf+(275-xf)*sin(120*t+i)+(150-yf)*cos(120*t+i);
line(x1,y1,x2,y2);
line(x2,y2,x4,y4);
line(x4,y4,x3,y3);
line(x3,y3,x1,y1);
x1=xf+(175-xf)*cos(240*t+i)-(130-yf)*sin(240*t+i);
y1=yf+(175-xf)*sin(240*t+i)+(130-yf)*cos(240*t+i);
x2=xf+(275-xf)*cos(240*t+i)-(130-yf)*sin(240*t+i);
y2=yf+(275-xf)*sin(240*t+i)+(130-yf)*cos(240*t+i);
x3=xf+(175-xf)*cos(240*t+i)-(150-yf)*sin(240*t+i);
y3=yf+(175-xf)*sin(240*t+i)+(150-yf)*cos(240*t+i);
x4=xf+(275-xf)*cos(240*t+i)-(150-yf)*sin(240*t+i);
y4=yf+(275-xf)*sin(240*t+i)+(150-yf)*cos(240*t+i);
line(x1,y1,x2,y2);
line(x2,y2,x4,y4);
line(x4,y4,x3,y3);
line(x3,y3,x1,y1);
x1=xf+(175-xf)*cos(360*t+i)-(130-yf)*sin(360*t+i);
y1=yf+(175-xf)*sin(360*t+i)+(130-yf)*cos(360*t+i);
x2=xf+(275-xf)*cos(360*t+i)-(130-yf)*sin(360*t+i);
y2=yf+(275-xf)*sin(360*t+i)+(130-yf)*cos(360*t+i);
x4=xf+(175-xf)*cos(360*t+i)-(150-yf)*sin(360*t+i);
y4=yf+(175-xf)*sin(360*t+i)+(150-yf)*cos(360*t+i);
x3=xf+(275-xf)*cos(360*t+i)-(150-yf)*sin(360*t+i);
y3=yf+(275-xf)*sin(360*t+i)+(150-yf)*cos(360*t+i);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x4,y4);
line(x4,y4,x1,y1);
delay(p);
}
}
}
OUTPUT:-
ellipse(265,123,0,360,13,20);//right eye
ellipse(210,133,0,360,10,10);//left eye ball
ellipse(265,133,0,360,10,10);//right eye ball
ellipse(210,133,0,360,3,3);//left eye ball
ellipse(265,133,0,360,3,3);//right eye ball
ellipse(238,160,0,360,10,13);//nose
arc(240,125,228,312,68);//mouth
arc(240,120,230,310,72);//mouth
setfillstyle(1,4);
floodfill(238,160,15);//nose
setfillstyle(1,15);
floodfill(210,113,15);
floodfill(265,113,15);
setfillstyle(1,9);
floodfill(210,100,15);
setfillstyle(1,1);
floodfill(315,80,15);
moveto(203,220);
lineto(203,260);
lineto(183,260);
lineto(183,350);
lineto(293,350);
lineto(293,260);
lineto(273,260);
lineto(273,220);
moveto(183,350);
lineto(173,460);
lineto(213,460);
lineto(238,400);
lineto(263,460);
lineto(303,460);
lineto(293,350);
moveto(173,460);
lineto(143,478);
lineto(213,478);
lineto(213,460);
moveto(263,460);
lineto(263,478);
lineto(333,478);
lineto(303,460);
line(238,400,238,350);
//right hand
moveto(183,260);
lineto(113,310);
lineto(183,375);
moveto(183,280);
lineto(137,310);
lineto(181,353);
setfillstyle(2,13);
floodfill(190,300,15);
setfillstyle(1,5);
floodfill(223,400,15);
setfillstyle(1,5);
floodfill(253,400,15);
setfillstyle(1,6);
floodfill(173,470,15);
floodfill(303,470,15);
//fingers
secondleft();
ellipse(413.5,228,0,180,3.5,3.5);
line(420,240,433,240);
line(423,247,440,247);
line(413,240,410,228);
line(417,228,420,240);
ellipse(433,243.5,-90,90,3.5,3.5);
line(423,254,440,254);
ellipse(440,250.5,-90,90,3.5,3.5);
ellipse(430,257,-90,90,3,3);
line(413,260,430,260);
area=imagesize(409,224,444,261);
buf=malloc(area);
getimage(409,224,444,261,buf);
while(!kbhit())
{
if(i==0)
{
setfillstyle(1,15);
setcolor(15);
ellipse(210,133,0,360,10,10);//left eye ball
ellipse(265,133,0,360,10,10);//right eye ball
setcolor(0);
ellipse(210,133,0,360,3,3);//left eye ball
ellipse(265,133,0,360,3,3);//right eye ball
floodfill(210,133,15);
floodfill(265,133,15);
setcolor(0);
putimage(391,209,buf,1);
firstleft();
setcolor(15);
secondleft();
putimage(409,224,buf,0);
i=1;
}
else
{
setfillstyle(1,0);
setcolor(0);
ellipse(210,133,0,360,10,10);//left eye ball
ellipse(265,133,0,360,10,10);//right eye ball
floodfill(210,133,0);
floodfill(265,133,0);
setcolor(15);
ellipse(210,133,0,360,3,3);//left eye ball
ellipse(265,133,0,360,3,3);//right eye ball
floodfill(210,133,15);
floodfill(265,133,15);
setcolor(0);
putimage(409,224,buf,1);
secondleft();
setcolor(15);
firstleft();
putimage(391,209,buf,0);
i=0;
}
delay(300);
}
getch();
}
void firstleft()
{
moveto(293,260);
lineto(353,276);
lineto(395,223);
moveto(293,280);
lineto(355,296);
lineto(395,245);
}
void secondleft()
{
moveto(293,260);
lineto(363,280);
lineto(413,240);
moveto(293,280);
lineto(363,300);
lineto(413,260);
}
OUTPUT :-