0% found this document useful (0 votes)
74 views

Computer Graphic CC Programs

The document contains 7 C programming examples related to computer graphics and image processing algorithms: 1. Programs to draw lines using DDA and Bresenham's line drawing algorithms. 2. A program to draw a circle using the midpoint circle algorithm. 3. A program demonstrating window to viewport transformation. 4. A program implementing the Cohen-Sutherland line clipping algorithm. 5. A program to draw a Bezier curve. 6. A program to shear a cuboid by drawing its faces. 7. A program to draw a polygon and perform rotation, translation and scaling transformations.

Uploaded by

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

Computer Graphic CC Programs

The document contains 7 C programming examples related to computer graphics and image processing algorithms: 1. Programs to draw lines using DDA and Bresenham's line drawing algorithms. 2. A program to draw a circle using the midpoint circle algorithm. 3. A program demonstrating window to viewport transformation. 4. A program implementing the Cohen-Sutherland line clipping algorithm. 5. A program to draw a Bezier curve. 6. A program to shear a cuboid by drawing its faces. 7. A program to draw a polygon and perform rotation, translation and scaling transformations.

Uploaded by

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

Program 1

WRITE A C PROGRAM TO DRAW LINE BY USING DDA AND BRESENHAM'S


ALGORITHM.
# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
void dda(float x1,float y1,float x2,float y2)
{
float dx,dy,x=x1,y=y1,m;
int i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>=abs(dy))
m=abs(dx);
else
m=abs(dy);
putpixel((int)x,(int)y,15);
for(i=1;i<=m;i++)
{
x=x+dx/m;
y=y+dy/m;
putpixel((int)x,(int)y,15);
}
}
void bress(float x1,float y1,float x2,float y2)
{
int x,y,end,inc=0,p,dx=abs(x2-x1);
int dy=abs(y2-y1),c=0,current=0;
if(dx>dy)
{
p=2*dy-dx;
if(x1<x2)
{
x=x1;
y=y1;
end=x2;
if(y1<y2)
inc=1;

if(y1>y2)
inc=-1;
}
else
{
x=x2;
y=y2;
end=x1;
if(y2<y1)
inc=1;
if(y2<y1)
inc=-1;
}
while(x<=end)
{
putpixel(x,y,15);
if(p<0)
p=p+2*dy;
else
{
y=y+inc;
p=p+2*(dy-dx);
}
x++;
if(current==0 && c==10)
{
current=1;
c=-1;
}
if(current==1 && c==6)
{
current=0;
c=-1;
}
c++;
}
}
}
void main()
{
float x1,x2,y1,y2;
int ch;
int gd=DETECT,gm=DETECT;

initgraph(&gd,&gm," ");
printf("ENTER THE END POINTS OF LINE (x1,y1,x2,y2): ");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
printf("CHOOSE ALGORITHM: ");
printf("\n 1-DDA");
printf("\n 2-BRESENHAM");
printf("\n YOUR CHOICE IS: ");
scanf("%d",&ch);
if(ch==1)
dda(x1,y1,x2,y2);
if(ch==2)
bress(x1,y1,x2,y2);
getch();
closegraph();
}

OUTPUT-1

Program 2
WRITE A C PROGRAM TO DRAW MIDPOINT CIRCLE ALGORITHM.
# include<stdio.h>
# include<graphics.h>
# include<conio.h>
# include<math.h>
void bresenham_circle(const int h,const int k,const int r)
{
int x=0,y=r,p=(3-(2*r));
//
cleardevice();
line(320,1,320,480);
line(1,240,640,240);
do
{
delay(15);
putpixel((h+x),(k+y),25);
putpixel((h+y),(k+x),15);
putpixel((h+y),(k-x),25);
putpixel((h+x),(k-y),15);
putpixel((h-x),(k-y),25);
putpixel((h-y),(k-x),15);
putpixel((h-y),(k+x),25);
putpixel((h-x),(k+y),15);
x++;
if(p<0)
p+=((4*x)+6);
else
{
y--;
p+=((4*(x-y))+10);
}
}
while(x<=y);
}
void main(void)
{
int driver=VGA,mode=VGAHI,h,k,r;

initgraph(& driver,& mode,"c:\tc\bgi");


printf("\n ENTER THE VALUE OF [H-COORDINATE]: ");
scanf("%d",&h);
printf("\n ENTER THE VALUE OF [K-COORDINATE]: ");
scanf("%d",&k);
printf("\n ENTER THE VALUE OF THE RADIUS: ");
scanf("%d",&r);
bresenham_circle(320+h,240-k,r);
do
{
r--;
bresenham_circle(320+h,240-k,r);
}while(r!=0);
getche();
}

OUTPUT-2

Program 3
WRITE A C PROGRAM OF WINDOW TO VIEW PORT.
# include<conio.h>
# include<stdio.h>
# include<graphics.h>
void image();
float wxmin,wymin,wxmax,wymax;
float vxmin,vymin,vxmax,vymax;
void main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc");
printf("\n\t ENTER THE COORDINATES OF WINDOW: \n");
printf("\n\t wxmin,wymin: ");
scanf("%f%f",&wxmax,&wymax);
printf("\n\t ENTER THE COORDINATES OF VIEW: \n");
printf("\n\t vxmin,vymin: ");
scanf("%f%f",&vxmin,&vymin);
printf("\n\t vxmax,vymax: ");
scanf("%f%f",&vxmax,&vymax);
rectangle(wxmin,wymin,wxmax,wymax);
rectangle(vxmin,vymin,vxmax,vymax);
getch();
cleardevice();
image();
getch();
}
void image()
{
float x1,y1,x2,y2,vx1,vx2,vy1,vy2;
clrscr();
printf("\n\n\t ENTER THE COORDINATES OF LINE: ");
printf("\n\n\t X1 Y1: ");
scanf("%f%f",&x1,&y1);
printf("\n\n\t X2 Y2: ");
scanf("%f%f",&x2,&y2);

rectangle(wxmin,wymin,wxmax,wymax);
rectangle(vxmin,vymin,vxmax,vymax);
line(x1,y1,x2,y2);
vx1=((vxmax-vxmin)/(wxmax-wxmin))*(x1-wxmin)+vxmin;
vy1=((vxmax-vxmin)/(wxmax-wxmin))*(y1-wxmin)+vxmin;
vx2=((vxmax-vxmin)/(wxmax-wxmin))*(x2-wxmin)+vxmin;
vy2=((vxmax-vxmin)/(wxmax-wxmin))*(y2-wxmin)+vxmin;
line(vx1,vy1,vx2,vy2);
}

OUTPUT-3

10

Program 4
WRITE A C PROGRAM OF COHEN-SUTHERLAND ALGORITHM.
#include<stdio.h>
#include<graphics.h>
typedef unsigned int outcode;
enum {
TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };
void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{
int gd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;
code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do{
if(!(code0 | code1))
{
accept =1 ; done =1; }
else
if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else
if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else

11

if ( codeout & RIGHT)


{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);
if(accept) line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
}
/*--------------------------------------------------------------------*/
int calcode (x,y,xwmin,ywmin,xwmax,ywmax)
float x,y,xwmin,ywmin,xwmax,ywmax;
{
int code =0;
if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;
else if ( x< xwmin)

12

code |= LEFT;
return(code);
}
/*-------------------------------------------------*/
void main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
cleardevice();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();
}

13

OUTPUT-4

14

Program 5
WRITE A C PROGRAM TO DRAW A BEZIER CURVE.
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<math.h>
int *x,*y;
void bezier(int *x1,int *y1)
{
int xa,ya,za;
float u=0;
moveto(*x1,*y1);
while (u<=1)
{
xa=(*(x1+3)*pow(u,3))+(3*(*(x1+2))*pow(u,2)*(1-u))+
(3*(*(x1+1))*u*pow(1-u,2))+(*x1*pow(1-u,3));
ya=(*(y1+3)*pow(u,3))+(3*(*(y1+2))*pow(u,2)*(1-u))+
(3*(*(y1+1))*u*pow(1-u,2))+(*y1*pow(1-u,3));
lineto(xa, ya);
u += 0.01;
}
}
void main(void)
{
int gd=DETECT, gm, key, xpos, ypos, i = 0;
char xp[4], yp[4];
x = (int *)malloc(sizeof(int) * 4);
y = (int *)malloc(sizeof(int) * 4);
//registerbgidriver(EGAVGA_driver);
initgraph(&gd, &gm, "c:\\tc\\bgi");
outtextxy(10, 460, "Press Esc to exit.");
outtextxy(400, 460, "Present position : ");
moveto(getmaxx() / 2, getmaxy() / 2);
do
{

15

xpos = getx();
ypos = gety();
itoa(xpos, xp, 10);
itoa(ypos,yp,10);
setviewport(550,460,639,479,1);
clearviewport();
outtextxy(0,0,xp);
outtextxy(40,0,yp);
setviewport(0,0,639,479,1);
moveto(xpos,ypos);
key=getch();
if (key==0) key=getch();
switch(key)
{
case 72 : ypos--;
//up arrow
moveto(xpos,ypos);
break;
case 80 : ypos++;
//down arrow
moveto(xpos,ypos);
break;
case 75 : xpos--;
//left arrow
moveto(xpos,ypos);
break;
case 77 : xpos++;
//right arrow
moveto(xpos,ypos);
break;
case 87 :
case 119 : ypos-=50;
moveto(xpos,ypos);
break;
case 65 :
case 97 : xpos-=50;
moveto(xpos,ypos);
break;
case 83 :
case 115 :xpos+=50;
moveto(xpos,ypos);
break;
case 90 :
case 122 :ypos+=50;
moveto(xpos,ypos);
break;
case 13 : putpixel(xpos,ypos,15);
*(x+i)=xpos;

16

*(y+i)=ypos;
i++;
break;
}
if (i==4)
{
bezier(x,y);
i=0;
}
}
while(key!=27);
closegraph();
getch();
}

17

Output-5

18

Program 6
WRITE A C PROGRAM TO SHEAR A CUBOID.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void bress(float x1,float y1, float x2,float y2)
{
int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0;
if ( dx > dy )
{
p=2*dy-dx;
if(x1<x2)
{
x=x1;
y=y1;
end=x2;
if(y1<y2)
inc=1;
if(y1>y2)
inc=-1;
}
else
{
x=x2;
y=y2;
end=x1;
if(y2<y1)
inc=1;
if(y2>y1)
inc=-1;
}
while(x<=end)
{
putpixel(x,y,15);
if(p<0)
p=p+2*dy;
else
{

19

y=y+inc;
p=p+2*(dy-dx);
}
x++;
if(current==0 && c==10)
{
current=1;
c=-1;
}
if(current==1 && c==6)
{
current=0;
c=-1;
}
c++;
}
}
else
{
p=2*dx-dy;
if(y1<y2)
{
x=x1;
y=y1;
end=y2;
if(x1<x2)
inc=1;
if(x1>x2)
inc=-1;
}
else
{
x=x2;
y=y2;
end=y1;
if(x2<x1)
inc=1;
if(x2>x1)
inc=-1;
}
while(y<=end)
{
putpixel(x,y,15);
if(p<0)

20

p=p+2*dx;
else
{
x=x+inc;
p=p+2*(dx-dy);
}
y++;
if(current==0 && c==10)
{
current=1;
c=-1;
}
if(current==1 && c==6)
{
current=0;
c=-1;
}
c++;
}
}
}
void main()
{
float x1,x2,a,b,c,d,y1,y2;
double ch;
int t=30;
int gd=DETECT,gm=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter the end pts of the line(x,y)");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
bress(x1,y1,x2,y1);
bress(x2,y1,x2,y2);
bress(x1,y2,x2,y2);
bress(x1,y2,x1,y1);
bress(x1+t,y1+t,x2+t,y1+t);
bress(x2+t,y1+t,x2+t,y2+t);
bress(x1+t,y2+t,x2+t,y2+t);
bress(x1+t,y2+t,x1+t,y1+t);
bress(x1,y1,x1+t,y1+t);
bress(x2,y1,x2+t,y1+t);

21

bress(x1,y2,x1+t,y2+t);
bress(x2,y2,x2+t,y2+t);
/*
a=x1+(10*y1);
b=x2+(10*y1);
c=(10*x1)+y1;
d=(10*x2)+y1;*/
bress(x1+50,y1+40,x2+50,y1+40);
bress(x1+t,y2+t,x1+50,y1+40);
bress(x2+t,y2+t,x2+50,y1+40);
bress(x1+50,y1+40,x1,y1);
bress(x2+50,y1+40,x2,y1);
getch();
closegraph();
}

22

OUTPUT-6

23

Program 7
WRITE A C PROGRAM TO DRAW A POLYGON AND PERFORM THE FOLLOWING
OPERATIONS:ROTATION
TRANSLATION
SCALING.
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
int *x,*y,i,nin;
float x1,y1,theta;
void drawpolygon(int *x,int *y)
{
int gd=DETECT,gm,ch=0,x1,y1,theta;
//registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,"");
for (i=0;i<(nin-1);i++)
line(*(x+i),*(y+i),*(x+i+1),*(y+i+1));
line(*(x+nin-1),*(y+nin-1),*x,*y);
getch();
closegraph();
}
void translate(float x1,float y1)
{
for (i=0;i<nin;i++)
{
*(x+i)+=x1;
*(y+i)+=y1;
}
}
void scale(float x1,float y1)
{
int a,b;
a=*x;

24

b=*y;
translate(-a,-b);
for (i=0;i<nin;i++)
{
*(x+i)*=x1;
*(y+i)*=y1;
}
translate(a,b);
}
void rotate(float theta)
{
int a,b,c,d;
c=*x;
d=*y;
translate(-c,-d);
for (i=0;i<nin;i++)
{
a=(*(x+i)*cos(theta))-(*(y+i)*sin(theta));
b=(*(x+i)*sin(theta))+(*(y+i)*cos(theta));
*(x+i)=a;
*(y+i)=b;
}
translate(c,d);
}
void main(void)
{
int ch;
x=(int *)malloc(sizeof(int)*10);
y=(int *)malloc(sizeof(int)*10);
clrscr();
printf("ENTER NUMBER OF SIDES IN POLYGON : ");
scanf("%d",&nin);
printf("ENTER THE COORDINATES OF THE VERTICES (x,y) :\n");
for (i=0;i<nin;i++)
{
printf("(i+1) : ");
scanf("%d%d",&(*(x+i)),&(*(y+i)));
}
drawpolygon(x,y);
while (ch!=4)
{
printf("YOUR OPTIONS :\n");

25

printf("1)TRANSLATE\n");
printf("2)SCALE\n");
printf("3)ROTATE\n");
printf("4)EXIT\n");
printf("\nYOUR CHOICE : ");
scanf("%d",&ch);
clrscr();
switch(ch)
{
case 1:printf("TRANSLATION IN X-DIRECTION : ");
scanf("%d",&x1);
printf("TRANSLATION IN Y-DIRECTION : ");
scanf("%d",&y1);
translate(x1,y1);
drawpolygon(x,y);
break;
case 2:printf("SCALING IN X-DIRECTION : ");
scanf("%f",&x1);
printf("SCALING IN Y-DIRECTION : ");
scanf("%f",&y1);
scale(x1,y1);
drawpolygon(x,y);
break;
case 3:printf("ANGLE OF ROTATION(ANTI-CLOCKWISE IS POSITIVE) : ");
scanf("%f",&theta);
theta*=3.1415/180;
rotate(-theta);
drawpolygon(x,y);
break;
case 4:exit(0);
}
}
}

26

Output-7

After translation

After scaling

27

After Rotation

28

Program 8
WRITE A C PROGRAM TO DRAW A RECTANGLE BY USING BRESENHAM AND DDA
ALGORITHM.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void dda(float x1,float y1,float x2,float y2)
{
float dx,dy,x=x1,y=y1,m;
int i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>=abs(dy))
m=abs(dx);
else m=abs(dy);
putpixel((int)x,(int)y,15);
for(i=1;i<m;i++)
{
x=x+dx/m;
y=y+dy/m;
putpixel((int)x,(int)y,15);
}
}
void bress(float x1,float y1,float x2,float y2)
{
int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0;
if(dx>dy)
{

p=2*dy-dx;
if(x1<x2)
{

x=x1;y=y1;end=x2;
}

if(y1<y2)
inc=1;
if(y1>y2)
inc=-1;
}

else
{

x=x2;y=y2;end=x1;
if(y2<y1)

29

inc=1;
if(y2>y1)
inc=-1;
}

while(x<=end)
{

putpixel(x,y,15);
if(p<0)
p=p+2*dy;
else
{

y=y+inc;p=p+2*(dy-dx);
}

x++;
if(current==0&&c==10)
{

current=1;
c=-1;
}

if(current==1&&c==6)
{

current=0;
c=-1;
}

c++;
}
}

void main()
{

float x1,x2,y1,y2,x3,y3,x4,y4;
int ch;
int gdriver=DETECT, gmode=DETECT;
initgraph(&gdriver, &gmode, "c:\\tc\\BGI");
printf("Enter end points of line (x1,y1,x2,y2)");
scanf("%f %f %f %f %f %f %f %f",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
printf("Choose algorithm(1-DDA 2-BRESENHAM)");
scanf("%d",&ch);
bress(x1,y1,x2,y2);
dda(x2,y2,x3,y3);
bress(x4,y4,x3,y3);
dda(x4,y4,x1,y1);
getch();
closegraph();
}

30

OUTPUT-8

31

Program 9
WRITE A C PROGRAM TO DRAW A SOLID AND FIND ITS VANISHING POINT.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void createsolid();
void main()
{
int gd = DETECT,gm = DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
createsolid();
getch();
closegraph();
}
void createsolid()
{
//The coordinates of first rectangle
line(250,200,370,200);
line(370,200,370,320);
line(370,320,250,320);
line(250,200,250,320);
//The coordinates of scond rectangle
line(280,150,400,150);
line(400,150,400,270);
line(400,270,280,270);
line(280,150,280,270);
//The coordinates of the lines
line(250,200,280,150);
line(370,200,400,150);
line(370,320,400,270);
line(250,320,280,270);
//The coordinates of the X,Y and Z axis
line(320,300,600,300);
line(320,50,320,300);
line(320,300,170,450);
//Coordinates for vanishing point
putpixel(190,430,RED);
}

32

OUTPUT-9

33

Program 10
WRITE A C PROGRAM TO TRANSLATE A LINE BY USING DDA ALGORITHM.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void dda(float x1,float y1,float x2,float y2)
{
float dx,dy,x=x1,y=y1,m;
int i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>=abs(dy))
m=abs(dx);
else
m=abs(dy);
putpixel((int)x,(int)y,15);
for(i=1;i<=m;i++)
{ x=x+dx/m;
y=y+dy/m;
putpixel((int)x,(int)y,15);
}
}
void main()
{
float x11,x12,y11,y12, x21, x22, y21, y22;
float nw;
int gd=DETECT,gm=DETECT;
initgraph(&gd,&gm,"c:\tc\bin");
printf("enter endpoints of line(x1,y1,x2,y2)");
scanf("%f%f%f%f",&x11,&x12,&y11,&y12);
dda(x11,x12,y11,y12);
printf("Please enter new position");
scanf("%f",&nw);
x21=x11+nw;
y21=y11+nw;
x22=x12+nw;
y22=y12+nw;
dda(x21,x22,y21,y22);
getch();
closegraph();
}

34

OUTPUT-10

35

Program 11
WRITE A C PROGRAM TO ROTATE A TRIANGLE.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void bress(float x1,float y1, float x2,float y2)
{
int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0;
if ( dx > dy )
{
p=2*dy-dx;
if(x1<x2)
{
x=x1;
y=y1;
end=x2;
if(y1<y2)
inc=1;
if(y1>y2)
inc=-1;
}
else
{
x=x2;
y=y2;
end=x1;
if(y2<y1)
inc=1;
if(y2>y1)
inc=-1;
}
while(x<=end)
{
putpixel(x,y,15);
if(p<0)
p=p+2*dy;
else
{
y=y+inc;
p=p+2*(dy-dx);

36

}
x++;
if(current==0 && c==10)
{
current=1;
c=-1;
}
if(current==1 && c==6)
{
current=0;
c=-1;
}
c++;
}
}
else
{
p=2*dx-dy;
if(y1<y2)
{
x=x1;
y=y1;
end=y2;
if(x1<x2)
inc=1;
if(x1>x2)
inc=-1;
}
else
{
x=x2;
y=y2;
end=y1;
if(x2<x1)
inc=1;
if(x2>x1)
inc=-1;
}
while(y<=end)
{
putpixel(x,y,15);
if(p<0)
p=p+2*dx;
else

37

{
x=x+inc;
p=p+2*(dx-dy);
}
y++;
if(current==0 && c==10)
{
current=1;
c=-1;
}
if(current==1 && c==6)
{
current=0;
c=-1;
}
c++;
}
}
}
void main()
{
float a[3][3],b[3][3],c[3][3],i,j,k;
float r=(3.14*45)/180;
int gd=DETECT,gm=DETECT;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
//IMAGE(triangle)
bress(0,0,100,100);
bress(100,100,500,200);
bress(500,200,0,0);
a[0][0]=0;
a[0][1]=0;
a[0][2]=1;
a[1][0]=100;
a[1][1]=100;
a[1][2]=1;
a[2][0]=500;
a[2][1]=200;
a[2][2]=1;
b[0][0]=cos(r);

38

b[0][1]=sin(r);
b[0][2]=0;
b[1][0]=-sin(r);
b[1][1]=cos(r);
b[1][2]=0;
b[2][0]=0;
b[2][1]=0;
b[2][2]=1;
//MATRIX MULTIPLICATION
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}

//AFTER ROTATION
line(c[0][0],c[0][1],c[1][0],c[1][1]);
line(c[1][0],c[1][1],c[2][0],c[2][1]);
line(c[2][0],c[2][1],c[0][0] ,c[0][1]);
getch();
}

39

OUTPUT-11

40

Program 12
WRITE A C PROGRAM TO DRAW BEZIER SURFACE.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm, x1,x2,x3,x4,y1,y2,y3,y4,y11,y22,y33,y44,i;
gd=DETECT,gm=DETECT;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
x1=100;x2=130;x3=170;x4=200;y1=150;y2=16 ;y3=160;x4=165;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x4,y4);
y11=y1;
y22=y2;
y33=y3;
y44=y4;
for(i=0;i<5;i++)
{
y11=y11+10;
y22=y22+10;
y33=y33+10;
y44=y44+10;
line(x1,y11,x2,y22);
line(x2,y22,x3,y33);
line(x3,y33,x4,y44);
}
line(x1,y1,x1,y11);
line(x2,y2,x2,y22);
line(x3,y3,x3,y33);
line(x4,y4,x4,y44);
getch();
closegraph();
}

41

OUTPUT-12

42

Program 13
WRITE A C PROGRAM TO CONVERT WINDOW COORDINATES IN TO VIEW PORT.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void bress(float x1,float y1, float x2,float y2)
{
int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0;
if ( dx > dy )
{
p=2*dy-dx;
if(x1<x2)
{
x=x1;
y=y1;
end=x2;
if(y1<y2)
inc=1;
if(y1>y2)
inc=-1;
}
else
{
x=x2;
y=y2;
end=x1;
if(y2<y1)
inc=1;
if(y2>y1)
inc=-1;
}
while(x<=end)
{
putpixel(x,y,15);
if(p<0)
p=p+2*dy;
else
{
y=y+inc;
p=p+2*(dy-dx);

43

}
x++;
if(current==0 && c==10)
{
current=1;
c=-1;
}
if(current==1 && c==6)
{
current=0;
c=-1;
}
c++;
}
}
else
{
p=2*dx-dy;
if(y1<y2)
{
x=x1;
y=y1;
end=y2;
if(x1<x2)
inc=1;
if(x1>x2)
inc=-1;
}
else
{
x=x2;
y=y2;
end=y1;
if(x2<x1)
inc=1;
if(x2>x1)
inc=-1;
}
while(y<=end)
{
putpixel(x,y,15);
if(p<0)
p=p+2*dx;
else

44

{
x=x+inc;
p=p+2*(dx-dy);
}
y++;
if(current==0 && c==10)
{
current=1;
c=-1;
}
if(current==1 && c==6)
{
current=0;
c=-1;
}
c++;
}
}
}
void main()
{
float x1,x2,x3,x4,y1,y2,y3,y4;
double ch;
int t;
float vx,vy,vx1,vx2,vy2;
int gd=DETECT,gm=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
outtextxy(250,190,"World coordinates");
bress(200,200,400,200);
bress(200,200,200,400);
bress(400,200,400,400);
bress(200,400,400,400);
outtextxy(23,10,"View port");
bress(20,20,100,20);
bress(20,20,20,100);
bress(100,20,100,100);
bress(20,100,100,100);
//IMAGE(triangle)
bress(250,250,350,250);

45

bress(250,250,300,300);
bress(350,250,300,300);
//TRANSFORMATION
vx=(((100-20)*(250-200))/(400-200))+20;
vy=(((100-20)*(250-200))/(400-200))+20;
vx1=(((100-20)*(350-200))/(400-200))+20;
vx2=(((100-20)*(300-200))/(400-200))+20;
vy2=(((100-20)*(300-200))/(400-200))+20;
//TRANSFORMED IMAGE
bress(vx,vy,vx1,vy);
bress(vx,vy,vx2,vy2);
bress(vx1,vy,vx2,vy2);
getch();
}

46

OUTPUT-13

47

You might also like