RCS 653 CG LAB FIle (Solution)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 42

EXPERIMENT -01

To implement DDA algorithms for line using C


PROGRAM –

#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,pixel;
int i,gd,gm;

printf("Enter the value of x1 : ");


scanf("%f",&x1);
printf("Enter the value of y1 : ");
scanf("%f",&y1);
printf("Enter the value of x2 : ");
scanf("%f",&x2);
printf("Enter the value of y1 : ");
scanf("%f",&y2);

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

dx=abs(x2-x1);
dy=abs(y2-y1);

if(dx>=dy)
pixel=dx;
else
pixel=dy;

dx=dx/pixel;
dy=dy/pixel;

x=x1;
y=y1;

i=1;
while(i<=pixel)
{
putpixel(x,y,15);
x=x+dx;
y=y+dy;
i=i+1;
//delay(100);
}
getch();
closegraph();
}
OUTPUT –

EXPERIMENT -02

To implement Bresenham’s algorithms for line using C


PROGRAM –

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

void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;

clrscr();

printf("\n\n\tEnter the co-ordinates of first point : ");


scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);

dx = (x2 - x1);
dy = (y2 - y1);

p = 2 * (dy) - (dx);

x = x1;
y = y1;

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\Turboc3\\bgi");
putpixel(x,y,WHITE);

while(x <= x2)


{
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 –

EXPERIMENT -03
1. To implement Bresenham’s algorithm for drawing circle using C
PROGRAM:

# include<stdio.h>
# include<conio.h>
# include<graphics.h>
void main()
{
int gd=DETECT,gm,xc,yc,r,x,y,Pk;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Bresenham's Circle Drawing Algorithm ***\n");
printf("Enter the value of Xc\t");
scanf("%d",&xc);
printf("Enter the value of yc\t");
scanf("%d",&yc);
printf("Enter the Radius of circle\t");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
Pk=3-(2*r);
for(x=0;x<=y;x++)
{
if (Pk<0)
{
y=y;
Pk=(Pk+(4*x)+6);
}
else
{
y=y-1;
Pk=Pk+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,7);
putpixel(xc-x,yc-y,7);
putpixel(xc+x,yc+y,7);
putpixel(xc-x,yc+y,7);
putpixel(xc+y,yc-x,7);
putpixel(xc-y,yc-x,7);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,7);
delay(100);

}
getch();

Output

2. To implement Bresenham’s algorithm for drawing ellipse using C


PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm,a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Ellipse Generating Algorithm ***\n");
printf("Enter the value of Xc\t");
scanf("%d",&xc);
printf("Enter the value of yc\t");
scanf("%d",&yc);
printf("Enter X axis length\t");
scanf("%d",&a);
printf("Enter Y axis length\t");
scanf("%d",&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
delay(50);
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
delay(50);
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,7);
putpixel(xc-x,yc+y,7);
putpixel(xc+x,yc-y,7);
putpixel(xc+x,yc-y,7);
}

Output

EXPERIMENT -04

1. To implement Mid Point Circle algorithm using C


PROGRAM –
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void pixel(int xc,int yc,int x,int y);
void main()
{
int gd=DETECT,gm,xc,yc,r,x,y,Pk;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi ");
printf("*** Mid-Point Subdivision algorithm of circle ***\n");
printf("Enter the value of Xc\t");
scanf("%d",&xc);
printf("Enter the value of Yc \t");
scanf("%d",&yc);
printf("Enter the Radius of circle\t");
scanf("%d",&r);
x=0;
y=r;
Pk=1-r;
pixel(xc,yc,x,y);
while(x<y)
{
if(Pk<0)
{
x=x+1;
Pk=Pk+(2*x)+1;
}
else
{
x=x+1;
y=y-1;
Pk=Pk+(2*x)-(2*y)+1;
}
pixel(xc,yc,x,y);
}
getch();
closegraph();
}
void pixel(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,7);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,7);
putpixel(xc-x,yc+y,7);
putpixel(xc-x,yc-y,7);
putpixel(xc-y,yc-x,7);
putpixel(xc+y,yc-x,7);
putpixel(xc+x,yc-y,7);
}

OUTPUT –
2. To implement Mid Point Ellipse generating algorithm using C.

PROGRAM –

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
scanf("%d%d",&xc,&yc);
scanf("%d%d",&a,&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc+x,yc-y,10);
}

EXPERIMENT -05

To perform 2D Transformations such as translation, rotation, scaling, reflection


and shearing.
PROGRAM –

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void menu();
void input();
void output();
void translation();
void rotation();
void scaling();
void shearing();
void reflection();
int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;
float sx,sy;
void menu()
{
printf("menu\n");
printf("1.Translation\n");
printf("2.rotation\n");
printf("3.scaling\n");
printf("4.shearing\n");
printf("5.reflection\n");
printf("6.exit\n");
printf("enter the choice:");
scanf("%d",&option);
switch(option)
{
case 1:
input();
translation();
break;
case 2:
input();
rotation();
break;
case 3:
input();
scaling();
break;
case 4 :
input();
shearing();
break;
case 5:
input();
reflection();
break;
case 6:
exit(0);
break;
}
}
void input()
{
printf("enter the number of vertices:" );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the coordinates:");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}
void output()
{
cleardevice();
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
}
void translation()
{
output();
printf("enter the tranformation vertex tx,ty:\n");
scanf("%d%d",&tx,&ty);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty;
}
output();
delay(10);
menu();
}
void rotation()
{
output();
printf("enter the rotating angle:");
scanf("%d",&y);
printf("enter the pivot point:");
scanf("%d%d",&fx,&fy);
k=(y*3.14)/180;
for(i=0;i<=n;i++)
{
a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k);
}
output();
delay(10);
menu();
}
void scaling()
{
output();
printf("enter the scaling factor\n");
scanf("%f%f",&sx,&sy);
printf("enter the fixed point:");
scanf("%d%d",&fx,&fy);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx+fy*(1-sx);
a[i][1]=a[i][1]*sy+fy*(1-sy);
}
output();
delay(10);
menu();
}
void shearing()
{
output();
printf("enter the shear value:");
scanf("%d",&sh);
printf("enter the fixed point:");
scanf("%d%d",&fx,&fy);
printf("enter the axis for shearing if x-axis then 1 if y-axis the 0:");
scanf("%d",&axis);
for(i=0;i<=n;i++)
{
if(axis==1)
{
a[i][0]=a[i][0]+sh*(a[i][1]-fy);
}
else
{
a[i][1]=a[i][1]+sh*(a[i][0]-fx);
}
}
output();
delay(10);
menu();
}
void reflection()
{
output();
for(i=0;i<=n;i++)
{
temp=a[i][0];
a[i][0]=a[i][1];
a[i][1]=temp;
}
output();
delay(10);
menu();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
menu();
getch();
}
OUTPUT:
Menu
1.Translation
2.Rotation
3.Scaling
4.Shearing
5.Reflection
6.Exit
Enter the choice : 1
Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200

Enter the coordinates : 60 200 30 150

Enter the translation vector Tx, Ty : 90 60


Enter the choice : 2
Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200

Enter the coordinates : 60 200 30 150

Enter the Rotating Angle : 90


Enter the Pivot Point : 100 200

Enter the choice : 3


Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200

Enter the coordinates : 60 200 30 150

Enter the scaling Factor : 0.3 0.4


Enter the Fixed Point : 100 200
Enter the choice : 4
Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200

Enter the coordinates : 60 200 30 150

Enter the shear Value : 5


Enter the fixed point : 50 100

Enter the Axis for shearing if x-axis then 1

if y-axis then 0

Enter the choice : 5


Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200

Enter the coordinates : 60 200 30 150


EXPERIMENT -06

1. To implement Cohen-Sutherland Line clipping algorithm using C


PROGRAM –

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define MAX 20

enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 };

enum { FALSE, TRUE };


typedef unsigned int outcode;

outcode compute_outcode(int x, int y,


int xmin, int ymin, int xmax, int ymax)
{
outcode oc = 0;

if (y > ymax)
oc |= TOP;
else if (y < ymin)
oc |= BOTTOM;

if (x > xmax)
oc |= RIGHT;
else if (x < xmin)
oc |= LEFT;

return oc;
}

void cohen_sutherland (double x1, double y1, double x2, double y2,
double xmin, double ymin, double xmax, double ymax)
{
int accept;
int done;
outcode outcode1, outcode2;

accept = FALSE;
done = FALSE;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
do
{
if (outcode1 == 0 && outcode2 == 0)
{
accept = TRUE;
done = TRUE;
}
else if (outcode1 & outcode2)
{
done = TRUE;
}
else
{
double x, y;
int outcode_ex = outcode1 ? outcode1 : outcode2;
if (outcode_ex & TOP)
{
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
}

else if (outcode_ex & BOTTOM)


{
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
}
else if (outcode_ex & RIGHT)
{
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
}
else
{
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (outcode_ex == outcode1)
{
x1 = x;
y1 = y;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
}
else
{
x2 = x;
y2 = y;
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
}
}
} while (done == FALSE);

if (accept == TRUE)
line (x1, y1, x2, y2);
}
void main()
{
int n;
int i, j;
int ln[MAX][4];
int clip[4];
int gd = DETECT, gm;

printf ("Enter the number of lines to be clipped");


scanf ("%d", &n);

printf ("Enter the x- and y-coordinates of the line-endpoints:\n");


for (i=0; i<n; i++)
for (j=0; j<4; j++)
scanf ("%d", &ln[i][j]);

printf ("Enter the x- and y-coordinates of the left-top and right-");


printf ("bottom corners\nof the clip window:\n");
for (i=0; i<4; i++)
scanf ("%d", &clip[i]);

initgraph (&gd, &gm, "..//bgi");

rectangle (clip[0], clip[1], clip[2], clip[3]);


for (i=0; i<n; i++)
line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]);
getch();
cleardevice();
rectangle (clip[0], clip[1], clip[2], clip[3]);
for (i=0; i<n; i++)
{
cohen_sutherland (ln[i][0], ln[i][1], ln[i][2], ln[i][3],
clip[0], clip[1], clip[2], clip[3]);
getch();
}
closegraph();
}
OUTPUT –
Before Clipping After Clipping

2. To write a C program to clip a Window to Viewport Mapping.

PROGRAM –

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
float sx,sy;
int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\n");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
w1=5;
w2=5;
w3=635;
w4=465;
rectangle(w1,w2,w3,w4);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
v1=425;
v2=75;
v3=550;
v4=250;
sx=(float)(v3-v1)/(w3-w1);
sy=(float)(v4-v2)/(w4-w2);
rectangle(v1,v2,v3,v4);
x1=v1+floor(((float)(x1-w1)*sx)+.5);
x2=v1+floor(((float)(x2-w1)*sx)+.5);
x3=v1+floor(((float)(x3-w1)*sx)+.5);
y1=v2+floor(((float)(y1-w2)*sy)+.5);
y2=v2+floor(((float)(y2-w2)*sy)+.5);
y3=v2+floor(((float)(y3-w2)*sy)+.5);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
return 0;
}
OUTPUT –
Enter The Coordinate x1,y1,x2,y2,x3,y3
100
200
300
400
500
350

EXPERIMENT -07

To implement Liang-Barsky Line clipping algorithm using C


PROGRAM –

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

void main()
{
float x1,y1,x2,y2,xmin,xmax,ymin,ymax,dx,dy;
float p[4],q[4],r[4];
float max,min,u1,u2;
float xi,xii,yi,yii;
int gd,gm,i;
gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
printf("\n enter the line co-ordinates");
printf("\n enter 1st x=");
scanf("%f",&x1);
printf("\t 1st y=");
scanf("%f",&y1);
printf("\n enter 2nd x=");
scanf("%f",&x2);
printf("\t 2nd y=");
scanf("%f",&y2);
printf("\n enter window boundry");
printf("\n xmin=");
scanf("%f",&xmin);
printf("\n ymin=");
scanf("%f",&ymin);
printf("\n xmax=");
scanf("%f",&xmax);
printf("\n ymax=");
scanf("%f",&ymax);
dx=x2-x1;
dy=y2-y1;
cleardevice();
line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
p[0]=-dx;
q[0]=x1-xmin;
p[1]=dx;
q[1]=xmax-x1;
p[2]=-dy;
q[2]=y1-ymin;
p[3]=dy;
q[3]=ymax-y1;
for(i=0;i<4;i++)
{
if(p[i]==0 && q[i]<0)
{
printf("Line is outside the boundry,it is not a clipping candidate\n");
getch();
exit(0);
}}
for(i=0;i<4;i++)
{
r[i]=q[i]/p[i];
printf("\n r[%d]=%f",i,r[i]);
}
max=0;min=1;
for(i=0;i<4;i++)
if(p[i]<0)
{ if(r[i]>max)
max=r[i]; }
else
if(r[i]<min)
min=r[i];
u1=max;
u2=min;
printf("\n u1=%f",u1);
printf("\n u2=%f",u2);
if(u1>u2)
{ printf("\n line is completely outside");
getch();
exit(0); }
xi=x1+(u1*dx);
yi=y1+(u1*dy);
xii=x1+(u2*dx);
yii=y1+(u2*dy);
rectangle(xmin,ymin,xmax,ymax);
setcolor(5);
line(xi,yi,xii,yii);
getch();
closegraph();
}

OUTPUT –
Enter the line coordinates:

Enter 1st x = 100


1st y = 200

Enter 2nd x = 150


2nd y = 250
Enter window boundary
Xmin = 110
Ymin = 160
Xmax = 1800
Ymax = 280

EXPERIMENT -08

To implement Sutherland-Hodgman Polygon algorithm using C.


PROGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;

void clipl(float x1,float y1,float x2,float y2)


{
if(x2-x1)
m=(y2-y1)/(x2-x1);
if(x1>=xmin && x2>=xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1<xmin && x2>=xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1>=xmin && x2<xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}
void clipt(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
if(y1<=ymax && y2<=ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1>ymax && y2<=ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1<=ymax && y2>ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}

void clipr(float x1,float y1,float x2,float y2)


{
if(x2-x1)
m=(y2-y1)/(x2-x1);
if(x1<=xmax && x2<=xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1>xmax && x2<=xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1<=xmax && x2>xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}

void clipb(float x1,float y1,float x2,float y2)


{
if(y2-y1)
m=(x2-x1)/(y2-y1);
if(y1>=ymin && y2>=ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1<ymin && y2>=ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1>=ymin && y2<ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}

void main()
{
int gdriver=DETECT,gmode,n,poly[20],i;
float xi,yi,xf,yf,polyy[20];
clrscr();
printf("Coordinates of rectangular clip window :\nxmin,ymin:");
scanf("%f%f",&xmin,&ymin);
printf("Coordinates of rectangular clip window :\nxmax,ymax:");
scanf("%f%f",&xmax,&ymax);
printf("\n\nPolygon to be clipped :\nNumber of sides :");
scanf("%d",&n);
printf("Enter the coordinates :");
for (i=0;i<2*n;i++)
scanf("%f",&polyy[i]);
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i<2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
rectangle(xmin,ymax,xmax,ymin);
printf("\tUNCLIPPED POLYGON");
printf("hi");
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i<2*n;i+=2)
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i<k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
rectangle(xmin,ymax,xmax,ymin);
printf("\tCLIPPED POLYGON");
getch();
closegraph();
}

OUTPUT –

Coordinates of rectangular clip window :


Xmin, Ymin 150 150
Xmax, Ymax 300 300

Polygon to be clipped:
Number of slides: 5
Enter the coordinates:
50
50
200
100
350
350
80
200
40
80

EXPERIMENT -09
1. To implement Bezier Curves algorithm using C.
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>

void bezier (int x[4], int y[4])


{
int gd = DETECT, gm;
int i;
double t;
initgraph (&gd, &gm, "..\\bgi");
for (t = 0.0; t < 1.0; t += 0.0005)
{
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] +
3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];

double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] +


3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];
putpixel (xt, yt, WHITE);
}
for (i=0; i<4; i++)
putpixel (x[i], y[i], YELLOW);
getch();
closegraph();
return;
}
void main()
{
int x[4], y[4];
int i;

printf ("Enter the x- and y-coordinates of the four control points.\n");


for (i=0; i<4; i++)
scanf ("%d%d", &x[i], &y[i]);
bezier (x, y);
}

OUTPUT –

Enter the x- and y-coordinates of the four control points.

20
200
40
300
80
30
50
400

2. To implement Bezier surface using C.

PROGRAM

#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();
}

EXPERIMENT -10
To perform 3D Transformations such as translation, rotation and scaling.

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int gd,gm,x,y,z,o,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"..\\bgi");
setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter Translation Factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("after translation");
bar3d(midx+(x+50),midy-(y+100),midx+x+60,midy-(y+90),5,1);
axis();
bar3d(midx+50,midy+100,midx+60,midy-90,5,1);
printf("Enter Scaling Factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("After Scaling");
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter Rotating Angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*cos(o*3.14/180)+100*sin(o*3.14/180);
x2=60*sin(o*3.14/180)-90*cos(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After Rotation about Z Axis");
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
axis();
printf("After Rotation about X Axis");
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
axis();
printf("After Rotation about Y Axis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
getch();
closegraph();
}
OUTPUT –
Translation

Enter Translation Factor 50 60 70


After Translation –

Scaling

Enter scaling factor: 70 80 90

Rotation
Enter rotating angle: 60

EXPERIMENT -11

To write a C program for implementation of Visualizing Projections of 3d Images.


PROGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
struct point
{
int x,y,z;
};
struct edge
{
struct point start;
struct point end;
};
float pi=3.14/180.0;
void convert2d(int *x,int *y,int *z)
{
int xp,yp;
xp=(*x)+(*z)*cos(pi*45)/tan(pi*45);
yp=(*y)+(*z)*sin(pi*45)/tan(pi*45);
*x=xp;
*y=yp;
}
void screen(int *x,int *y)
{
int xm,ym;
xm=getmaxx();
ym=getmaxy();
*x=xm/2+*x;
*y=ym/2-*y;
}
void draw3d(struct edge po[],int n)
{
int i,x1,y1,z1,x2,y2,z2;
for(i=0;i<n;i++)
{
x1=po[i].start.x;
y1=po[i].start.y;
z1=po[i].start.z;
convert2d(&x1,&y1,&z1);
x2=po[i].end.x;
y2=po[i].end.y;
z2=po[i].end.z;
convert2d(&x2,&y2,&z2);
screen(&x1,&y1);
screen(&x2,&y2);
line(x1,y1,x2,y2);
}
}
void main()
{
int gd=DETECT,gm=0;
int i,tx,ty,tz,sx,sy,sz,n;
int xx1,xx2,yy1,yy2;
float rx,ry,rz;
struct edge p[50],q[50],r[50],s[50],t[50],v[50];
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
printf("\nEnter the number of edges:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nStart pt for edge %d(x,y,z):",i+1);
scanf("%d%d%d",&p[i].start.x,&p[i].start.y,&p[i].start.z);
printf("\nEnd pt for edge %d(x,y,z):",i+1);
scanf("%d%d%d",&p[i].end.x,&p[i].end.y,&p[i].end.z);
}
cleardevice();
printf("\n3D VIEW");
draw3d(p,n);
getch();
cleardevice();
printf("\nTOP VIEW");
for(i=0;i<n;i++)
{
xx1=p[i].start.x;
yy1=p[i].start.z;
xx2=p[i].end.x;
yy2=p[i].end.z;
screen(&xx1,&yy1);
screen(&xx2,&yy2);
line(xx1,yy1,xx2,yy2);
}
getch();
cleardevice();
printf("\nSIDE VIEW");
for(i=0;i<n;i++)
{
xx1=p[i].start.z;
yy1=p[i].start.y;
xx2=p[i].end.z;
yy2=p[i].end.y;
screen(&xx1,&yy1);
screen(&xx2,&yy2);
line(xx1,yy1,xx2,yy2);
}
getch();
cleardevice();
printf("\nFRONT VIEW");
for(i=0;i<n;i++)
{
xx1=p[i].start.x;
yy1=p[i].start.y;
xx2=p[i].end.x;
yy2=p[i].end.y;
screen(&xx1,&yy1);
screen(&xx2,&yy2);
line(xx1,yy1,xx2,yy2);
}
getch();
cleardevice();
}
OUTPUT –
Start pt for edge 1 (x,y,z) :0 0 0
End pt for edge 1 (x,y,z) : 200 0 0
Start pt for edge 2 (x,y,z) : 200 0 0
End pt for edge 2 (x,y,z) : 200 0 100
Start pt for edge 3 (x,y,z) : 200 0 100
End pt for edge 3 (x,y,z) :0 0 100
Start pt for edge 4 (x,y,z) :0 0 100
End pt for edge 4 (x,y,z) :0 0 0
Start pt for edge 5 (x,y,z) :0 100 0
End pt for edge 5 (x,y,z) : 200 100 0
Start pt for edge 6 (x,y,z) : 200 100 0
End pt for edge 6 (x,y,z) : 200 100 100
Start pt for edge 7 (x,y,z) : 200 100 100
End pt for edge 7 (x,y,z) :0 100 100
Start pt for edge 8 (x,y,z) :0 100 100
End pt for edge 8 (x,y,z) :0 100 0
Start pt for edge 9 (x,y,z ) :0 100 0
End pt for edge 9 (x,y,z) :0 0 0
Start pt for edge 10 (x,y,z) : 200 100 0
End pt for edge 10 (x,y,z) : 200 0 0
Start pt for edge 11 (x,y,z) : 200 100 100
End pt for edge 11 (x,y,z) : 200 0 100
Start pt for edge 12 (x,y,z) :0 100 100
End pt for edge 12 (x,y,z) :0 0 100

3D View –

Top View –

Side View –

Front View –
EXPERIMENT -12
Write a c program to draw a solid and find its vanishing point.

PROGRAM

#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);
}
OUTPUT –
EXPERIMENT -13
To convert between color models using C.
PROGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm,n;
float r=0,g=0,b=0;
float y=0,i=0,q=0;
float c=0,m=0,y1=0;
initgraph(&gd,&gm,"");
printf("(r,g,b)keys for incrementing R,G,B values respectively\n");
printf("(shift+(r,g,b))keys for decrementing R,G,B values respectively\n");
printf("press esc to exit\n");
setcolor(15);
while(1)
{ gotoxy(18,10);
printf("R G B");
c=1.0-r;
m=1.0-g;
y1=1.0-y;
gotoxy(47,10);
printf("C M Y");
y=0.299*r+0.587*g+0.144*b;
i=0.596*r-0.275*g-0.3218*b;
q=0.212*r-0.528*g+0.311*b;
gotoxy(18,23);
printf("Y I Q");
switch(getche())
{ case 'r':
r++;
break;
case 'g':
g++;
break;
case 'b':
b++;
break;
case 'R':
r--;
break;
case 'G':
g--;
break;
case 'B':
b--;
break;
case 27:
closegraph();
exit(0);
}
if(r>255)
r=0;
if(g>255)
g=0;
if(b>255)
b=0;
setrgbpalette(1,r,g,b);
setfillstyle(1,1);
bar(50,50,270,250);
rectangle(50,50,270,250);
setrgbpalette(2,c,m,y1);
setfillstyle(1,2);
bar(275,50,495,250);
rectangle(275,50,495,250);
setrgbpalette(3,y,i,q);
setfillstyle(1,3);
bar(50,255,270,455);
rectangle(50,255,270,455);
}
}

OUTPUT-

EXPERIMENT -14
1. Road animation using C.

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int i,gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
// Set Background colour to GREEN
setbkcolor(GREEN);
// The program runs indefinitely until you press a key
while(!kbhit())
{
// Draw the road
setfillstyle(SOLID_FILL,8);
bar(150,0,470,500);
// Draw the WHITE divider lines
setfillstyle(SOLID_FILL,15);
bar(310,0+i,320,50+i);
bar(310,70+i,320,120+i);
bar(310,140+i,320,190+i);
bar(310,210+i,320,260+i);
bar(310,280+i,320,330+i);
bar(310,350+i,320,400+i);
bar(310,420+i,320,470+i);
delay(200);
i=i+10;
if(i >= 50) {
i=0;
}
}
closegraph();
getch();
}

2. Solar System animation using C


PROGRAM
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <math.h>
/* manipulates the position of planets on the orbit */
void planetMotion(int xrad, int yrad, int midx, int midy, int x[60], int y[60]) {
int i, j = 0;

/* positions of planets in their corresponding orbits */


for (i = 360; i > 0; i = i - 6) {
x[j] = midx - (xrad * cos((i * 3.14) / 180));
y[j++] = midy - (yrad * sin((i * 3.14) / 180));
}
return;
}
int main() {
/* request auto detection */
int gdriver = DETECT, gmode, err;
int i = 0, midx, midy;
int xrad[9], yrad[9], x[9][60], y[9][60];
int pos[9], planet[9], tmp;
/* initialize graphic mode */
initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
err = graphresult();
if (err != grOk) {
/* error occurred */
printf("Graphics Error: %s",
grapherrormsg(err));
return 0;
}
/* mid positions at x and y-axis */
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* manipulating radius of all 9 planets */
planet[0] = 7;
for (i = 1; i < 9; i++) {
planet[i] = planet[i - 1] + 1;
}
/* offset position for the planets on their corresponding orbit */
for (i = 0; i < 9; i++) {
pos[i] = i * 6;
}
/* orbits for all 9 planets */
xrad[0] = 60, yrad[0] = 30;
for (i = 1; i < 9; i++) {
xrad[i] = xrad[i - 1] + 30;
yrad[i] = yrad[i - 1] + 15;
}
/* positions of planets on their corresponding orbits */
for (i = 0; i < 9; i++) {
planetMotion(xrad[i], yrad[i], midx, midy, x[i], y[i]);
}

while (!kbhit()) {
/* drawing 9 orbits */
setcolor(WHITE);
for (i = 0; i < 9; i++) {
ellipse(midx, midy, 0, 360, xrad[i], yrad[i]);
}
/* sun at the mid of the solar system */
setcolor(YELLOW);
setfillstyle(SOLID_FILL, YELLOW);
circle(midx, midy, 20);
floodfill(midx, midy, YELLOW);
/* mercury in first orbit */
setcolor(CYAN);
setfillstyle(SOLID_FILL, CYAN);
pieslice(x[0][pos[0]], y[0][pos[0]], 0, 360, planet[0]);
/* venus in second orbit */
setcolor(GREEN);
setfillstyle(SOLID_FILL, GREEN);
pieslice(x[1][pos[1]], y[1][pos[1]], 0, 360, planet[1]);
/* earth in third orbit */
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
pieslice(x[2][pos[2]], y[2][pos[2]], 0, 360, planet[2]);
/* mars in fourth orbit */
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
pieslice(x[3][pos[3]], y[3][pos[3]], 0, 360, planet[3]);
/* jupiter in fifth orbit */
setcolor(BROWN);
setfillstyle(SOLID_FILL, BROWN);
pieslice(x[4][pos[4]], y[4][pos[4]], 0, 360, planet[4]);
/* saturn in sixth orbit */
setcolor(LIGHTGRAY);
setfillstyle(SOLID_FILL, LIGHTGRAY);
pieslice(x[5][pos[5]], y[5][pos[5]], 0, 360, planet[5]);
/* uranus in sevth orbit */
setcolor(BROWN);
setfillstyle(SOLID_FILL, BROWN);
pieslice(x[6][pos[6]], y[6][pos[6]], 0, 360, planet[6]);
/* neptune in eigth orbit */
setcolor(LIGHTBLUE);
setfillstyle(SOLID_FILL, LIGHTBLUE);
pieslice(x[7][pos[7]], y[7][pos[7]], 0, 360, planet[7]);
/* pluto in ninth orbit */
setcolor(LIGHTRED);
setfillstyle(SOLID_FILL, LIGHTRED);
pieslice(x[8][pos[8]], y[8][pos[8]], 0, 360, planet[8]);
/* checking for one complete rotation */
for (i = 0; i < 9; i++) {
if (pos[i] <= 0) {
pos[i] = 59;
} else {
pos[i] = pos[i] - 1;
}
}
/* sleep for 100 milliseconds */
delay(100);
/* clears graphic screen */
cleardevice();
}
/* deallocate memory allocated for graphic screen */
closegraph();
return 0;
}
EXPERIMENT -15

To perform basic operations on image using any image editing software (Adobe
Photoshop)
PROGRAM

Part 1. Loading Images

The easiest way to load an image is to open the image file on your computer. You can do this
from the menu using File -> Open or pressing Ctrl+O on your keyboard.
Part 2. Cropping an Image
In cropping an image, our goal is to get rid of excess space or unwanted graphics in the
image.First, select the Select Tool, or Rectangular Marquee Tool. The hotkey for this is “M”. It
is located on the panel in the left with all of your image editing tools, pictured here to the left.
Then, drag your mouse across the area you wish to keep.
Note: While selecting part of the image, you may click elsewhere to reselect the area, or right-
click and hit “deselect”. Ctrl+R or View -> Rulers will add rulers to the top and left to help
decide which area you wish to select. You may also wish to zoom in or out using View -> Zoom
In or View -> Zoom Out.
Lastly, you may undo or redo changes with Edit -> Step Backward and Edit -> Step Forward in
the top menu. The hotkeys for these are Alt+Ctrl+Z and Shift+Ctrl+Z for backward and forward
respectively and are very useful. Once you have selected your region, open the “Image” menu at
the top and select “Crop” to crop out the rest of the image.
Part 3. Resizing the Image
At times, an image may be too small or large for its intended purpose and may need to be
resized. From the Image menu, select “Image Size”. The hotkey for this is Alt+Ctrl+I. A menu
with various options will appear. However, it is actually rather simple. You may enter the
dimensions of the image as a width and height in any units you want (it is currently set under
“pixels”, a common unit of graphical space, where one “pixel” is equal to a colored dot on your
computer)
Part 4. To adjust the brightness and contrast
1. Open adobe Photoshop 7.0-> file->open-> choose a file and open it.
2. Go to image->adjustments->Brightness/Contrast.
3. After getting the Brightness/Contrast window adjust the brightness and contrast by dragging.
the appropriate bar setting.
4. Finally save the image file.
EXPERIMENT -16

1. To draw different shapes such as kite etc.


PROGRAM
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>

void main()
{
int gd=DETECT,gm;
int x=10,y=480;
initgraph(&gd,&gm,"..\\bgi");
while(!kbhit())
{
cleardevice();
if(y==0)
{
y=random(480);
x=random(640);
}
else
{
y=y-1;
x=x+1;
line(x-50,y,x,y-70);
line(x,y-70,x+50,y);
line(x+50,y,x,y+70);
line(x,y+70,x-50,y);
line(x,y-70,x,y+70);
line(x,y+70,x+10,y+140);
line(x,y+70,x-10,y+140);
line(x-50,y,x+50,y);
line(x,y,x+130,y+640);
}
delay(20);
}
closegraph();
restorecrtmode();
}

2. To draw different shapes such as hut.

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

int main(){
int gd = DETECT,gm;
initgraph(&gd, &gm, "X:\\TC\\BGI");
/* Draw Hut */
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);

line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);

/* Fill colours */
setfillstyle(SOLID_FILL, BROWN);
floodfill(152, 182, WHITE);
floodfill(252, 182, WHITE);
setfillstyle(SLASH_FILL, BLUE);
floodfill(182, 252, WHITE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
floodfill(210, 105, WHITE);

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

3. To draw different shapes such as fish.

PROGRAM
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm ,"");
ellipse(200,200,0,360,50,30);
line(250,200,280,170);
line(280,170,280,230);
line(280,230,250,200);
circle(160,190,3);
getch();
closegraph();
}

You might also like