Computer Graphics Lab Manual For CSE Sixth Semester
Computer Graphics Lab Manual For CSE Sixth Semester
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
void draw(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2;
int gdriver=DETECT,gmode,gerror;
initgraph(&gdriver,&gmode,"d:\\tc");
printf("\n Enter the x and y value for starting point:\n");
scanf("%d%d",&x1,&y1);
printf("\n Enter the x and y value for ending point:\n");
scanf("%d%d",&x2,&y2);
printf("\n The Line is shown below: \n");
draw(x1,y1,x2,y2);
getch();
}
void draw(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k;
int step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(x,y,1);
for(k=1;k<=step;k++)
{
delay(100);
x=x+xinc;
y=y+yinc;
putpixel(x,y,2);
}
}
*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm;
int dx, dy, p, end;
float x1, x2, y1, y2, x, y;
initgraph(&gd, &gm, "c:\tc\bgi");
printf("Enter Value of X1: ");
scanf("%f", &x1);
printf("Enter Value of Y1: ");
scanf("%f", &y1);
printf("Enter Value of X2: ");
scanf("%f", &x2);
printf("Enter Value of Y2: ");
scanf("%f", &y2);
dx = abs(x1 - x2);
dy = abs(y1 - y2);
p = 2 * dy - dx;
if(x1 > x2)
{
x = x2;
y = y2;
end = x1;
}
else
{
x = x1;
y = y1;
end = x2;
}
putpixel(x, y, 10);
while(x < end)
{
delay(100);
x = x + 1;
if(p < 0)
{
p = p + 2 * dy;
}
else
{
y = y + 1;
p = p + 2 * (dy - dx);
}
putpixel(x, y, 10);
}
getch();
closegraph();
}
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void circlemid(int xc,int yc,int r);
void plotpoints(int xc,int yc,int x,int y);
void plotpoints(int xc,int yc,int x,int y)
{
delay(200);
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc-x,yc-y,10);
putpixel(xc+y,yc+x,10);
putpixel(xc-y,yc+x,10);
putpixel(xc+y,yc-x,10);
putpixel(xc-y,yc-x,10);
}
void circlemid(int xc,int yc,int r)
{
int p,x,y;
x=0;
y=r;
plotpoints(xc,yc,x,y);
p=1-r;
while(x<y)
{
x++;
if(p<0)
p=p+2*x+1;
else
{
p=p+2*(x-y)+1;
y--;
}
plotpoints(xc,yc,x,y);
}
}
void main()
{
int xc,yc,r;
int gdriver,gmode;
gdriver=DETECT;
initgraph(&gdriver,&gmode,"");
printf("Enter the center and radius:");
scanf("%d%d%d",&xc,&yc,&r);
circlemid(xc,yc,r);
getch();
closegraph();
}
/*
*/
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
long d1,d2;
int i,gd=DETECT,gm,x,y,x0,y0;
long rx,ry,rxsq,rysq,tworxsq,tworysq,dx,dy;
initgraph(&gd,&gm,"d:\\tc\\BGI");
printf("Enter the X radius and Y radius of the Ellipse:\n");
scanf("%ld%ld",&rx,&ry);
printf("\nEnter the center (x,y) of the ellipse:\n");
scanf("%d%d",&x0,&y0);
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
{
delay(100);
putpixel(x0+x,y0+y,15);
putpixel(x0-x,y0-y,15);
putpixel(x0+x,y0-y,15);
putpixel(x0-x,y0+y,15);
if(d1<0)
{
x=x+1;
y=y;
dx=dx+tworysq;
d1=d1+dx+rysq;
}
else
{
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
{
delay(100);
putpixel(x0+x,y0+y,15);
putpixel(x0-x,y0-y,15);
putpixel(x0+x,y0-y,15);
putpixel(x0-x,y0+y,15);
if(d2>0)
{
x=x;
y=y-1;
dy=dy-tworxsq;
d2=d2-dy+rxsq;
}
else
{
x=x+1;
y=y-1;
dx=dx+tworysq;
dy=dy-tworxsq;
d2=d2+dx-dy+rxsq;
}
}
while(y>0);
getch();
closegraph();
}
/*
*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<graphics.h>
#include<math.h>
#define PI 3.14159265;
void display(int *,int *);
void trans(int *,int *);
void rotate(int *,int *);
void scale(int *,int *);
void mux(float a[5][5],int x[],int y[]);
void main()
{
int x[5],y[5],ch,q;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
do
{
clrscr();
x[0]=250;
y[0]=200;
x[1]=350;
y[1]=200;
x[2]=300;
y[2]=100;
printf("\tTwo Dimensional Transformation\n\n");
printf("\t* * * MENU * * *");
printf("\n\t1:Translation\n\t2:Rotation\n\t3:Scaling\n\t4:Exit\n");
printf("Enter the choice of transformation:");
scanf("%d",&ch);
switch(ch)
{
case 1:
clrscr();
printf("\nBefore
setcolor(BLUE);
display(x,y);
trans(x,y);
break;
case 2:
clrscr();
printf("\nBefore
setcolor(BLUE);
display(x,y);
rotate(x,y);
break;
case 3:
clrscr();
printf("\nBefore
setcolor(BLUE);
display(x,y);
scale(x,y);
break;
case 4:
exit(0);
break;
default:
printf("\nChoice
}
Translation:\n\n\n\n\n\n");
Rotation:\n\n\n\n\n\n");
Scaling:\n\n\n\n\n\n");
Incorrect...\n");
}
while(ch!=4);
getch();
closegraph();
}
void trans(int x[],int y[])
{
int i,tx,ty;
printf("\nEnter the translation factors:\n");
scanf("%d%d",&tx,&ty);
for(i=0;i<3;i++)
{
x[i]=x[i]+tx;
y[i]=y[i]+ty;
}
printf("\nAfter Translation:\n");
setcolor(RED);
display(x,y);
}
void rotate(int x[],int y[])
{
int i,b[5],c[5];
float theta,rad,a[5][5];
printf("\nEnter the rotation angle:\n");
scanf("%f",&theta);
theta=theta*3.14;
theta=theta/180;
a[0][0]=cos(theta);
a[0][1]=(-1)*sin(theta);
a[1][0]=sin(theta);
a[1][1]=cos(theta);
mux(a,x,y);
}
void scale(int x[],int y[])
{
float a[5][5];
float sx,sy;
printf("Enter the scaling factors:\n");
scanf("%f%f",&sx,&sy);
a[0][0]=sx;
a[0][1]=0;
a[1][0]=0;
a[1][1]=sy;
mux(a,x,y);
}
void mux(float a[5][5],int x[],int y[])
{
int n,i,k,b[5],c[5];
for(n=0;n<3;n++)
{
b[0]=x[n];
b[1]=y[n];
for(i=0;i<2;i++)
{
c[i]=0;
for(k=0;k<2;k++)
{
c[i]=c[i]+a[i][k]*b[k];
}
}
x[n]=c[0];
y[n]=c[1];
}
setcolor(RED);
display(x,y);
}
void display(int x[],int y[])
{
int i;
line(x[0],y[0],x[1],y[1]);
line(x[1],y[1],x[2],y[2]);
line(x[2],y[2],x[0],y[0]);
getch();
}
/*
*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
setcolor(GREEN);
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
setcolor(RED);
}
void main()
{
int gd,gm,x,y,z,o,x1,x2,y1,y2,ch,q;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:/tc/bgi");
setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
do
{
clrscr();
printf("\tTHREE DIMENSIONAL TRANSFORMATION\n");
printf("\t* * * MENU * * *\n");
printf("\t\t1:Translation\n\t\t2:Scaling\n\t\t3:Rotation\n\t\t4:Exit");
printf("\nEnter the choice:");
scanf("%d",&ch);
clrscr();
axis();
setcolor(RED);
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
switch(ch)
{
case 1:
printf("\nTRANSLATION\n");
printf("\nEnter the Translation Factors:");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("\nAfter Translation");
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
setcolor(WHITE);
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);
break;
case 2:
printf("\nSCALING\n");
printf("\nEnter the Scaling Factors:");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("\nAfter Scaling");
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
setcolor(WHITE);
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);
break;
case 3:
printf("\nROTATION\n");
printf("\nEnter the Rotating 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("\nAfter Rotating about Z-axis");
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
setcolor(WHITE);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
axis();
printf("\nAfter rotating about X-axis");
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
setcolor(WHITE);
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
axis();
printf("\nAfter rotating about Y-axis");
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
setcolor(WHITE);
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
break;
case 4:
exit(0);
break;
default:
printf("\nChoice Incorrect...\n");
}
getch();
clrscr();
}
while(ch!=4);
getch();
closegraph();
}
*/
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define MAX 20
enum { TOP = 0x8, BOTTOM = 0x4, RIGHT = 0x2, LEFT = 0x1 };
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)
{
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("\tCOHEN SUTHERLAND LINE CLIPPING ALGORITHM\n");
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]);
*/
#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");
printf("\n\t5) ABOUT -X=Y");
printf("\n\t6) EXIT");
printf("\n\tENTER YOUR CHOICE : ");
scanf("%d",&ch);
clrscr();
cleardevice();
disp(n,c);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
b[i][j]=0;
if(i==j)
b[i][j]=1;
}
switch(ch)
{
case 1:
b[1][1]=-1;
break;
case 2:
b[0][0]=-1;
break;
case 3:
b[0][0]=-1;
b[1][1]=-1;
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();
disp(n,c);
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," ");
printf("\n\tTWO DIMENSIONAL TRANSFORMATION\n");
printf("\n\tREFLECTION AND SHEARING\n");
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\tTWO DIMENSIONAL TRANSFORMATION\n");
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();
}