Sie sind auf Seite 1von 28

engineerportal.blogspot.

in

computer graphics lab manual

1.Bresenhams line drawing algorithm :


ALGORITHM :
Step 1 : Start.
Step 2 : Initialize the graphics header files and functions.
Step 3 : Declare the required variables and functions.
Step 4 : Get the four points for drawing a line namely x1,x2,y1,y2.
Step 5 : Draw the line using the algorithm.
Step 6 : Display the output.
Step 7 : stop.
PROGRAM :
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
int gd=DETECT,gm;
int xa,xb,ya,yb;
int dx,dy,x,y,xend,p;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter The Two Left
Endpoints(xa,ya):\n");
scanf("%d%d",&xa,&ya);
printf("Enter The Two Right
Endpoints(xb,yb):\n");
scanf("%d%d",&xb,&yb);
dx=abs(xa-xb);
dy=abs(ya-yb);
p=2*dy-dx;
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
OUTPUT :
Enter The Two Left Endpoints(xa,ya):

234 124

Enter The Two Right Endpoints(xb,yb): 578 321

else
x=xa;
y=ya;
xend=xb;
putpixel(x,y,6);
while(x<xend)
{
x=x+1;
if(p<0)
{
p=p+2*dy;
}
else
{
y=y+1;
p=p+2*(dy-dx);
}
putpixel(x,y,6);
}
getch();
return(0);

engineerportal.blogspot.in

computer graphics lab manual

Bresenhams circle drawing algorithm


ALGORITHM :
Step 1 : Start.
Step 2 : Initialize the graphics header files and functions.
Step 3 : Declare the required variables and functions.
Step 4 : Get the co-ordinates and radius of the circle.
Step 5 : Draw the circle using the algorithm.
Step 6 : Display the output.
Step 7 : stop.
PROGRAM :

#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
int gd=DETECT,gm;
int xcenter,ycenter,radius;
int p,x,y;
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=0;
printf("Enter The Radius Value:\n");
scanf("%d",&radius);
y=radius;
printf("Enter The xcenter and
ycenter Values:\n");
scanf("%d%d",&xcenter,&ycenter);
plotpoints(xcenter,ycenter,x,y);
p=1-radius;
while(x<y)
{
if(p<0)
x=x+1;
OUTPUT :
Enter The Radius Value
: 80
Enter The xcenter and ycenter Values : 230 260

else
x=x+1;
y=y-1;

}
if(p<0)
p=p+2*x+1;
else
p=p+2*(x-y)+1;
plotpoints(xcenter,ycenter,x,y);
}
getch();
return(0);
}
int plotpoints(int xcenter,int ycenter,int
x,int y)
{
putpixel(xcenter+x,ycenter+y,1);
putpixel(xcenter-x,ycenter+y,1);
putpixel(xcenter+x,ycenter-y,1);
putpixel(xcenter-x,ycenter-y,1);
putpixel(xcenter+y,ycenter+x,1);
putpixel(xcenter-y,ycenter+x,1);
putpixel(xcenter+y,ycenter-x,1);
}

engineerportal.blogspot.in

computer graphics lab manual

Bresenhams ellipse drawing algorithm:


ALGORITHM :
Step 1 : Start.
Step 2 : Initialize the graphics header files and functions.
Step 3 : Declare the required variables and functions.
Step 4 : Get the co-ordinates and radius of the ellipse.
Step 5 : Draw the ellipse using the algorithm.
Step 6 : Display the output.
Step 7 : stop.
PROGRAM :
#include "stdio.h"
p=p+ry1+px-py;
#include "conio.h"
plotpoints(xcenter,ycenter,x,y);
#include "math.h"
p=(ry1*(x+0.5)*(x+0.5)+rx1*(y#include "graphics.h"
1)*(y-1)-rx1*ry1);
main()
while(y>0)
{
{
int gd=DETECT,gm;
y=y-1;
int xcenter,ycenter,rx,ry;
py=py-rx2;
int p,x,y,px,py,rx1,ry1,rx2,ry2;
if(p<=0)
initgraph(&gd,&gm,"c:\\tc\\bgi");
{
printf("Enter The Radius Value:\n");
x=x+1;
scanf("%d%d",&rx,&ry);
px=px+ry2;
printf("Enter The xcenter and ycenter
}
Values:\n");
if(p>0)
scanf("%d%d",&xcenter,&ycenter);
p=p+rx1-py;
ry1=ry*ry;
else
rx1=rx*rx;
p=p+rx1-py+px;
ry2=2*ry1;
rx2=2*rx1;
plotpoints(xcenter,ycenter,x,y);
x=0;
}
y=ry;
}
plotpoints(xcenter,ycenter,x,y);
getch();
p=(ry1-rx1*ry+(0.25*rx1));
return(0);
px=0;
}
py=rx2*y;
while(px<py)
int plotpoints(int xcenter,int ycenter,int
{
x,int y)
x=x+1;
{
px=px+ry2;
putpixel(xcenter+x,ycenter+y,6);
if(p>=0)
putpixel(xcenter-x,ycenter+y,6);
y=y-1;
putpixel(xcenter+x,ycenter-y,6);
py=py-rx2;
putpixel(xcenter-x,ycenter-y,6);
if(p<0)
p=p+ry1+px;
}
else
OUTPUT :
Enter The Radius Value(Rx,Ry)
: 10 30
Enter The xcenter and ycenter Values : 300 150

engineerportal.blogspot.in

2D transformations
a.Translation, Reflection, and Shear
b. Rotation (With and without pivot
point), Scaling (With and without pivot
point)
c. Translation, Scaling, Rotation
d. fixed point scaling, fixed point
rotation
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void translation(x,y,x1,y1,x2,y2)
{
int tx,ty,xx,yy;
printf("Enter transformational values:");
scanf("%d%d",&tx,&ty);
line(x+tx,y+ty,x1+tx,y1+ty);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x+tx,y+ty);
}
void scaling(x,y,x1,y1,x2,y2)
{
float sx,sy;
printf("Enter the scaling vertices:");
scanf("%f%f",&sx,&sy);
line(x*sx,y*sy,x1*sx,y1*sy);
line(x1*sx,y1*sy,x2*sx,y2*sy);
line(x2*sx,y2*sy,x*sx,y*sy);
}
void scalingpivot(x,y,x1,y1,x2,y2)
{
float sx,sy,xf,yf;
int xp,yp,xp1,yp1,xp2,yp2;
printf("Enter the scaling vertices:");
scanf("%f%f",&sx,&sy);
printf("Enter the scaling pivot points:");
scanf("%f%f",&xf,&yf);
xp=(x*sx)+xf*(1-sx);
yp=(y*sy)+yf*(1-sy);
xp1=(x1*sx)+xf*(1-sx);
yp1=(y1*sy)+yf*(1-sy);

computer graphics lab manual

xp2=(x2*sx)+xf*(1-sx);
yp2=(y2*sy)+yf*(1-sy);
line(x+xp,y+yp,x1+xp1,y1+yp1);
line(x1+xp1,y1+yp1,x2+xp2,y2+yp2);
line(x2+xp2,y2+yp2,x+xp,y+yp);
}
void rotation(x,y,x1,y1,x2,y2)
{
int the,xr,yr,xr1,yr1,xr2,yr2;
float rad;
printf("Enter the theta value:");
scanf("%d",&the);
rad=the*(3.14/180);
xr=x*cos(rad)-y*sin(rad);
yr=x*sin(rad)+y*cos(rad);
xr1=x1*cos(rad)-y1*sin(rad);
yr1=x1*sin(rad)+y1*cos(rad);
xr2=x2*cos(rad)-y2*sin(rad);
yr2=x2*sin(rad)+y2*cos(rad);
line(x+xr,y+yr,x1+xr1,y1+yr1);
line(x1+xr1,y1+yr1,x2+xr2,y2+yr2);
line(x2+xr2,y2+yr2,x+xr,y+yr);
}
void rotationpivot(x,y,x1,y1,x2,y2)
{
int the,xr,yr,xr1,yr1,xr2,yr2,xf,yf;
float rad;
printf("Enter the theta value:");
scanf("%d",&the);
printf("\n Enter the pivot points:");
scanf("%d%d",&xf,&yf);
rad=the*(3.14/180);
xr=xf+(x-xf)*cos(rad)-(y-yf)*sin(rad);
yr=yf+(x-xf)*sin(rad)-(y-yf)*cos(rad);
xr1=xf+(x1-xf)*cos(rad)-(y1yf)*sin(rad);
yr1=yf+(x1-xf)*sin(rad)-(y1yf)*cos(rad);
xr2=xf+(x2-xf)*cos(rad)-(y2yf)*sin(rad);
yr2=yf+(x2-xf)*sin(rad)-(y2yf)*cos(rad);
line(x+xr,y+yr,x1+xr1,y1+yr1);

engineerportal.blogspot.in

computer graphics lab manual

line(x1+xr1,y1+yr1,x2+xr2,y2+yr2);
line(x2+xr2,y2+yr2,x+xr,y+yr);

printf(" 4.Rotation\n 5.Rotation


with pivot\n 6.Reflection\n");
printf(" 7.Shearing\n 8.Exit\n");
printf("Enter ur choice:");
scanf("%d",&c);
switch(c)
{

}
void reflection(x,y,x1,y1,x2,y2)
{
line(y,x,y1,x1);
line(y1,x1,y2,x2);
line(y2,x2,y,x);

case 1:
}
void shear(x,y,x1,y1,x2,y2)
{
int s;
//x+=100; y+=100;
//x1+=100; y1+=100;
//x2+=100; y2+=100;
printf("Enter the shear value about x:");
scanf("%d",&s);
line(x,y,x1+s,y1);
line(x1+s,y1,x2,y2);
line(x2,y2,x,y);

translation(x,y,x1,y1,x2,y2);
break;
case 2:
scaling(x,y,x1,y1,x2,y2);
break;
case 3:
scalingpivot(x,y,x1,y1,x2,y2);
break;
case 4:
rotation(x,y,x1,y1,x2,y2);
break;
case 5:

}
void main()
{
int gd,gm,x,y,c,tx,ty,x1,y1,x2,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter the 6 values:");
scanf("%d%d%d%d%d%d",&x,&y,&x1,
&y1,&x2,&y2);
printf("Actual polygon");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
do
{
printf("\nMenu\n 1.Translation\n
2.Scaling\n 3.Scaling with pivot\n");

Ex number : 5-a,6-b,7-c,8-d

rotationpivot(x,y,x1,y1,x2,y2);
break;
case 6:
reflection(x,y,x1,y1,x2,y2);
break;
case 7:
shear(x,y,x1,y1,x2,y2);
break;
case 8:
exit(0);
break;
}
}while(c<8);
getch();
closegraph();
}

2 d trans

engineerportal.blogspot.in

computer graphics lab manual

Implementation of Cohen-Sutherland Line Clipping Algorithm


Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
typedef struct coderegion
{
int x,y;
char code[4];
}CR;
CR setregioncode(CR c)
{
CR ctemp;
int
xmin=100,xmax=500,ymin=150,ymax=400;
if(c.x<xmin)
{
ctemp.code[0]='1';
printf("\nbit1= %c",ctemp.code[0]);
}
else
{
ctemp.code[0]='0';
printf("\nbit1= %c",ctemp.code[0]);
}
if(c.x>xmax)
{
ctemp.code[1]='1';
printf("\nbit2= %c",ctemp.code[1]);
}
else
{
ctemp.code[1]='0';
printf("\nbit2= %c",ctemp.code[1]);
}
if(c.y<ymin)
{
ctemp.code[2]='1';
printf("\nbit3= %c",ctemp.code[2]);
}
else
{
ctemp.code[2]='0';
printf("\nbit3= %c",ctemp.code[2]);
}
if(c.y>ymax)
{
ctemp.code[3]='1';
printf("\nbit4= %c",ctemp.code[3]);
}

else
{

ctemp.code[3]='0';
printf("\nbit4= %c",ctemp.code[3]);

}
ctemp.x=c.x;
ctemp.y=c.y;
return(ctemp);

int objectvisibility(CR c1,CR c2)


{
int i,flag=0;
for(i=0;i<4;i++)
{
if((c1.code[i]=='1')||(c2.code[i]=='1'))
{
flag=1;
}
}
if(flag==0)
return 0;
for(i=0;i<4;i++)
{
if((c1.code[i]==c2.code[i])&&(c1.code[i]=='1')
)
{
flag=0;
}
}
if(flag==0)
return 1;
return 2;
}
CR swapendpoint(CR c1,CR c2)
{
CR temp;
int x,y,i;
float m,k;
int
xmin=100,xmax=500,ymin=150,ymax=400;
if(c1.code[3]=='1')
{
y=ymax;
}
if(c1.code[2]=='1')

engineerportal.blogspot.in
{

y=ymin;

}
if((c1.code[3]=='1')||(c1.code[2]=='1'))
{
m=(float)(c2.y-c1.y)/(c2.x-c1.x);
printf("\n slope value = %f",m);
k=(float)c1.x+(float)(y-c1.y)/m;
printf("\n x value = %f",k);
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=c1.code[i];
return(temp);
}
if(c1.code[1]=='1')
{
x=xmax;
}
if(c1.code[0]=='1')
{
x=xmin;
}
if((c1.code[1]=='1')||(c1.code[0]=='1'))
{
m=(float)(c2.y-c1.y)/(c2.x-c1.x);
printf("\n slope value = %f",m);
k=c1.y+(m*(x-c1.x));
printf("\n y value = %f",k);
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=c1.code[i];
return(temp);
}

void main()
{
int gd,gm,v;
int
xmin=100,xmax=500,ymin=150,ymax=400;
CR c1,c2,c3,ctemp;

computer graphics lab manual


detectgraph(&gd,&gm);
initgraph(&gd,&gm,"G:\\tc\\bgi");
rectangle(xmin,ymin,xmax,ymax);
outtextxy(200,20,"COHEN-SUTHERLAND
LINE CLIPPING");
printf("\n\n\n Enter the end points:");
scanf("%d%d%d%d",&c1.x,&c1.y,&c2.x,&c2.y
);
line(c1.x,c1.y,c2.x,c2.y);
c1=setregioncode(c1);
c2=setregioncode(c2);
v=objectvisibility(c1,c2);
printf("\nvisibility =%d",v);
switch(v)
{
case 0:
rectangle(xmin,ymin,xmax,ymax);
line(c1.x,c1.y,c2.x,c2.y);
break;
case 1:
clearviewport();
outtextxy(200,20,"AFTER
CLIPPING");
c1=setregioncode(c1);
c2=setregioncode(c2);
rectangle(xmin,ymin,xmax,ymax);
break;
case 2:
clearviewport();
outtextxy(200,20,"AFTER
CLIPPING");
c1=swapendpoint(c1,c2);
c2=swapendpoint(c2,c1);
rectangle(xmin,ymin,xmax,ymax);
line(c1.x,c1.y,c2.x,c2.y);
break;
}
getch();
closegraph();
}

OUTPUT:

engineerportal.blogspot.in

computer graphics lab manual

Sutherland-Hodgeman Polygon clipping Algorithm


//Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
enum area{left,right,top,bottom};
typedef struct
{
double x,y;
} points;
points outvertex[10];
points vertex[10];
int max=0;
int n;
enum area id;
void sutherclip(int,int,int,int);
points intersect(int,points,points);
int inside(int,points);
int inside(int clipbound,points s)
{
int pos=0;
switch(id)
{
case left:
if(s.x>clipbound)
pos=1;
break;

break;

break;

break;
}

case right:
if(s.x<clipbound)
pos=1;
case top:
if(s.y>clipbound)
pos=1;
case bottom:
if(s.y<clipbound)
pos=1;
}
return(pos);

points intersect(int clipbound,points s ,points


p)
{
points temp; double calc;
switch(id)
{
case left:
case right:
temp.x=clipbound;
temp.y=s.y+(p.y-s.y)*(clipbound-s.x)/(p.xs.x);
break;
case bottom:
case top:
temp.y=clipbound;
temp.x=s.x+(p.x-s.x)*(clipbound-s.y)/(p.ys.y);
break;
}
return temp;
}
void clip(int xmin,enum area id1)
{
int i;
points temp;
points s,p;
int pt1,pt2;
id=id1;
for(i=0;i<n;i++)
{
s=vertex[i];
if(i==n-1)
p=vertex[0];
else
p=vertex[i+1];
pt1=inside(xmin,s);
pt2=inside(xmin,p);
if(pt1==1 && pt2==1)
outvertex[max++]=p;
if(pt1==0 && pt2==1)
{
temp=intersect(xmin,s,p);
outvertex[max++]=temp;
outvertex[max++]=p;
}
if(pt1==1 && pt2==0)

engineerportal.blogspot.in
{
}

temp=intersect(xmin,s,p);
outvertex[max++]=temp;

}
n=max;
for(i=0;i<max;i++)
vertex[i]=outvertex[i];
max=0;
}
void sutherclip(int xmin,int xmax,int ymin,int
ymax)
{
clip(xmin,left);
clip(xmax,right);
clip(ymin,top);
clip(ymax,bottom);
}
void main()
{
int xmin,xmax,ymin,ymax,i;
int gd=DETECT,gm;
sin(1);
clrscr();
printf("Enter the co ordinates of
clipping window....\n");
scanf("%d %d %d %d"
,&xmin,&ymin,&xmax,&ymax);
printf("\nEnter the no of vertices of
the polygon....\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{

Output:

computer graphics lab manual


printf("Enter the x & y co
ordinates of vertex %d \n",i+1);
scanf("%lf %lf",&vertex[i].x
,&vertex[i].y);
}
initgraph(&gd,&gm,"d:\\tc\\bgi");
setbkcolor(7);
printf("\n\nBefore Clipping");
rectangle(xmin,ymin,xmax,ymax);
for(i=0;i<n-1;i++)
{
line(vertex[i].x,vertex[i].y,vertex[i+1].x
,vertex[i+1].y);
line(vertex[n-1].x,vertex[n1].y,vertex[0].x,vertex[0].y);
}
getch();
sutherclip(xmin,xmax,ymin,ymax);
cleardevice();
printf("After clipping");
rectangle(xmin,ymin,xmax,ymax);
for(i=0;i<n-1;i++)
{
line(outvertex[i].x,outvertex[i].y,outve
rtex[i+1].x,outvertex[i+1].y);
line(outvertex[n1].x,outvertex[n1].y,outvertex[0].x,outvertex[0].y);
}
getch();
closegraph();
}

engineerportal.blogspot.in

Implementation of 3D Transformations

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void draw(float x1,float y1,float
x2,float y2,float z)
{
bar3d(x1,y1,x2,y2,z,1);
}
void z_rotate(float x1,float y1,float
x2,float y2,float z,float r)
{
float a,b,c,d;
a=(x1*cos(r))-(y1*sin(r));
b=(x2*cos(r))-(y2*sin(r));
c=(x1*sin(r))+(y1*cos(r));
d=(x2*sin(r))+(y2*cos(r));
draw(a,c,b,d,z);
}
void main()
{
int gd,gm,s,ch;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"E:\\TC\\BGI");
do
{
float
x1=200,x2=300,y1=200,y2=300,z=1
00,tx,ty,sx,sy,sz,o,r;
clrscr();
printf("\n\n\n\t\t 3D
Transformations");

computer graphics lab manual

printf("\n\n1.3D
Translation\n2.3D Scaling\n3.3D
Rotation \n4.Exit");
printf("\n\nEnter Ur
Option:");
scanf("%d",&ch);
clrscr();
switch(ch)
{
case 1:
printf("\n\nEnter Translation
Values(tx,ty):");
scanf("%f %f",&tx,&ty);
printf("\nBefore Translation");
setcolor(10);
draw(x1,y1,x2,y2,z);
sleep(3);
printf("\nAfter Translation");
setcolor(12);
draw(x1+tx,y1+ty,x2+tx,y2+ty,z);
break;
case 2:
printf("\n\nEnter the scaling
Values(sx,sy,sz):");
scanf("%f %f %f",&sx,&sy,&sz);
printf("\nBefore Scaling");
setcolor(4);
draw(x1,y1,x2,y2,z);
sleep(3);
printf("\nAfter Scaling");
x1*=sx;
y1*=sy;
x2*=sx;
y2*=sy;
z*=sz;
setcolor(6);

engineerportal.blogspot.in

computer graphics lab manual

draw(x1,y1,x2,y2,z);
break;

case 4:
exit(0);

case 3:
printf("\n\nEnter the rotation
angle:");
scanf("%f",&o);
printf("\nBefore Rotation");
draw(x1,y1,x2,y2,z);
sleep(3);
printf("\nAfter Rotation");
r=(o*3.14)/180;
z_rotate(x1,y1,x2,y2,z,r);
break;

default:
printf("\n\nEnter
a valid choice");
}
printf("\n\nPress 1 to
continue:");
scanf("%d",&s);
}
while(s==1);
getch();
closegraph();
}

Output:

Enter the optin

translation

Scaling

rotation

engineerportal.blogspot.in

computer graphics lab manual

Implementation of composite 3D Transformations


Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
struct pt
{
float x;float y;
};
float thematrix[3][3];
void print(float t[][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%f\t",t[i][j]);
printf("\n");
}
}
void matrixsetidentity(float m[][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
m[i][j]=1;
else
m[i][j]=0;
}
}
}
void premul(float a[][3],float b[][3])
{
int r,c,z=20;
float tmp[3][3];
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)

tmp[r][c]=a[r][0]*b[0][c]+a[r][1]*b
[1][c]+a[r][2]*b[2][c];
}
}
for(r=0;r<3;r++)
for(c=0;c<3;c++)
b[r][c]=tmp[r][c];
z=z*a[2][2];
}
void triangle(struct pt *p1)
{
line(p1[0].x,p1[0].y,p1[1].x,p1[1].y);
line(p1[1].x,p1[1].y,p1[2].x,p1[2].y);
line(p1[2].x,p1[2].y,p1[0].x,p1[0].y);
}
void translate(int tx,int ty)
{
float m[3][3];
matrixsetidentity(m);
m[0][2]=tx;
m[1][2]=ty;
premul(m,thematrix);
}
void scaling()
{
float m[3][3],sx,sy,sz;
matrixsetidentity(m);
printf("\nEnter the scaling with respect to
x-axis:");
scanf("%f%f%f",&sx,&sy,&sz);
m[0][0]=sx;
m[1][1]=sy;
m[2][2]=sz;
premul(m,thematrix);
}
void rotation()
{
float m[3][3];

engineerportal.blogspot.in

float rad;
int a;
printf("\nEnter rotation angle");
scanf("%d",&a);
rad=a*(3.14/180);
matrixsetidentity(m);
m[0][0]=cos(rad);
m[0][1]=-sin(rad);
m[1][0]=sin(rad);
m[1][1]=cos(rad);
premul(m,thematrix);
}
void transformpts(int npts,struct pt *p1)
{
int k;
float tmp;
for(k=0;k<npts;k++)
{
tmp=thematrix[0][0]*p1[k].x+thematrix[0]
[1]*p1[k].y+thematrix[0][2];
p1[k].y=thematrix[1][0]*p1[k].x+thematri
x[1][1]*p1[k].y+thematrix[1][2];
p1[k].x=tmp;

computer graphics lab manual

p1[1].x=250.0;p1[1].y=150.0;
z=20;
do
{
printf("\n1.Translation\n2.Scaling\n3.Rotat
ion\n4.exit\n");
printf("Enter ur option");
scanf("%d",&ch);
switch(ch)
{
case 1:
cleardevice();
d3d(p1,z);
printf("Enter translation first
coordinates\n");
scanf("%d %d",&tx,&ty);
translate(tx,ty);
transformpts(3,p1);
d3d(p1,z);
printf("Enter translation second
coordinates\n");
scanf("%d %d",&tx1,&ty1);
translate(tx1,ty1);
transformpts(3,p1);
d3d(p1,z);
break;

}
}
void d3d(struct pt* p1,int z)
{

case 2:
cleardevice();
d3d(p1,z);
scaling();
transformpts(3,p1);
d3d(p1,z);
scaling();
transformpts(3,p1);
d3d(p1,z);
break;

bar3d(p1[0].x,p1[0].y,p1[1].x,p1[1].y,z,1);
}
void main()
{
int gd,gm;
int tx,ty,tx1,ty1,z;
int xf,yf,ch;
struct pt p1[3];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
matrixsetidentity(thematrix);
p1[0].x=150.0;p1[0].y=100.0;

case 3:
cleardevice();
d3d(p1,z);
rotation();
transformpts(3,p1);
d3d(p1,z);
rotation();

engineerportal.blogspot.in

transformpts(3,p1);
d3d(p1,z);
break;
case 4:
break;

Output:

computer graphics lab manual

}
}while(ch<4);
getch();
closegraph();
}

engineerportal.blogspot.in

computer graphics lab manual

Projection 3d
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd,gm;
int tlx,tly,brx,bry,d;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\
bgi");
printf("Enter the input for 3D
object");
scanf("%d %d %d %d
%d",&tlx,&tly,&brx,&bry,&d);
bar3d(tlx,tly,brx,bry,d,1);

Output:

sleep(3);
//getch();
cleardevice();
printf("Front View");
bar3d(tlx,tly,brx,bry,0,0);
sleep(3);
//getch();
cleardevice();
printf("Top view");
bar3d(tlx,tly-d,brx,tly,0,0);
sleep(3);
//getch();
cleardevice();
printf("Side View");
bar3d(brx,tly,brx+d,bry,0,0);
getch();
closegraph();
}

engineerportal.blogspot.in

//chain of diamonds
#include <gl/Glut.h>
typedef struct diamond
{
GLint x,y;
}center;
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
void myDisplay(void)
{
int size=50,n=5,i;
glClear(GL_COLOR_BUFFER_BIT);
center cen;
cen.x=26,cen.y=30;
for(i=0;i<5;i++)
{
glBegin(GL_LINE_LOOP);
glVertex2i(cen.x-size/2,cen.y);
glVertex2i(cen.x,cen.y+size/2);
glVertex2i(cen.x+size/2,cen.y);
glVertex2i(cen.x,cen.y-size/2);
glEnd();
cen.x=cen.x+size;
}
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,150);
glutCreateWindow("MY First");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}
//output

computer graphics lab manual

engineerportal.blogspot.in
//chessboard
#include<GL/glut.h>
void myInit(void)
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,480.0,0.0,480.0);
}
void board(int size)
{
int i=0,k=0;
for(i=0;i<8;++i)
{
for(k=0;k<8;++k)
{
if((i+k)%2==0)
glColor3f(1.0,1.0,1.0);
else
glColor3f(0.0,0.0,0.0);
glRecti(i*size,k*size,(i+1)*size,(k+1)*
size);
}
}
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE);
board(60);
glEnd();
glFlush();
}

void main(int argc,char** argv)


{
glutInit(&argc, argv);

computer graphics lab manual


glutInitDisplayMode(GLUT_SINGLE|
GLUT_RGB);
glutInitWindowSize(480,480);
glutInitWindowPosition(100,150);
glutCreateWindow("Checker board");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}
Output:

engineerportal.blogspot.in

computer graphics lab manual

glVertex2i(pointx,pointy);
glEnd();
//j++;
}

CODING:
#include<stdlib.h>
#include<GL/glut.h>
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
int random(int m)
{
return rand()%m;
}
void myDisplay(void)
{
//int i;
//int j=300,k=200;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2i(0,0);
glVertex2i(0,400);
glVertex2i(500,400);
glVertex2i(500,0);
glEnd();

glFlush();
}
int main(int argc, char** argv)
{
//Initialise GLUT with command-line parameters.
glutInit(&argc, argv);
//Set Display Mode
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
//Set the window size
glutInitWindowSize(640,480);
//Set the window position
glutInitWindowPosition(100,100);
//Create the window
glutCreateWindow("CONSTELLATION OF 500
RANDOM DOTS WITHIN A WINDOW");
//Call init (initialise GLUT
//Call "display" function
glutDisplayFunc(myDisplay);

int tx[3];//={200,500,200};
int ty[3];//={400,50,50};
int index=random(500);
int pointx;//=tx[index];
int pointy;//=ty[index];
for(int i=0,j=0;i<500;i++)
{
pointx=random(500);
pointy=random(400);
glBegin(GL_POINTS);

OUTPUT:

myInit();

glViewport(0,0,640,480);
//Enter the GLUT event loop
glutMainLoop();
//exit(0);
return 0;

engineerportal.blogspot.in

computer graphics lab manual

DRAWING RANDOM DIAMONDS


USING OPEN GL
#include<stdlib.h>
#include<GL/glut.h>
struct point
{
int x,y;
}centre={100,100};
int random(int n)
{
return rand()%n;
}
void myinit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTIO
N);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
void myDisplay(void)
{
GLint size=100;
glClear(GL_COLOR_BUFFER_B

IT);
for(int i=0;i<=20;i++)
{
centre.x=random(400);
centre.y=random(400);
GLfloat lev1=random(10)/10.0;
OUTPUT:

GLfloat lev2=random(10)/10.0;
GLfloat lev3=random(10)/10.0;
glColor3f(lev1,lev2,lev3);
glBegin(GL_LINE_LOOP);
glVertex2i(centre.xsize/2,centre.y);
2);
y);
size/2);
}

glVertex2i(centre.x,centre.y+size/
glVertex2i(centre.x+size/2,centre.
glVertex2i(centre.x,centre.yglEnd();

glFlush();
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SIN
GLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("DIAMOND")
;
glutDisplayFunc(myDisplay);
myinit();
glutMainLoop();
}

engineerportal.blogspot.in

// Simple OpenGL Program


Sierpinski gasket
Program:
#include<stdlib.h>
#include<GL/glut.h>
struct GLintPoint
{
public:
GLint x,y;
}tri;
void myInit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
int random(int m)
{
return (rand() % m );
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
int r=random(6);
for(int j=0; j<=r; j++)
{
int pt1=random(200);
int pt2=random(250);

Output :

computer graphics lab manual


GLintPoint
T[3]={{150+pt1,50+pt2},{300+pt1,35
0+pt2},{400+pt1,50+pt2}};
int index =random(3);
tri.x=T[index].x;
tri.y=T[index].y;
glBegin(GL_POINTS);
{
for(int i=0;i<1000;i++)
{
index=random(3);
tri.x=(tri.x+T[index].x)/2;
tri.y=(tri.y+T[index].y)/2;
glVertex2i(tri.x,tri.y);
}
}
}
glEnd();
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE
| GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("Sierpinski
gasket");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}

engineerportal.blogspot.in
Village of houses
#include<stdlib.h>
#include<GL/glut.h>
typedef struct village
{
int x,y;
}house;
house peak;
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(3.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
int random(int r)
{
return rand()%r;
}
void parameterizedHouse(GLint width,GLint
height)
{
peak.x=random(300);
peak.y=random(500);
glBegin(GL_LINE_STRIP);
glVertex2i(peak.x-width/2,peak.y-height);
glVertex2i(peak.x-width/2,peak.y-3*height/8);
glVertex2i(peak.x,peak.y);
glVertex2i(peak.x+width/2,peak.y-3*height/8);
glVertex2i(peak.x+width/2,peak.y-height);
glVertex2i(peak.x-width/2,peak.y-height);
glEnd();
Output:

computer graphics lab manual

glBegin(GL_LINE_STRIP);
glVertex2i(((peak.x-width/2)+(.3*width)),peak.yheight);
glVertex2i(((peak.x-width/2)+(.3*width)),((peak.y3*height/8)-(.35*height)));
glVertex2i(((peak.x-width/2)+(.7*width)),((peak.y3*height/8)-(.35*height)));
glVertex2i(((peak.x-width/2)+(.7*width)),peak.yheight);
glEnd();
glBegin(GL_LINES);
glVertex2i(peak.x-width/2,peak.y-3*height/8);
glVertex2i(peak.x+width/2,peak.y-3*height/8);
glEnd();
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
for(int i=0;i<=5;i++)
{
int r1=random(200);
int r2=random(400);
parameterizedHouse(r1,r2);
}
glEnd();
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RG
B);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("Village of houses");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}

engineerportal.blogspot.in

computer graphics lab manual

3d objects
#include <gl.h>
#include<glu.h>
#include<freeglut.h>
#include<windows.h>
#include<process.h>
void axis(double length)
{
glPushMatrix();
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0,0,length);
glEnd();
glTranslated(0,0,length-0.2);
glutWireCone(0.04,0.2,12,9);
glPopMatrix();
}
void display(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0*64/48.0,2.0*64/48.0,2.0,2.0,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(0,0,0);
axis(0.5);
glPushMatrix();
glRotated(90,0,1.0,0);
axis(0.5);
glRotated(-90.0,1,0,0);
axis(0.5);
glPopMatrix();
//BIG CUBE
glPushMatrix();
glTranslated(0.5,0.5,0.5);
glutWireCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslated(1,1,0);
glutWireSphere(0.25,10,8);
glPopMatrix();
glPushMatrix();
glTranslated(1,0,1);

glutWireCone(0.2,0.5,10,8);
glPopMatrix();
glPushMatrix();
glTranslated(1,1,1);
glutWireTeapot(0.1);
glPopMatrix();
glPushMatrix();
glTranslated(0,1,0);
glRotated(90,1,0,0);
glutWireTorus(0.1,0.3,10,10);
glPopMatrix();
glPushMatrix();
glTranslated(1,0,0);
glScaled(0.15,0.15,0.15);
glutWireDodecahedron();
glPopMatrix();
glPushMatrix();
glTranslated(0,1,1);
glutWireCube(0.25);
glPopMatrix();
glPushMatrix();
glTranslated(0,0,1);
GLUquadricObj *q;
q=gluNewQuadric();
gluQuadricDrawStyle(q,GLU_LINE);
gluCylinder(q,0.2,0.2,0.4,8,8);
glPopMatrix();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Windows
Application with GLUT");
glutDisplayFunc(display);
glClearColor(1.0,1.0,1.0,0.0);
glViewport(0,0,640,480);
glutMainLoop();
return 0;
}

engineerportal.blogspot.in

computer graphics lab manual

3d scenes

#include <GL/glut.h>
void wall(double thickness)
{
glPushMatrix();
glTranslated(0.5,0.5*thickness,0.5);
glScaled(1.0,thickness,1.0);
glutSolidCube(1.0);
glPopMatrix();
}
void tableleg(double thick,double len)
{
glPushMatrix();
glTranslated(0,len/2,0);
glScaled(thick,len,thick);
glutSolidCube(1.0);
glPopMatrix();
}
void jackpart()
{
glPushMatrix();
glScaled(0.2,0.2,1.0);
glutSolidSphere(1,15,15);
glPopMatrix();
glPushMatrix();
glTranslated(0,0,1.2);
glutSolidSphere(0.2,15,15);
glTranslated(0,0,-2.4);
glutSolidSphere(0.2,15,15);
glPopMatrix();
}
void jack()
{
glPushMatrix();
jackpart();
glRotated(90.0,0,1,0);
jackpart();
glRotated(90.0,1,0,0);
jackpart();
glPopMatrix();
}
void table(double topwid,double
topthick,double legthick,double leglen)
{
glPushMatrix();

glTranslated(0,leglen,0);
glScaled(topwid,topthick,topwid);
glutSolidCube(1.0);
glPopMatrix();
double dist=0.95*topwid/2.0-legthick/2.0;
glPushMatrix();
glTranslated(dist,0,dist);
tableleg(legthick,leglen);
glTranslated(0,0,-2*dist);
tableleg(legthick,leglen);
glTranslated(-2*dist,0,2*dist);
tableleg(legthick,leglen);
glTranslated(0,0,-2*dist);
tableleg(legthick,leglen);
glPopMatrix();
}
void display(void)
{
GLfloat
mat_ambient[]={0.7f,0.7f,0.7f,1.0f};
GLfloat mat_diffuse[]={0.6f,0.6f,0.6f,1.0f};
GLfloat
mat_specular[]={1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[]={50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat
_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_
diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,ma
t_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,ma
t_shininess);
GLfloat
lightIntensity[]={0.7f,0.7f,0.7f,1.0f};
GLfloat
light_position[]={2.0f,6.0f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,light_p
osition);
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightInte
nsity);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double winHt=1.0;

engineerportal.blogspot.in

glOrtho(-winHt*64/48.0,winHt*64/48.0,winHt,winHt,0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2.3,1.3,2,0,0.25,0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(0.4,0.4,0.6);
glRotated(45,0,0,1);
glScaled(0.08,0.08,0.08);
jack();
glPopMatrix();
glPushMatrix();
glTranslated(0.6,0.38,0.5);
glRotated(30,0,1,0);
glutSolidTeapot(0.08);
glPopMatrix();
glPushMatrix();
glTranslated(0.25,0.42,0.35);
glutSolidSphere(0.1,15,15);
glPopMatrix();
glPushMatrix();
glTranslated(0.4,0,0.4);
table(0.6,0.02,0.02,0.3);
glPopMatrix();
wall(0.02);
glPushMatrix();
glRotated(90.0,0.0,0.0,1.0);
wall(0.02);
glPopMatrix();

computer graphics lab manual

glPushMatrix();
glRotated(-90.0,1.0,0.0,0.0);
wall(0.02);
glPopMatrix();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Windows
Application with GLUT");
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glClearColor(1.0,1.0,1.0,0.0);

glViewport(0,0,640,480);
glutMainLoop();
return 0;

engineerportal.blogspot.in

computer graphics lab manual

Colour models

#include <GL/glut.h>
void display(void)
{
float r,g,b,c,m,y;
glClear(GL_COLOR_BUFFER_BIT);
r=1;
g=0;
b=0;
c=1-r;
m=1-g;
y=1-b;
glColor3f(r,g,b);
int i;
{
for(i=0;i<20;i++)
glRecti(100+i,200+i,150+i,250
+i);
}
glColor3f(c,m,y);
for(i=0;i<20;i++)
{
glRecti(200+i,300+i,250+i,350
+i);
}
glFlush();

Output :

}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0,
440.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE
| GLUT_RGB);
glutInitWindowSize(640,440);
glutInitWindowPosition(100,100);
glutCreateWindow("A Simple
OpenGL Windows Application with
GLUT");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

engineerportal.blogspot.in

computer graphics lab manual

Window to viewport
#include <GL/glut.h>
void display(void)
{
GLint i,j;
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0,0,640,440);
glBegin(GL_TRIANGLES);
glVertex2i(300,300);
glVertex2i(250,350);
glVertex2i(350,350);
glEnd();
glColor3f(1.0,0.0,0.0);
for(i=0;i<3;i++)
for(j=0;j<5;j++)
{
glViewport(i*64,j*44,64,44);
glBegin(GL_TRIANGLES);
glVertex2i(300,300);
glVertex2i(250,350);
glVertex2i(350,350);
glEnd();
}
glFlush();
}

gluOrtho2D(0.0, 640.0, 0.0, 440.0);

}
int main(int argc, char** argv)
{
//Initialise GLUT with command-line
parameters.
glutInit(&argc, argv);
//Set Display Mode
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
//Set the window size
glutInitWindowSize(640,440);
//Set the window position
glutInitWindowPosition(100,100);
//Create the window
glutCreateWindow("A Simple OpenGL
Windows Application with GLUT");
//Call init (initialise GLUT
init();

void init()
{
//select clearing (background) color
glClearColor(0.0, 0.0, 0.0, 0.0);
//initialize viewing values
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

//Call "display" function


glutDisplayFunc(display);

//Enter the GLUT event loop


glutMainLoop();
//exit(0);
return 0;

engineerportal.blogspot.in

computer graphics lab manual

//JULIA sets
#include <GL/glut.h>
int dwell(double sx,double sy)
{
int iteration,itermax=100;
double dx,dy,tmp,fsq,cx=-0.5,cy=0.5;
dx = sx; dy = sy;
fsq=sx*sx+sy*sy;
for (iteration=1;iteration<=itermax &&
fsq<=4;iteration++)
{
tmp=dx;
dx = dx*dx-dy*dy+cx;
dy = 2.0*tmp*dy+cy;
fsq=dx*dx+dy*dy;
}
return iteration;
}
void color(int x,int y,int f)
void color(int x,int y,int f)
{
if(f==1)
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
else if(f==2)
{
glColor3f(0.0,1.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
glFlush();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);

Output

glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 440.0);

}
void display()
{
double sx,sy,tmp,zoom=1.5;
int iteration,hx,hy;
int itermax = 100;
int hxres = 200;
int hyres = 200;
for (hy=1;hy<=hyres;hy++)
{
sy=(hy-hyres/2)/(0.5*zoom*hyres);
for (hx=1;hx<=hxres;hx++)
{
sx=1.5*(hx-hxres/2)/(0.5*zoom*hxres);
iteration=dwell(sx,sy);
if (iteration<=itermax)
color(hx,hy,1);
else
color(hx,hy,2);
}
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
glutInitWindowSize(640,440);
glutInitWindowPosition(100,100);
glutCreateWindow("A Simple OpenGL
Windows Application with GLUT");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

engineerportal.blogspot.in
Mandelbort Set
#include <GL/glut.h>
int dwell(double cx,double cy)
{
int iteration,itermax=100;
double dx,dy,tmp,fsq;
dx = 0; dy = 0;
fsq=cx*cx+cy*cy;
for (iteration=1;iteration<=itermax &&
fsq<=4;iteration++)
{
tmp=dx;
dx = dx*dx-dy*dy+cx;
dy = 2.0*tmp*dy+cy;
fsq=dx*dx+dy*dy;
}
return iteration;
}
void color(int x,int y,int f) {
if(f==1)
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
else if(f==2)
{
glColor3f(0.0,1.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
glFlush();
}
void display()
{
double dx,tmp,dy,cx,cy;
int iteration,hx,hy;
int itermax = 100;

Output

computer graphics lab manual


double zoom=0.7;
int hxres = 200;
int hyres = 200;
for (hy=1;hy<=hyres;hy++)
{
cy = (((float)hy)/((float)hyres)0.5)/zoom*3.0;
for (hx=1;hx<=hxres;hx++)
{
cx =
(((float)hx)/((float)hxres)-0.5)/zoom*3.0-0.7;
iteration=dwell(cx,cy);

if (iteration<=itermax)
color(hx,hy,1);
else
color(hx,hy,2);

}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 440.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
glutInitWindowSize(640,440);
glutInitWindowPosition(100,100);
glutCreateWindow("A Simple OpenGL Windows
Application with GLUT");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Das könnte Ihnen auch gefallen