Sunteți pe pagina 1din 30

COMPUTER GRAPHICS LAB MANUAL 2015

Ex.no.1a DDA Line Drawing Algorithm


Aim:
To implement DDA Line Drawing Algorithm in C.
C Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,x2,y1,y2,k,dx,dy,s,xi,yi;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi:");
printf("enter first point");
scanf("%d%d",&x1,&y1);
printf("enter second point");
scanf("%d%d",&x2,&y2);
x=x1;
y=y1;
putpixel(x,y,7);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
s=abs(dx);
else
s=abs(dy);
xi=dx/s;
yi=dy/s;
x=x1;
y=y1;
putpixel(x,y,7);
for(k=0;k<s;k++)
{
x=x+xi;
y=y+yi;
putpixel(x,y,7);
}
getch();
closegraph();
}
Result:
Thus the program was executed and result was achieved.
Ex.no.1b Bresenham Line Drawing Algorithm
Aim:
To implement Bresenham Line Drawing Algorithm in C.
C Code:
# 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,"e:\\tc\\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();
}
Result:
Thus the program was executed and result was achieved.
Ex.no.1c Generalised Bresenham Line Drawing Algorithm
Aim:
To implement Generalised Bresenham Line Drawing Algorithm in C.
C Code:

#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)
{
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();
}
Result:
Thus the program was executed and result was achieved.
Ex.no.1d Midpoint Circle Drawing Algorithm
Aim:
To implement Midpoint Circle Drawing Algorithm in C.
C Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gdriver=DETECT,gmode;
int r,x,y,p,xc=320,yc=240;
initgraph(&gdriver,&gmode,"C://TC//BGI");
printf(" enter the radius,xc,xy : ");
scanf("%d%d%d",&r,&xc,&yc);
x=0;y=r;
putpixel(xc+x,yc-y,1);
p=1-r;
for(x=0;x<=y;x++)
{
if(p<0)
{
y=y;
p=p+2*x+1;
}
else
{
y=y-1;
p=p+2*(x-y)+1;
}
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7) ;
putpixel(xc-y,yc+x,8);
}
getch();
closegraph();

} Result:
Thus the program was executed and result was achieved.
Ex.no.2a 2D Basic transformations
Aim:
To implement 2D Basic transformations in C.
C Code:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"c:\tc\bg:");
printf("\t Program for basic transactions");
printf("\n\t Enter the points of triangle");
setcolor(1);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Enter the translation factor");
scanf("%d%d",&xt,&yt);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 2:
printf("\n Enter the angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 3:
printf("\n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 4:
break;
default:
printf("Enter the correct choice");
}
closegraph();
}
Result:
Thus the program was executed and result was achieved.
Ex.no.2b 2D Reflection
Aim:
To implement 2D Reflection of a triangle in C.
C Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void main()
{
clrscr();
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"...\\bgi");
int x,y,x1,a[3][3];
double b[3][3],c[3][3];
cout<<"\n Enter Ist coordinates of triangle:";
cin>>a[0][0]>>a[1][0];
cout<<"\n Enter 2nd coordinates of triangle:";
cin>>a[0][1]>>a[1][1];
cout<<"\n Enter 3rd coordinates of triangle:";
cin>>a[0][2]>>a[1][2];
cout<<"\n Enter 1. for reflection in x-axis:\n";
cout<<"\n Enter 2. for reflection in y-axis:\n";
cout<<"\n Enter 3. for reflection in both the axis:\n";
cin>>x;
cleardevice();
line(320,0,320,479);
line(0,240,639,240);
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
switch(x)
{
case 1:b[0][0]=640-a[0][0];
b[0][1]=640-a[0][1];
b[0][2]=640-a[0][2];
b[1][0]=a[1][0];
b[1][1]=a[1][1];
b[1][2]=a[1][2];
line(320,0,320,479);
line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch();
break;
case 2:b[1][0]=480-a[1][0];
b[1][1]=480-a[1][1];
b[1][2]=480-a[1][2];
b[0][0]=a[0][0];
b[0][1]=a[0][1];
b[0][2]=a[0][2];
line(320,0,320,479);
line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch();
break;
case 3: b[0][0]=640-a[0][0];
b[0][1]=640-a[0][1];
b[0][2]=640-a[0][2];
b[1][0]=a[1][0];
b[1][1]=a[1][1];
b[1][2]=a[1][2];
line(320,0,320,479);
line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
b[1][0]=480-a[1][0];
b[1][1]=480-a[1][1];
b[1][2]=480-a[1][2];
b[0][0]=a[0][0];
b[0][1]=a[0][1];
b[0][2]=a[0][2];
line(320,0,320,479);
line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch();
break;
}
getch();
closegraph();
}
Result:
Thus the program was executed and result was achieved.
Ex.no.2c 2D Shear
Aim:
To implement 2D shear of a rectangle in C.

C Code:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
int x1,y1,x2,y2,x,y;
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
clrscr();
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
cout<<"enter the top left coordinates:";
cin>>x1>>y1;
cout<<"enter the bottom right coordinates:";
cin>>x2>>y2;
cout<<"enter the values of shearing coordinate for x_shear:\n";
cin>>x;
cout<<"enter the values of shearing coordinate for y_shear:\n";
cin>>y;
cleardevice();
rectangle(x1,y1,x2,y2);
cout<<"now hit a key to see shear in x_axis:";
getch();
rectangle(x1,y1,x2*x,y2);
cout<<"\nnow hit a key to see shear in y_axis:";
getch();
rectangle(x1,y1,x2,y2*y);
getch();
closegraph();
}
Result:
Thus the program was executed and result was achieved.
Ex.no.2d Windows viewport conversion
Aim:
To implement Windows viewport conversion in C.
C Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
float xwmin,xwmax,ywmax,ywmin;
float xvmin,xvmax,yvmax,yvmin;
float x[10],y[10],yv,xv,sx,sy;
int gd=DETECT,gm,i;
clrscr();
printf("\n enter window port coordinates:\n(xwmin,ywmin,xwmax,ywmax): ");
scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax);
printf("\n enter view port coordinates:\n(xvmin,yvmin,xvmax,yvmax): ");
scanf("%f%f%f%f",&xvmin,&yvmin,&xvmax,&yvmax);
printf("\n enter vertices for triangle: ");
for(i=0;i < 3;i++)
{
printf("\n enter(x%d,y%d):",i,i);
scanf("%f%f",&x[i],&y[i]);
}
sx=((xvmax-xvmin)/(xwmax-xwmin));
sy=((yvmax-yvmin)/(ywmax-xwmin));

initgraph(&gd,&gm," ");
outtextxy(80,30,"window port");
rectangle(xwmin,ywmin,xwmax,ywmax);
for(i=0;i <2;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[2],y[2],x[0],y[0]);
getch();
cleardevice();
for(i=0;i <3;i++)
{
x[i]=xvmin+((x[i]-xwmin)*sx);
y[i]=yvmin+((y[i]-ywmin)*sy);
}
outtextxy(150,10,"view port");
rectangle(xvmin,yvmin,xvmax,yvmax);
for(i=0;i <2;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[2],y[2],x[0],y[0]);
getch();
}
Result:
Thus the program was executed and result was successfully achieved.
Ex.no.3 Composite 2-D transformations
Aim:
To implement Composite 2-D transformations in C.
C code:
#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>
int i,pt[8]={50,50,150,50,150,100,50,50};

void trans();

void scale(int *);

void invtrans();

void main()

int gd=DETECT,gm;

int refpt[2]={100,100};

initgraph(&gd,&gm,” c:\\tc\\bgi “);

setbkcolor(WHITE);

setcolor(BLUE);

for(i=0;i<7;i++)

drawpoly(4,pt);

trans();

closegraph();

void trans()

int p[8];

int i,tx=0,ty=150;

printf(“Translation”);

for(i=0;i<8;i=i+2)

{
p[i]=pt[i]+tx;

p[i+1]=pt[i+1]+ty;

drawpoly(4,pt);

drawpoly(4,p);

getch();

scale(refpt);

void scale(int ref[])

int p[8],i;

float sx=2.0,sy=2.0;

printf(“\t Scaling “);

for(i=0;i<8;i=i+2)

p[i]=ref[0]+(pt[i]-ref[0])*sx;

p[i+1]=ref[1]+(pt[i+1]-ref[1])*sy;

drawpoly(4,p);

getch();

invtrans();

cleardevice();

void invtrans()

{
int p[8];

int i,tx=0,ty=150;

printf(“Inverse Translation”);

for(i=0;i<8;i=i+2)

p[i]=pt[i]-tx;

p[i+1]=pt[i+1]-ty;

drawpoly(4,p);

getch();

}
Result:
Thus the program was executed and result was successfully achieved.
Ex.no.4 Line clipping
Aim:
To implement Cohen Sutherland line clipping algorithm in C.
C code:
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define MAX 20

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

enum{ FALSE, TRUE };


typedefunsignedintoutcode;

outcodecompute_outcode(int x,int y,
intxmin,intymin,intxmax,intymax)
{
outcodeoc=0;

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

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

returnoc;
}

void cohen_sutherland(double x1,double y1,double x2,double y2,


doublexmin,doubleymin,doublexmax,doubleymax)
{
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;
intoutcode_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;
intln[MAX][4];
int clip[4];
intgd= 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();
}
Result:
Thus the program was executed and result was successfully achieved.
Ex.no.5 3-D Transformation
Aim:
To implement 3-D Transformation in C.
C code:
#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,ang,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:/TC/BGI");
setfillstyle(3,25);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
outtextxy(100,100,"ORIGINAL OBJECT");
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
axis();
outtextxy(100,20,"TRANSLATION");
printf("\n\n Enter the Translation vector: ");
scanf("%d%d%d",&x,&y,&z);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+(x+100),midy-(y+20),midx+(x+60),midy-(y+90),20,5);
axis();
outtextxy(100,20,"SCALING");
printf("\n Enter the Scaling Factor: ");
scanf("%d%d%d",&x,&y,&z);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+(x*100),midy-(y*20),midx+(x*60),midy-(y*90),20*z,5);
axis();
outtextxy(100,20,"ROTATION");
printf("\n Enter the Rotation angle: ");
scanf("%f",&ang);
x1=100*cos(ang*3.14/180)-20*sin(ang*3.14/180);
y1=100*sin(ang*3.14/180)+20*sin(ang*3.14/180);
x2=60*cos(ang*3.14/180)-90*sin(ang*3.14/180);
y2=60*sin(ang*3.14/180)+90*sin(ang*3.14/180);
axis();
printf("\n After rotating about z-axis\n");
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
setfillstyle(2,5);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,20,5);
axis();
printf("\n After rotating about x-axis\n");
setfillstyle(3,25);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
setfillstyle(2,5);
bar3d(midx+100,midy-x1,midx+60,midy-x2,20,5);
axis();
printf("\n After rotating about y-axis\n");
setfillstyle(3,25);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
setfillstyle(2,5);
bar3d(midx+x1,midy-20,midx+x2,midy-90,20,5);
axis();
closegraph();
}
Result:
Thus the program was executed and result was successfully achieved.
Ex.no.6a 3-D Parellel projection
AIM:
To implement 3-D Parellel Projection in C.
C Code:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void input(int[],int[],int[],int);
void printfFigure(int[],int[],int);
void printfFigure_ortho(int[],int[],int);
void getOblique(int [] , int [],int [] , int , float , int [],int [],int);
int main()
{
int gd=DETECT,gm;
int x[10],y[10],z[10],no,xp[10],yp[10],choice;
float angle1;
printf("Enter Your choice : \n");
printf("1:Point\n");
printf("2 : Line\n");
printf("3 : Triangle\n");
printf("4 : Square\n");
printf("Or Enter no of vertices for any Polygon\n");
scanf("%d",&no);
printf("Enter angle Phi\t");
scanf("%f",&angle1);
input(x,y,z,no);
initgraph(&gd,&gm,NULL);
printf("Orthographic Projenction(WHITE)\n");
printfFigure_ortho(x,y,no);
printf("Oblique Projenction(YELLOW)\n");
printf("Enter 1. Cavalier Projection\n 2. Cabinet Projection\n");
scanf("%d",&choice);
getOblique(x,y,z,no,angle1,xp,yp,choice);
setcolor(YELLOW);
printfFigure(xp,yp,no);
return 0;
}
void input(int x[],int y[],int z[],int no)
{
int i;
printf("Enter co-ordinate\n");
for (i = 0; i < no; i++)
{
printf("P[%d]\t",i+1);
scanf("%d",&x[i]);
scanf("%d",&y[i]);
scanf("%d",&z[i]);
}

}
void getOblique(int x[],int y[],int z[],int no,float angle1,int xp[],int yp[],int choice)
{
int i;

for (i = 0; i < no; ++i)

{
if(choice==1)
{
xp[i]=x[i]+(z[i]/tan(45))*cos(angle1);
yp[i]=y[i]+(z[i]/tan(45))*sin(angle1);
}
if(choice==2)
{
xp[i]=x[i]+(z[i]/tan(63.43))*cos(angle1);
yp[i]=y[i]+(z[i]/tan(63.43))*sin(angle1);
}
}
}

void printfFigure(int xp[],int yp[],int no)


{
int i;

if(no==1)
{

putpixel(xp[0],yp[0],YELLOW);
}
else
{
for (i = 0; i < no-1; ++i)
{
line(xp[i],yp[i],xp[i+1],yp[i+1]);
}
}
if(no>2)
{
line(xp[0],yp[0],xp[no-1],yp[no-1]);
}
}

void printfFigure_ortho(int x[],int y[],int no)


{
int i;

if(no==1)
{
putpixel(x[0],y[0],YELLOW);
}
else
{
for (i = 0; i < no-1; ++i)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
}
if(no>2)
{
line(x[0],y[0],x[no-1],y[no-1]);
}
}

Result:
Thus the program was executed and result was successfully achieved.
Ex.no.6b 3-D Perspective projection
AIM:
To implement 3-D Perspective Projection in C.
C Code:
#include<stdio.h>
#include<math.h>
#include<graphics.h>

main()
{

int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
int xp, yp, zp, x, y, z;

a[0][0] = 100; a[1][0] = 100; a[2][0] = -100;


a[0][1] = 200; a[1][1] = 100; a[2][1] = -100;

a[0][2] = 200; a[1][2] = 200; a[2][2] = -100;


a[0][3] = 100; a[1][3] = 200; a[2][3] = -100;

a[0][4] = 100; a[1][4] = 100; a[2][4] = -200;


a[0][5] = 200; a[1][5] = 100; a[2][5] = -200;
a[0][6] = 200; a[1][6] = 200; a[2][6] = -200;
a[0][7] = 100; a[1][7] = 200; a[2][7] = -200;

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

ymax = getmaxy();
xp = 300; yp = 320; zp = 100;

for(j=0; j<8; j++)


{
x = a[0][j]; y = a[1][j]; z = a[2][j];

b[0][j] = xp - ( (float)( x - xp )/(z - zp)) * (zp);


b[1][j] = yp - ( (float)( y - yp )/(z - zp)) * (zp);
}

/*- front plane display -*/

for(j=0;j<3;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);

}
x1=(int) b[0][3]; y1=(int) b[1][3];
x2=(int) b[0][0]; y2=(int) b[1][0];
line( x1, ymax-y1, x2, ymax-y2);

/*- back plane display -*/


setcolor(11);
for(j=4;j<7;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1, ymax-y1, x2, ymax-y2);

}
x1=(int) b[0][7]; y1=(int) b[1][7];
x2=(int) b[0][4]; y2=(int) b[1][4];
line( x1, ymax-y1, x2, ymax-y2);

setcolor(7);
for(i=0;i<4;i++)
{
x1=(int) b[0][i]; y1=(int) b[1][i];
x2=(int) b[0][4+i]; y2=(int) b[1][4+i];
line( x1, ymax-y1, x2, ymax-y2);
}

getch();

}
Result:
Thus the program was executed and result was successfully achieved.

Ex.no.7 CUBE DRAWING USING OPENGL AIM:


To implement Cube Drawing using OpenGL
C Code:
#include <GL/glut.h>

float ver[8][3] =
{
{-1.0,-1.0,1.0},
{-1.0,1.0,1.0},
{1.0,1.0,1.0},
{1.0,-1.0,1.0},
{-1.0,-1.0,-1.0},
{-1.0,1.0,-1.0},
{1.0,1.0,-1.0},
{1.0,-1.0,-1.0},
};

GLfloat color[8][3] =
{
{0.0,0.0,0.0},
{1.0,0.0,0.0},
{1.0,1.0,0.0},
{0.0,1.0,0.0},
{0.0,0.0,1.0},
{1.0,0.0,1.0},
{1.0,1.0,1.0},
{0.0,1.0,1.0},
};

void quad(int a,int b,int c,int d)


{
glBegin(GL_QUADS);
glColor3fv(color[a]);
glVertex3fv(ver[a]);

glColor3fv(color[b]);
glVertex3fv(ver[b]);

glColor3fv(color[c]);
glVertex3fv(ver[c]);

glColor3fv(color[d]);
glVertex3fv(ver[d]);
glEnd();
}

void colorcube()
{
quad(0,3,2,1);
quad(2,3,7,6);
quad(0,4,7,3);
quad(1,2,6,5);
quad(4,5,6,7);
quad(0,1,5,4);
}

double rotate_y = 0;
double rotate_x = 0;
void specialKeys( int key, int x, int y )
{
if (key == GLUT_KEY_RIGHT)
rotate_y += 5;
else if (key == GLUT_KEY_LEFT)
rotate_y -= 5;
else if (key == GLUT_KEY_UP)
rotate_x += 5;
else if (key == GLUT_KEY_DOWN)
rotate_x -= 5;
glutPostRedisplay();
}

void display()
{
glClearColor( 0, 0, 0, 1 );
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
int w = glutGet( GLUT_WINDOW_WIDTH );
int h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective( 60, w / h, 0.1, 100 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt
(
3, 3, 3,
0, 0, 0,
0, 0, 1
);

glRotatef( rotate_x, 1.0, 0.0, 0.0 );


glRotatef( rotate_y, 0.0, 1.0, 0.0 );
colorcube();

glutSwapBuffers();
}

int main( int argc, char **argv )


{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutSpecialFunc( specialKeys );
glEnable( GL_DEPTH_TEST );
glutMainLoop();
return 0;
}

Result:
Thus the program was executed and result was successfully achieved.
__________________________________________________

S-ar putea să vă placă și