Sei sulla pagina 1di 49

Ashish M.

Gautam Application Id: 14131000229


1

Program No: 1

Aim: Write a C Program to implement the DDA Line Drawing Algorithm to draw a line.

Program:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int xa, ya, xb, yb, step, dx, dy;
float xi, yi, x, y;
int cx, cy;
void getdata();
void getdata()
{
clrscr();
printf("Enter Starting Co-Ordinate : ");
scanf("%d",&xa);
scanf("%d",&ya);

printf("Enter Ending Co-Ordinate : ");
scanf("%d",&xb);
scanf("%d",&yb);
}

void dispdata()
{
int i;
dx = xb - xa;
dy = yb - ya;
if (abs(dx) > abs(dy))
step = abs(dx);
else
step = abs(dy);
xi = (float)dx/step;
yi = (float)dy/step;
x = xa;
y = ya;
cx = x + 0.5;
cy= y+0.5;
putpixel(cx,cy,BLUE);
for(i=1; i<=step; i++)
{
x = x + xi;
y = y + yi;
cx = x+0.5;
Ashish M. Gautam Application Id: 14131000229
2

cy= y+0.5;
putpixel(cx,cy,BLUE);
}
getch();
}

void main()
{
int gd=DETECT, gm, errorcode;
clrscr();
getdata();
clrscr();

initgraph(&gd, &gm, "D:\\TC\\BGI");

errorcode = graphresult();

if (errorcode != grOk)
{
printf("Graphics Error:");
printf(grapherrormsg(errorcode));
printf("\n");
printf("Press any key to halt");
getch();
exit(1);
}
dispdata();
}

Output:

Enter Starting Co-Ordinate: 100
100
Enter Ending Co-Ordinate: 200
200

Ashish M. Gautam Application Id: 14131000229
3

Program No: 2

Aim: Write a C Program to implement the DDA Line rasterisation of two end points.

Program:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>

int xa, ya, xb, yb, step, dx, dy;
float xi, yi, x, y;
int cx, cy;
void getdata();

void getdata()
{
clrscr();
printf("Enter Starting Co-Ordinate : ");
scanf("%d",&xa);
scanf("%d",&ya);

printf("Enter Ending Co-Ordinate : ");
scanf("%d",&xb);
scanf("%d",&yb);
}


void dispdata()
{
int i;
dx = xb - xa;
dy = yb - ya;
if (abs(dx) > abs(dy))
step = abs(dx);
else
step = abs(dy);

printf("\n\tstep = %d",step);


xi = (float)dx/step;
yi = (float)dy/step;
printf("\n\txi = %.3f",xi);
printf("\n\tyi = %.3f",yi);


Ashish M. Gautam Application Id: 14131000229
4

x = xa;
y = ya;


printf("\n\n");
printf("i \t Plotted(x,y) \t x=(x+xi) \t y=(y+yi)");
printf("\n==========================================\n");
cx = x+0.5; //round to nearest integer value
cy= y+0.5;
printf("0 \t ( %d, %d)",cx,cy);

for(i=1; i<=step; i++)
{
x = x + xi;
y = y + yi;
printf("\t\t %.3f \t\t %.3f ", x, y);

printf("\n\n");
cx = x+0.5;
cy= y+0.5;
printf("%d \t ( %d, %d)",i,cx,cy);
}

getch();
}

void main()
{
clrscr();
getdata();
clrscr();
dispdata();
}













Ashish M. Gautam Application Id: 14131000229
5

Output:




Ashish M. Gautam Application Id: 14131000229
6

Program No: 3

Aim: Write a C Program to Implement the Bresenhams Line Algorithm to draw a line.

Program:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int xa, ya, xb, yb;
int dx,dy,sx,sy, x, y;
int steps, flag, p;

void getdata()
{
clrscr();
printf("Enter Starting Co-Ordinate : ");
scanf("%d",&xa);
scanf("%d",&ya);

printf("Enter Ending Co-Ordinate : ");
scanf("%d",&xb);
scanf("%d",&yb);
}

void dispdata()
{
int i,t;

dx = abs(xb-xa);
dy = abs(yb-ya);

if ((xb-xa) > 0)
sx = +1;
else if((xb-xa)==0)
sx = 0;
else
sx = -1;

if ((yb-ya) > 0)
sy = +1;
else if((yb-ya)==0)
sy = 0;
else
sy = -1;
Ashish M. Gautam Application Id: 14131000229
7

x = xa;
y = ya;

if (dy > dx)
{
t = dx;
dx = dy;
dy = t;
flag = 0;
}
else
{
flag = 1;
}

steps = dx;
p = 2*dy - dx;
putpixel(x,y,BLUE);


for(i=0; i<=steps; i++)
{
if(flag == 1)
{
x = x+sx;
if (p<0)
p = p + 2*dy;
else
{
y = y +sy;
p = p + 2* (dy-dx);
}
putpixel(x,y,BLUE);
}
if(flag == 0)
{
y = y+sy;
if (p<0)
p = p + 2*dy;
else
{
x = x +sx;
p = p + 2* (dy-dx);
}
putpixel(x,y,BLUE);
}
Ashish M. Gautam Application Id: 14131000229
8

}

getch();
}

void main()
{
int gd=DETECT, gm, errorcode;
clrscr();
getdata();
clrscr();

initgraph(&gd, &gm, "D:\\TC\\BGI");
errorcode = graphresult();

if (errorcode != grOk)
{
printf("Graphics Error:");
printf(grapherrormsg(errorcode));
printf("\n");
printf("Press any key to halt");
getch();
exit(1);
}
dispdata();
}

Output:


Ashish M. Gautam Application Id: 14131000229
9

Program No: 4

Aim: Write a C Program to Implement the Midpoint Circle Drawing Algorithm to draw a
circle.

Program:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int xc, yc, r, x, y;
float p;
void dispcircle(int,int,int,int);

void getdata()
{
clrscr();
printf("Enter the center of the circle : ");
scanf("%d %d", &xc,&yc);
printf("Enter the radius of the circle : ");
scanf("%d",&r);
getch();
}

void dispdata()
{
x = 0;
y = r;
p = 1.25 - r;
dispcircle(xc,yc,x,y);
while(x < y)
{
x = x + 1;
if ( p < 0)
p = p + 2*x + 1;
else
{
y = y - 1;
p = p + 2*x - 2*y + 1;
}
dispcircle(xc,yc,x,y);
}
getch();
}

Ashish M. Gautam Application Id: 14131000229
10

void dispcircle(int xc, int yc,int x,int y)
{
putpixel(xc+x,yc+y,BLUE);
putpixel(xc+x,yc-y,BLUE);
putpixel(xc-x,yc+y,BLUE);
putpixel(xc-x,yc-y,BLUE);
putpixel(xc+y,yc+x,BLUE);
putpixel(xc+y,yc-x,BLUE);
putpixel(xc-y,yc+x,BLUE);
putpixel(xc-y,yc-x,BLUE);
}

void main()
{
int gd=DETECT, gm, errorcode;
clrscr();
getdata();
clrscr();
initgraph(&gd, &gm, "C:\\TC\\BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics Error:");
printf(grapherrormsg(errorcode));
printf("\n");
printf("Press any key to halt");
getch();
exit(1);
}
dispdata();
closegraph();
}

Output:

Ashish M. Gautam Application Id: 14131000229
11

Program No: 5

Aim: Write a C Program to implement the rasterization of Midpoint Circle Drawing
Algorithm.

Program:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>

int xc, yc, r, x, y, p;

void getdata()
{
clrscr();
printf("Enter the center of the circle : ");
scanf("%d %d", &xc,&yc);
printf("Enter the radius of the circle : ");
scanf("%d",&r);
getch();
clrscr();
}

void dispdata()
{
int k = 0;
x = 0;
y = r;
p = 1 - r;
printf("Initial Co-ordiante at Origin is : (%d, %d) ", x, y);
printf("\nInitial Decision Parameter = %d", p);

printf("\n\n\tK \t (x,y) \t (xc+x, yc+y) 2x \t 2y \t Pk");
printf("\n\t 0 \t (%d,%d) \t (%d,%d) \t %d \t %d \t %d",x,y,xc+x,yc+y,2*x,2*y,p);

while(x < y)
{
x = x + 1;
if ( p < 0)
p = p + 2*x + 1;
else
{
y = y - 1;
p = p + 2*x - 2*y + 1;
}
k++;
Ashish M. Gautam Application Id: 14131000229
12

printf("\n\t %d \t (%d,%d) \t (%d,%d) \t %d \t %d \t %d",k,x,y,xc+x,yc+y,2*x,2*y,p);
}
getch();
}

void main()
{
clrscr();
getdata();
dispdata();
}

Output:


Ashish M. Gautam Application Id: 14131000229
13

Program No: 6

Aim: Write a C Program to implement the drawing of Ellipse Algorithm.

Program:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
main()
{
long d1,d2;
int i,gd,gm,x,y;
long rx, ry, rxsq,rysq,tworxsq,tworysq,dx,dy;

printf("Enter the x radius of the ellipse :");
scanf("%ld",&rx);
printf("Enter the y radius of the ellipse :");
scanf("%ld",&ry);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");

rxsq = rx * rx;
rysq = ry * ry;
tworxsq = 2 * rxsq;
tworysq = 2 * rysq;
x = 0;
y = ry;
d1 = rysq - rxsq * ry + (0.25 * rxsq) ;
dx = tworysq * x;
dy = tworxsq * y;

do
{
putpixel(200+x,200+y,15);
putpixel(200-x,200-y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);

if (d1 < 0)
{
x = x + 1;
y = y;
dx = dx + tworysq;
d1 = d1 + dx + rysq;
}
else
Ashish M. Gautam Application Id: 14131000229
14

{
x = x + 1;
y = y - 1;
dx = dx + tworysq;
dy = dy - tworxsq;
d1 = d1 + dx - dy + rysq;
}
delay(10);
}while( dx < dy);

d2 = rysq* (x+0.5) * (x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq ;
do
{
putpixel(200+x,200+y,15);
putpixel(200-x,200-y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);
if(d2 > 0)
{
x = x;
y = y -1;
dy = dy -tworxsq;
d2 = d2 - dy + rxsq;
}
else
{
x = x+1;
y = y -1;
dy = dy -tworxsq;
dx = dx + tworysq;
d2 = d2 + dx - dy + rxsq;
}
}while (y > 0);

getch();
closegraph();
}

Output:

Enter the x radius of the ellipse: 26
Enter the y radius of the ellipse: 16



Ashish M. Gautam Application Id: 14131000229
15

Program No: 7

Aim: Write a C Program to implement the flood fill Algorithm.

Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flood(int,int,int,int);

void main()
{
int gd,gm=DETECT;
int x, y, fillcolor=12, oldcolor=0;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");

rectangle(50,50,100,100);
x=y=55;
flood(x,y,fillcolor,oldcolor);
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);
}
}

Output:



Ashish M. Gautam Application Id: 14131000229
16

Program No: 8

Aim: Write a C Program to Implement the Boundary filling Algorithm.

Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void boundary_fill(int,int,int,int);

void main()
{
int gd,gm=DETECT;
int x, y, fillcolor=12, boundarycolor=15;
detectgraph(&gd,&gm);
initgraph(&gd, &gm, "d:\\tc\\bgi");
rectangle(50,50,100,100);
getch();
x=y=55;
boundary_fill(x,y,fillcolor,boundarycolor);
getch();
}


void boundary_fill(int x, int y, int fcolor, int bcolor)
{
if ((getpixel(x, y) != fcolor) && (getpixel(x, y) != bcolor))
{
delay(10);
putpixel(x, y, fcolor);
boundary_fill(x+1, y, fcolor, bcolor);
boundary_fill(x-1, y, fcolor, bcolor);
boundary_fill(x, y+1, fcolor, bcolor);
boundary_fill(x, y-1, fcolor, bcolor);
}
}

Output:



Ashish M. Gautam Application Id: 14131000229
17

Program No: 9

Aim: Write a C Program for the Implementation of 2D-Transformations-Translation,
Rotation and Scaling.

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

int ch,x,y,az,i,w,ch1,ch2,xa,ya,ra,a[10],b[10],da,db;
float x1,y1,az1,w1,dx,dy,theta,x1s,y1s,sx,sy,a1[10],b1[10];

void main()
{
int gm ,gr;
clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"c:\\tc\\BGI");
printf("Enter the upper left corner of the rectangle:\n");
scanf("%d%d",&x,&y);
printf("Enter the lower right corner of the rectangle:\n");
scanf("%d%d",&az,&w);
rectangle(x,y,az,w);
da=az-x;
db=w-y;
a[0]=x;
b[0]=y;
a[1]=x+da;
b[1]=y;
a[2]=x+da;
b[2]=y+db;
a[3]=x;b[3]=y+db;
printf("%d %d",x,y);

while(1)
{
printf("******2D Transformations*******\n");

printf("1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing\n6.Exit\nEnter
your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
Ashish M. Gautam Application Id: 14131000229
18

detectgraph(&gm,&gr);
initgraph(&gm,&gr,"c:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Translation*******\n\n");
printf("Enter the value of shift vector:\n");
scanf("%f%f",&dx,&dy);
x1=x+dx;
y1=y+dy;
az1=az+dx;
w1=w+dy;
rectangle(x1,y1,az1,w1);
break;

case 2:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"c:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Rotation*******\n\n");
printf("Enter the value of fixed point and angle of rotation:Enter the value
of fixed point and angle of rotation:\n");
scanf("%d%d%d",&xa,&ya,&ra);
theta=(float)(ra*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;

case 3:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"c:\\tc\\BGI");
rectangle(x,y,az,w);
printf("********Scaling*******\n\n");
printf("Enter the value of scaling factor:\n");
scanf("%f%f",&sx,&sy);
x1=x*sx;
y1=y*sy;
az1=az*sx;
Ashish M. Gautam Application Id: 14131000229
19

w1=w*sy;
rectangle(x1,y1,az1,w1);
break;

case 4:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"c:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Reflection*********\n");
printf("1.About x-axis\n2.About y-axis\n3.About both axis\nEnter your
choice:\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(90*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;

case 2:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(270*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((-a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((-a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
Ashish M. Gautam Application Id: 14131000229
20

line(a1[i],b1[i],a1[0],b1[0]);
}
break;

case 3:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(180*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((-a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));
b1[i]=(ya+((-a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
}
break;

case 5:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"c:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Shearing******\n\n");
printf("1.x-direction shear\n2.y-direction shear\nEnter your choice:\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
printf("Enter the value of shear:\n");
scanf("%f",&x1s);
x1=x+(y*x1s);
y1=y;
az1=az+(w*x1s);
w1=w;
rectangle(x1,y1,az1,w1);
break;

case 2:
printf("Enter the value of shear:\n");
scanf("%f",&y1s);
Ashish M. Gautam Application Id: 14131000229
21

x1=x;
y1=y+(x*y1s);
az1=az;
w1=w+(az*y1s);
rectangle(x1,y1,az1,w1);
break;
}
break;

case 6:
exit(0);
}
}
getch();
}

Output:

Enter the upper left corner of the rectangle:
100
100
Enter the lower right corner of the rectangle:
200
200


******2DTransformations*******
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit

Enter your choice: 1

*******Translation******* Enter the value of shift vector:
Ashish M. Gautam Application Id: 14131000229
22

150
150



******2DTransformations*******
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit
Enter your choice: 2
*******Rotation*******
Enter the value of fixed point and angle of rotation:
300
300
70


Ashish M. Gautam Application Id: 14131000229
23

Program No: 10

Aim: Write a C Program to rotate a small circle about an arbitrary center point.

Program:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<dos.h>

int xc=50,yc=200,r=35;
int x[15],y[15];

void drawcircles()
{
setcolor(YELLOW);
circle(xc,yc,r);
circle(xc,yc,r+5);
}

void main()
{
double angle=0,theta;
int i,a;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\tc\\bgi");
a=xc+r;

while(!kbhit())
{
while(a<=630)
{
theta=M_PI*angle/180;
cleardevice();
drawcircles();

for(i=0;i<18;i++)
{
theta=M_PI*angle/180;
x[i]=xc+r*cos(theta);
y[i]=yc+r*sin(theta);
angle+=20;
line(xc,yc,x[i],y[i]);
}

Ashish M. Gautam Application Id: 14131000229
24

angle+=2; xc+=2; a=xc+r;
delay(50);
}
xc=50; r=35; a=xc+r;
}
getch();
closegraph();
}

Output:








Ashish M. Gautam Application Id: 14131000229
25

Program No: 11

Aim: Write a C Program for the character generation.

Program:
#include<stdio.h>
#include<graphics.h>
main()
{
int gd,gm,i,j;
// Save character map of letter A
int a[13][9] = {
{ 0, 0, 0, 0, 1, 0, 0, 0, 0},
{ 0, 0, 0, 1, 0, 1, 0, 0, 0},
{ 0, 0, 1, 0, 0, 0, 1, 0, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 1, 1, 1, 1, 1, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
};
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
for(i=0;i<13;i++)
{
for(j=0;j<9;j++)
{
putpixel(200+j,200+i,15*a[i][j]);
}
}
getch();
closegraph();
}

Output:


Ashish M. Gautam Application Id: 14131000229
26

Program No: 12

Aim: Write a C program for the Implementation of Bezier Curve Algorithm.

Program:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
int gd,gm,maxx,maxy;
float xxx[4][2];

void line1(float x2,float y2)
{
line(xxx[0][0],xxx[0][1],x2,y2);
xxx[0][0]=x2;
xxx[0][1]=y2;
}

bezier(float xb,float yb,float xc,float yc,float xd,float yd,int n)
{
float xab,yab,xbc,ybc,xcd,ycd;
float xabc,yabc,xbcd,ybcd;
float xabcd,yabcd;
if (n==0)
{
line1(xb,yb);
line1(xc,yc);
line1(xd,yd);
}
else
{
xab = (xxx[0][0]+xb)/2;
yab = (xxx[0][1]+yb)/2;
xbc = (xb+xc)/2;
ybc = (yb+yc)/2;
xcd = (xc+xd)/2;
ycd = (yc+yd)/2;
xabc = (xab+xbc)/2;
yabc = (yab+ybc)/2;
xbcd = (xbc+xcd)/2;
ybcd = (ybc+ycd)/2;
xabcd = (xabc+xbcd)/2;
yabcd = (yabc+ybcd)/2;
n=n-1;
bezier(xab,yab,xabc,yabc,xabcd,yabcd,n);
bezier(xbcd,ybcd,xcd,ycd,xd,yd,n);
Ashish M. Gautam Application Id: 14131000229
27

}
}

void igraph()
{
detectgraph(&gd,&gm);
if(gd<0)
{
puts("CANNOT DETECT A GRAPHICS CARD");
exit(1);
}
initgraph(&gd,&gm,"C:\\TC\\BGI");
}

main()
{
int i;
float temp1,temp2;
igraph();
for(i=0;i<4;i++)
{
printf("Enter (x,y) coordinates of point%d : ",i+1);
scanf("%f %f",&temp1,&temp2);
xxx[i][0] = temp1;
xxx[i][1] = temp2;
}
clrscr();
bezier(xxx[1][0],xxx[1][1],xxx[2][0],xxx[2][1],xxx[3][0],xxx[3][1],8);
getch();
closegraph();
}

Output:

Enter (x,y) coordinates of point 1: 10 10
Enter (x,y) coordinates of point 1: 20 30
Enter (x,y) coordinates of point 1: 40 30
Enter (x,y) coordinates of point 1: 60 40




Ashish M. Gautam Application Id: 14131000229
28

Program No: 13

Aim: Write a C program for the Implementation of Sutherland and Cohen subdivision line
clipping Algorithm.

Program:
#include<stdio.h>
#include<graphics.h>
#include<math.h>

typedef struct
{
float x;
float y;
}PT;

int n;

main()
{
int i,j,gd,gm;
PT d,p1,p2,p[20],pi1,pi2,pp[20];
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("Enter coordinates (left,top) of point1 : ");
scanf("%f %f",&p1.x,&p1.y);
printf("Enter coordinates (right,bottom) of point2 : ");
scanf("%f %f",&p2.x,&p2.y);

printf("Enter the number of vertex : ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Enter coordinates of vertex%d : ",i+1);
scanf("%f %f",&p[i].x,&p[i].y);
}
p[i].x = p[0].x;
p[i].y = p[0].y;
cleardevice();
drawpolygon(p,n);
rectangle(p1.x,p1.y,p2.x,p2.y);
getch();
left(p1,p,pp);
Ashish M. Gautam Application Id: 14131000229
29

right(p2,p,pp);
top(p1,p,pp);
bottom(p2,p,pp);
cleardevice();
rectangle(p1.x,p1.y,p2.x,p2.y);
drawpolygon(p,n);
getch();
closegraph();
}

left(PT p1,PT p[20],PT pp[20])
{
int i,j=0;
for(i=0;i<n;i++)
{
if(p[i].x < p1.x && p[i+1].x >= p1.x)
{
if(p[i+1].x-p[i].x!=0)
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p1.x-p[i].x)+p[i].y;
else
pp[j].y = p[i].y;
pp[j].x = p1.x;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}

if(p[i].x > p1.x && p[i+1].x >= p1.x)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}

if(p[i].x > p1.x && p[i+1].x <= p1.x)
{
if(p[i+1].x-p[i].x!=0)
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p1.x-p[i].x)+p[i].y;
else
pp[j].y = p[i].y;
pp[j].x = p1.x;
j++;
}
}
for(i=0;i<j;i++)
Ashish M. Gautam Application Id: 14131000229
30

{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
n=j;
}

right(PT p2,PT p[20],PT pp[20])
{
int i,j=0;
for(i=0;i<n;i++)
{
if(p[i].x > p2.x && p[i+1].x <= p2.x)
{
if(p[i+1].x-p[i].x!=0)
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p2.x-p[i].x)+p[i].y;
else
pp[j].y = p[i].y;
pp[j].x = p2.x;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].x < p2.x && p[i+1].x <= p2.x)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}
if(p[i].x < p2.x && p[i+1].x >= p2.x)
{
if(p[i+1].x-p[i].x!=0)
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p2.x-p[i].x)+p[i].y;
else
pp[j].y = p[i].y;
pp[j].x = p2.x;
j++;
}
}
for(i=0;i<j;i++)
{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
Ashish M. Gautam Application Id: 14131000229
31

}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
n=j;
}

top(PT p1,PT p[20],PT pp[20])
{
int i,j=0;
for(i=0;i<n;i++)
{
if(p[i].y < p1.y && p[i+1].y >= p1.y)
{
if(p[i+1].y-p[i].y!=0)
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p1.y-p[i].y)+p[i].x;
else
pp[j].x = p[i].x;
pp[j].y = p1.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].y > p1.y && p[i+1].y >= p1.y)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}
if(p[i].y > p1.y && p[i+1].y <= p1.y)
{
if(p[i+1].y-p[i].y!=0)
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p1.y-p[i].y)+p[i].x;
else
pp[j].x = p[i].x;
pp[j].y = p1.y;
j++;
}
}
for(i=0;i<j;i++)
{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
Ashish M. Gautam Application Id: 14131000229
32

n=j;
}

bottom(PT p2,PT p[20],PT pp[20])
{
int i,j=0;
for(i=0;i<n;i++)
{
if(p[i].y > p2.y && p[i+1].y <= p2.y)
{
if(p[i+1].y-p[i].y!=0)
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p2.y-p[i].y)+p[i].x;
else
pp[j].x = p[i].x;
pp[j].y = p2.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].y < p2.y && p[i+1].y <= p2.y)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}
if(p[i].y < p2.y && p[i+1].y >= p2.y)
{
if(p[i+1].y-p[i].y!=0)
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p2.y-p[i].y)+p[i].x;
else
pp[j].x = p[i].x;
pp[j].y = p2.y;
j++;
}
}
for(i=0;i<j;i++)
{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
n=j;
}

Ashish M. Gautam Application Id: 14131000229
33

drawpolygon(PT x[20],int n)
{
int i;
for(i=0;i<n-1;i++)
{
line(x[i].x,x[i].y,x[i+1].x,x[i+1].y);
}
line(x[i].x,x[i].y,x[0].x,x[0].y);
}


Output:

Enter coordinates (left,top) of point1 : 100 200
Enter coordinates (right,bottom) of point2 : 300 400
Enter the number of vertex : 2
Enter coordinates of vertex 1: 100 150
Enter coordinates of vertex 2: 300 350





















Ashish M. Gautam Application Id: 14131000229
34

Program No: 14

Aim: Write a C program for Drawing Smile Using Fractals.

Program:
#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:\\TC\\BGI");
setcolor(YELLOW);
circle(50,100,25);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(50,100,YELLOW);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
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;
Ashish M. Gautam Application Id: 14131000229
35

top = temp2;
}

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

Output:




Ashish M. Gautam Application Id: 14131000229
36

Program No: 15

Aim: Write a C Program Generate a 2D/3D fractal figures.

Program:

Sierpinski Triangle:
#include <stdio.h>
#include <conio.h>
#include <stdlig.h>
#include <math.h>
#include <graphics.h>

void DrawSierpinski(void);

void main(void)
{
int gd=VGA;
int gm=VGAHI;
initgraph(&gd, &gm, "\\tc\\bgi");
DrawSierpinski();
getch();
}

void DrawSierpinski(void)
{
char Direct;
int iterate;
unsigned int x1, y1, x2, y2;

x1 = x2 = 320;
y1 = y2 = 0;

for(iterate = 0; iterate < 10000; iterate++)
{
Direct = random(3);
if(Direct == 0)
{
x1 = (x2 + 320) / 2;
y1 = (y2 + 0) / 2;
}
else if(Direct == 1)
{
x1 = (x2 + 0) / 2;
y1 = (y2 + 480) / 2;
}
Ashish M. Gautam Application Id: 14131000229
37

else if(Direct == 2)
{
x1 = (x2 + 640) / 2;
y1 = (y2 + 480) / 2;
}
putpixel(x1, y1, WHITE);
x2 = x1;
y2 = y1;
}
}

Cantor set:
#include <stdio.h>
#include "stdlib.h"
#include "math.h"
#include <graphics.h>
#include <sys/types.h>
#include <time.h>

float (*(createALine)(int x1,int y1,int x2,int y2,float (*previousArrLine)[2],int *arrLength))[2]
{
int q,i,k=0,no,n;
int no_of_divisions=3;
float (*arrLine)[2];
float temp_x1,temp_x2,diff,next_temp_x1,next_temp_x2,equal_part;

no=*arrLength;
if(no==0)
{
arrLine[0][0]=x1;
arrLine[0][1]=x2;
line(x1,y1,x2,y2);
k=1;
}
else
{
k=0;
for(q=0;q<no;q++)
{
temp_x1=*(*(previousArrLine+q)+0);
temp_x2=*(*(previousArrLine+q)+1);
diff=temp_x2 - temp_x1;
equal_part=diff / no_of_divisions;
next_temp_x1=temp_x1;
next_temp_x2=temp_x1+equal_part;
for(i=1;i<=no_of_divisions;i++)
Ashish M. Gautam Application Id: 14131000229
38

{
if(i%2!=0)
{
arrLine[k][0]=next_temp_x1;
arrLine[k][1]=next_temp_x2;
line(next_temp_x1,y1,next_temp_x2,y2);
k=k+1;
}
next_temp_x1=next_temp_x1+equal_part;
next_temp_x2=next_temp_x2+equal_part;
}
}
}

*arrLength=*(&k);
return arrLine;
}

int main()
{
int i;
int gd=DETECT,gm, arrLength=0;
int x1=0,x2=635,y1=0,y2=0,row;
float arrLine1[100][2];
float (*arrLine)[2];
float (*(createALine)(int,int,int,int,float (*arrLine)[2],int *))[2];
clrscr();
initgraph(&gd,&gm,"c:tcbgi");
arrLine=arrLine1;

for(row=2;row<=42;row=row+20)
{
y1=row;
y2=row;
arrLine=createALine(x1,y1,x2,y2,arrLine,&arrLength);
}

getch();
}







Ashish M. Gautam Application Id: 14131000229
39

Output:




Ashish M. Gautam Application Id: 14131000229
40

Program No: 16

Aim: Write a C Program to Draw Bezier and B-Spline Curves with interactive user inputs.

Program:
#include <GL/glut.h>

typedef enum
{
BEZIER,
INTERPOLATED,
BSPLINE
} curveType;

void keyboard(unsigned char key, int x, int y);
void computeMatrix(curveType type, float m[4][4]);
void vmult(float m[4][4], float v[4][3], float r[4][3]);

GLfloat colors[][3] =
{
{ 1.0, 0.0, 0.0 },
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 }
};

#define MAX_CPTS 25 /* Fixed maximum number of control points */

GLfloat cpts[MAX_CPTS][3];
int ncpts = 0;

static int width = 500, height = 500; /* Window width and height */

void reshape(int w, int h)
{
width = w;
height = h;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, w, h);
}

void vmult(float m[4][4], float v[4][3], float r[4][3])
{
Ashish M. Gautam Application Id: 14131000229
41

int i, j, k;

for (i = 0; i < 4; i++)
for (j = 0; j < 3; j++)
for (k = 0, r[i][j] = 0.0; k < 4; k++)
r[i][j] += m[i][k] * v[k][j];
}

static float minterp[4][4] =
{
{ 1.0, 0.0, 0.0, 0.0 },
{ -5.0/6.0, 3.0, -3.0/2.0, 1.0/3.0 },
{ 1.0/3.0, -3.0/2.0, 3.0, -5.0/6.0 },
{ 0.0, 0.0, 0.0, 1.0 },
};

static float mbspline[4][4] =
{
{ 1.0/6.0, 4.0/6.0, 1.0/6.0, 0.0 },
{ 0.0, 4.0/6.0, 2.0/6.0, 0.0 },
{ 0.0, 2.0/6.0, 4.0/6.0, 0.0 },
{ 0.0, 1.0/6.0, 4.0/6.0, 1.0/6.0 },
};

static float midentity[4][4] =
{
{ 1.0, 0.0, 0.0, 0.0},
{ 0.0, 1.0, 0.0, 0.0},
{ 0.0, 0.0, 1.0, 0.0},
{ 0.0, 0.0, 0.0, 1.0}
};

void computeMatrix(curveType type, float m[4][4])
{
int i, j;

switch (type)
{
case BEZIER:
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
m[i][j] = midentity[i][j];
break;
case INTERPOLATED:
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
Ashish M. Gautam Application Id: 14131000229
42

m[i][j] = minterp[i][j];
break;
case BSPLINE:
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
m[i][j] = mbspline[i][j];
break;
}
}

static void drawCurves(curveType type)
{
int i, j;
int step;
GLfloat newcpts[4][3];

float m[4][4];

computeMatrix(type, m);

if(type == BSPLINE) step = 1;
else step = 3;

glColor3fv(colors[type]);

i = 0;
while (i + 3 < ncpts)
{
vmult(m, &cpts[i], newcpts);
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &newcpts[0][0]);
glMapGrid1f(30, 0.0, 1.0);
glEvalMesh1(GL_LINE, 0, 30);
i += step;
}
glFlush();
}

static void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glPointSize(5.0);
glBegin(GL_POINTS);
for (i = 0; i < ncpts; i++)
glVertex3fv(cpts[i]);
Ashish M. Gautam Application Id: 14131000229
43

glEnd();
glFlush();
}

static void mouse(int button, int state, int x, int y)
{
float wx, wy;
if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
return;
wx = (2.0 * x) / (float)(width - 1) - 1.0;
wy = (2.0 * (height - 1 - y)) / (float)(height - 1) - 1.0;
if (ncpts == MAX_CPTS)
return;
cpts[ncpts][0] = wx;
cpts[ncpts][1] = wy;
cpts[ncpts][2] = 0.0;
ncpts++;

glColor3f(0.0, 0.0, 0.0);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex3f(wx, wy, 0.0);
glEnd();
glFlush();
}

void keyboard(unsigned char key, int x, int y)
{
static curveType lasttype = BEZIER;

switch (key)
{
case 'q': case 'Q':
exit(0);
break;
case 'c': case 'C':
ncpts = 0;
glutPostRedisplay();
break;
case 'e': case 'E':
glutPostRedisplay();
break;
case 'b': case 'B':
drawCurves(BEZIER);
lasttype = BEZIER;
break;
Ashish M. Gautam Application Id: 14131000229
44

case 'i': case 'I':
drawCurves(INTERPOLATED);
lasttype = INTERPOLATED;
break;
case 's': case 'S':
drawCurves(BSPLINE);
lasttype = BSPLINE;
break;
}
}


main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("curves");

glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_MAP1_VERTEX_3);

glutMainLoop();
}

Output:




Ashish M. Gautam Application Id: 14131000229
45

Program No: 17

Aim: Write a C program to Demonstrate 2D animation such as clock simulation.

Program:
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>

void main()
{
int gd=DETECT,gm;
int x=320,y=240,r=200,i,h,m,s,thetamin,thetasec;
struct time t;
char n[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"};
initgraph(&gd,&gm,"c:\tc");
circle(x,y,210);
setcolor(4);
settextstyle(4,0,5);

for(i=0;i<12;i++)
{
if(i!=3)
outtextxy(x+(r-14)*cos(M_PI/6*i)-10,y-(r-14)*sin(M_PI/6*i)-26,n[i]);
else
outtextxy(x+(r-14)*cos(M_PI/6*i)-20,y-(r-14)*sin(M_PI/6*i)-26,n[i]);
}

gettime(&t);
printf("The current time is: %2d:%02d:%02d.%02d",t.ti_hour, t.ti_min,t.ti_sec,
t.ti_hund);

while(!kbhit())
{
setcolor(5);
setfillstyle(1,5);
circle(x,y,10);
floodfill(x,y,5);
gettime(&t);
if(t.ti_min!=m)
{
setcolor(0);
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-
60)*sin(thetamin*(M_PI/180)));
Ashish M. Gautam Application Id: 14131000229
46

circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-
80)*sin(thetamin*(M_PI/180)),10);
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-
110)*sin(M_PI/6*h-((m/2)*(M_PI/180))));
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-
130)*sin(M_PI/6*h-((m/2)*(M_PI/180))),10);
}
if(t.ti_hour>12)
t.ti_hour=t.ti_hour-12;
if(t.ti_hour<4)
h=abs(t.ti_hour-3);
else
h=15-t.ti_hour;
m=t.ti_min;
if(t.ti_min<=15)
thetamin=(15-t.ti_min)*6;
else
thetamin=450-t.ti_min*6;
if(t.ti_sec<=15)
thetasec=(15-t.ti_sec)*6;
else
thetasec=450-t.ti_sec*6;
setcolor(4);
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h-
((m/2)*(M_PI/180))));
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-
((m/2)*(M_PI/180))),10);
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-
60)*sin(thetamin*(M_PI/180)));
circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-
80)*sin(thetamin*(M_PI/180)),10);
setcolor(15);
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180)));
delay(1000);
setcolor(0);
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180)));
}
}








Ashish M. Gautam Application Id: 14131000229
47

Output:




Ashish M. Gautam Application Id: 14131000229
48

Program No: 18

Aim: Write a C Program implement the bouncing ball inside a defined rectangular
window.

Program:
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
main(void)
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
int maxx,maxy,area,x=25,y=25,ch,xdir=1,ydir=1;void *buff;
while(!kbhit())
{
setcolor(15);
rectangle(0,0,getmaxx(),getmaxy());
outtextxy(200,1,"Press ESC to EXIT...!");
setcolor(14);x=x+(xdir*5);y=y+(ydir*2);
circle(x,y,25);setfillstyle(WIDE_DOT_FILL,2);
floodfill(x+5,y+5,14);delay(10);setfillstyle(SOLID_FILL,0);
floodfill(x+5,y+5,14);setcolor(0);
circle(x,y,25);
floodfill(x+2,y+2,0);
if(x>getmaxx()-25||x<25){sound(5000);
delay(40);sound(3000);delay(40);
nosound();
xdir*=-1;
}
if(y>getmaxy()-25||y<25)
{
sound(5000);
delay(40);
sound(3000);
delay(40);
nosound();
ydir*=-1;
}
}
getch();
closegraph();
restorecrtmode();
Ashish M. Gautam Application Id: 14131000229
49

return 0;
}

Output:

Potrebbero piacerti anche