0% found this document useful (0 votes)
225 views

Computer Graphics Lab

The document contains the index and programs for a computer graphics lab. The programs include drawing shapes using mid-point algorithms, constructing the Indian flag, moving a circle on an ellipse and vice versa, oscillating a circle inside a parabola, demonstrating clipping and transformations, and constructing a name using splines.

Uploaded by

Garvit Pareek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views

Computer Graphics Lab

The document contains the index and programs for a computer graphics lab. The programs include drawing shapes using mid-point algorithms, constructing the Indian flag, moving a circle on an ellipse and vice versa, oscillating a circle inside a parabola, demonstrating clipping and transformations, and constructing a name using splines.

Uploaded by

Garvit Pareek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

COMPUTER GRAPHICS LAB

PROGRAMS FILE

Garvit Pareek
267/CO/12
Vth Sem(3rd year)

INDEX
1.Program to build a header file(.H) containing
methods of drawing
line, circle, ellipse and
parabola using mid-point approach.
2. Program to construct waving flag of India.
3. Program to move a circle on an ellipse.
4. Program to move an ellipse on a circle.
5. Program to oscillate a circle inside a parabola.
6. Program to demonstrate cyrus beck clipping
algorithm using given clipping polygon and line.
7. Program to demonstrate 2D transformation.
8. & 9. Program to demonstrate 3D transformation
with/without hidden planes.
10. Program to construct your name using Hermite
Spline.

1. Program to build a header file(.H) containing methods of drawing line, circle,


ellipse and parabola using mid-point approach.
#include<graphics.h>
/* Line mid point */
void putline(int x, int y, int col)
{

putpixel(x,y, col); }

void midline(int x1, int y1, int x2, int y2, int col)
{ int d;
float m, x, y;
int xa,ya,xb,yb, dx, dy;
if(x2!=x1)
m=1.0*(y2-y1)/(x2-x1);
if(x1<x2)
{

xa=x1;ya=y1;
xb=x2;yb=y2;

}
else
{

xa=x2;ya=y2;
xb=x1;yb=y1;

}
dx = xb-xa;
dy = yb-ya;
x = xa; y = ya;
if( m>=0 && m<=1 )
{

putline(xa,ya,col);
d = 2.0*dy-dx;
while(x<xb)
{

if(d>0)
{ d+=dy-dx;
x++;
y++;
}
else
{ d+= dy;
x++;
}
putline(x,y,col);

}
}

else if(m>-1 && m<0)


{
putline(xa,ya,col);
d = 2.0*dy+dx;
while(x<xb)
{
if(d>0)
{
d+=dy;
x++;

else
{

d+= dy+dx;
x++;

y--;

}
putline(x,y,col);
}
}
else if(m>1)
{ putline(xa,ya,col);
d = dy - 2.0*dx;
while(x<xb)
{
if(d>0)
{

d-= dx;

y++;
}
else
{

d+= dy-dx;
x++;

y++;

}
putline(x,y,col);
}
}
else if(m<-1)
{
putline(xa,ya,col);
d = dy + 2.0*dx;
while(x<xb)
{

if(d>0)
{

d+=dy+dx;
x++;y--;

}
else
{

d+= dx;
y--;

}
putline(x,y,col);
}
}
}
///////////////////////////////////////////////////////////////
/* Circle mid point */
void plotcircle(int x, int y,int xc, int yc, int col)
{
putpixel((x+xc),(y+yc),col);
putpixel((-x+xc),(y+yc),col);
putpixel((x+xc),(-y+yc),col);
putpixel((-x+xc),(-y+yc),col);
putpixel((y+xc),(yc+x),col);
putpixel((-y+xc),(yc+x),col);
putpixel((y+xc),(yc-x),col);
putpixel((-y+xc),(yc-x),col);
}
void midcircle(int xc, int yc,int r, int col)
{

float x=0,y=r;
float d = 1-r;
float de = 3,dse = 5-2*r;
plotcircle(x,y,xc,yc, col);
while(x<=y)
{
if(d<0)
{
d+=de;
de+=2;
dse+=2;
x++;
}

else
{
d+=dse;
de+=2;
dse+=4;
x++;
y--;
}
plotcircle(x,y,xc,yc, col);
}
}
///////////////////////////////////////////////////////////////
/* Ellipse mid point */
void plot_elli(int xc, int yc, int x, int y, int col)
{
putpixel((x+xc),(yc-y),col);
putpixel((-x+xc),(yc-y),col);
putpixel((x+xc),(yc+y),col);
putpixel((-x+xc),(yc+y),col);
}
void midellipse(int xc, int yc,float a, float b, int col)
{
float x=0,y=b;
float d = a*a*(0.25-b) + b*b;
plot_elli(xc,yc,x,y, col);
while((b*b*(x+1))<=(a*a*(y-0.5)))
{
if(d<0)
{
d+= 1.0*b*b*(3+2*x);
x++;
}
else
{
d+= b*b*(3+2*x)*1.0+2*1.0*a*a*(1-y);
x++;
y--;
}

plot_elli(xc,yc,x,y, col);
}
d = b*b*(1.0*x+0.5)*(1.0*x+0.5)+1.0*a*a*(y-1)*(y-1)-1.0*a*a*b*b;
while(y>=0)
{
if(d<0)
{
d+= 2.0*b*b*(1+x)+1.0*a*a*(3-2*y);
x++;
y--;
}
else
{
d+= a*a*(3-2*y)*1.0;
y--;
}
plot_elli(xc,yc,x,y, col);
}
}
//////////////////////////////////////////////////////////
/* Parabola mid point */
void putpara(int cx, int cy,int x, int y, int col)
{
putpixel(cx+x,cy-y,col);
putpixel(cx+x,cy+y,col);
}
void midpara(int cx, int cy, double a, int col)
{
double x=0,y=0;

/* initial coorodinates */

double d1, de;


d1 = (2*a) - 1;
double dne = 4*a-3;
double dn = -3;
putpara(cx,cy,x,y,col);
while(y<= (2*a*1.0))
{
if(d1<0)
{

d1+= dne;
dne-=2;
dn-=2;
x++;
y++;
}
else
{
d1+=dn;
dne-=2;
dn-=2;
y++;
}
putpara(cx,cy,x,y,col);
}
d1 = (4.0*a*(x+1) - (y+0.5)*(y+0.5) );
de = 4*a;
dne = 4*a - 2*(1+y);
while( y < 220 )
{
if(d1<0)
{
d1+= de;
x++;
}
else
{
d1+= dne;
dne+=-2;
x++;
y++;
}
putpara(cx,cy,x,y,col);
}
}

2. Program to draw the Indian flag.


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
const float PI=3.14154;
void main()
{
int gdriver=DETECT,gmode=0;
int I;
int x,y;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
cleardevice();
rectangle(0,0,639,479);
outtextxy(280,20,"INDIAN FLAG");
rectangle(80,50,560,380);
line(80,160,560,160);
line(80,270,560,270);
setfillstyle(SOLID_FILL,LIGHTRED);
floodfill(85,60,getmaxcolor());
setfillstyle(SOLID_FILL,WHITE);
floodfill(85,170,getmaxcolor());
setfillstyle(SOLID_FILL,GREEN);
floodfill(85,280,getmaxcolor());
setcolor(BLUE);
circle(320,215,50);
for(I=0;I<=360;I+=15)
{
x=50*cos(I*PI/180);
y=50*sin(I*PI/180);
line(320,215,320+x,215-y);
}
setcolor(CYAN);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,6);
outtextxy(100,400,"
JAI HIND!!!" ) ;
getch();
}

3. Program to move a circle on an ellipse.


#include<myheader.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#include<process.h>
void motion(int xrad, int yrad, int midx, int midy, int x[60], int y[60]) {

int i, j = 0;
xrad+=30;
yrad+=30;
/* positions of rads 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()
{
int gd=DETECT, gm;
int midx, midy, rad,pos,xrad,yrad,x[60],y[60], i;
initgraph(&gd, &gm, "\\turboc3\\bgi");
midx=getmaxx()/2;
midy=getmaxy()/2;
rad=30;
pos=1;
xrad=180;
yrad=100;
motion(xrad, yrad, midx, midy, x, y);
while(!kbhit())
{
setcolor(WHITE);
midellipse(midx,midy, xrad,yrad);
setcolor(BLUE);

//setfillstyle(SOLID_FILL,BLUE);
midcircle(x[pos], y[pos], rad);
//floodfill(x[pos], y[pos], BLUE);
/* checking for one complete rotation */
if (pos <= 0) {
pos = 59;
}
else {
pos = pos - 1;
}
/* sleep for 100 milliseconds */
delay(100);
cleardevice();

}
closegraph();
return 0;
}

4. Program to move an ellipse on a circle.


#include <dos.h>
#include <gc.h>
#define PI 3.1416
#include <math.h>
void main()
{ float xrad = 50, yrad = 25;
int gd = DETECT, gm;
initgraph(&gd,&gm," ");
midcircle(300,250,100,12);
while(!kbhit()) {
for(int deg = 0;deg < 360; ++deg)
{

int x = 300 + (100+xrad) * cos(PI/180*deg);


int y = 250 - (100+yrad) * sin(PI/180*deg);
midellipse(x,y,xrad,yrad,14);
delay(50);
setcolor(BLACK);
midellipse(x,y,xrad,yrad,0);
setcolor(WHITE);
midcircle(300,250,100,12);

closegraph(); }

3.Program to move a ball inside a parabola.


#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include "paramove.h"
#include "midcirc.h"
#include<dos.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm=DETECT;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
cleardevice();
int theta;
int x,y,x1,y1,p,x2,y2,r;
p=100,y=0;
while(!kbhit())
{
for(theta=180;theta>=150;theta--)
{
drawparabola(0,0,p,BLUE,theta);
x=-sqrt(2*p*y);
x1=((x)*cos(theta*3.14/180)+(y)*sin(theta*3.14/180));
y1=((x)*sin(theta*3.14/180)-(y)*cos(theta*3.14/180));
plotcircle(x1,y1+20,20,BLUE);
delay(100);
plotcircle(x1,y1+20,20,BLACK);
drawparabola(0,0,p,BLACK,theta);
y++;
}
for(theta=150;theta<=180;theta++)
{
drawparabola(0,0,p,BLUE,theta);
x=-sqrt(2*p*y);
x1=((x)*cos(theta*3.14/180)+(y)*sin(theta*3.14/180));
y1=((x)*sin(theta*3.14/180)-(y)*cos(theta*3.14/180));
plotcircle(x1,y1+20,20,BLUE);

delay(100);
plotcircle(x1,y1+20,20,BLACK);
drawparabola(0,0,p,BLACK,theta);
y--;

}
for(theta=180;theta<=210;theta++)
{
drawparabola(0,0,p,BLUE,theta);
x=-sqrt(2*p*y);
x1=((x)*cos(theta*3.14/180)+(y)*sin(theta*3.14/180));
y1=((x)*sin(theta*3.14/180)-(y)*cos(theta*3.14/180));
r=2*((x1*(tan(theta*3.14/180)))-y1)*pow(cos(theta*(3.14/180)),2);
x2=(r)*(tan(theta*3.14/180))-x1;
y2=-r-y1;
plotcircle(x2,y2+20,20,BLUE);
delay(100);
plotcircle(x2,y2+20,20,BLACK);
drawparabola(0,0,p,BLACK,theta);
y++;
}
for(theta=210;theta>=180;theta--)
{
drawparabola(0,0,p,BLUE,theta);
x=-sqrt(2*p*y);
x1=((x)*cos(theta*3.14/180)+(y)*sin(theta*3.14/180));
y1=((x)*sin(theta*3.14/180)-(y)*cos(theta*3.14/180));
r=2*((x1*(tan(theta*3.14/180)))-y1)*pow(cos(theta*(3.14/180)),2);
x2=(r)*(tan(theta*3.14/180))-x1;
y2=-r-y1;
plotcircle(x2,y2+20,20,BLUE);
delay(100);
plotcircle(x2,y2+20,20,BLACK);
drawparabola(0,0,p,BLACK,theta);
y--;
}
}

closegraph();
return 1;
}

6. Program to demonstrate cyrus beck clipping algorithm using given clipping polygon
and line.
#include<graphics.h>
#include<stdio.h>
#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;
struct point
{
float x;
float y;
};
point getnorm(point a,point b)
{
point j;
j.x=b.y-a.y;
j.y=-(b.x-a.x);
return(j);
}
int main()
{
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

point a[5];
point s,e;
s.x=200+0;
s.y=200+200;
e.x=200+80;
e.y=200-40;
point norm[4];
a[0]={200+160,200+140};
a[1]={200+40.0,200+0.0};
a[2]={200+70.0,200+80.0};
a[3]={200+0.0,200+80.0};
a[4]={200+160.0,200+140.0};
setlinestyle(0,1,3);
setcolor(3);
for(int i=0; i<4; i++)
{
line(a[i].x,a[i].y,a[i+1].x,a[i+1].y);
}
line(a[4].x,a[4].y,a[0].x,a[0].y);
for(int i=0; i<4; i++)
{
norm[i]=getnorm(a[i+1],a[i]);
}
float t[4];
for(int i=0; i<4; i++)
{
float f=(s.x-a[i+1].x)*norm[i].x+(s.y-a[i+1].y)*norm[i].y;
float d=(s.y-e.y)*norm[i].y+(s.x-e.x)*norm[i].x;
t[i]=(f/d);
}
point t1,t2,t3,t4;
t1={s.x+(e.x-s.x)*t[0],s.y+(e.y-s.y)*t[0]};
t2={s.x+(e.x-s.x)*t[1],s.y+(e.y-s.y)*t[1]};
t3={s.x+(e.x-s.x)*t[2],s.y+(e.y-s.y)*t[2]};
t4={s.x+(e.x-s.x)*t[3],s.y+(e.y-s.y)*t[3]};
setlinestyle(0,1,2);
setcolor(10);
line(t1.x,t1.y,t2.x,t2.y);
line(t3.x,t3.y,t4.x,t4.y);

setlinestyle(1,1,1);
setcolor(WHITE);
line(s.x,s.y,t4.x,t4.y);
line(t2.x,t2.y,t3.x,t3.y);
line(t1.x,t1.y,e.x,e.y);
getch();
return(0);
}

7. Program to demonstrate 2D transformation


#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
struct point
{
float x;
float y;
float w;
};
struct polygon
{
int vertex_num;
point vertices[20];
};
void showpoly(polygon fig){
for(int i=0;i<fig.vertex_num;i++){
cout<<fig.vertices[i].x<<"

"<<fig.vertices[i].y<<"\n";

}
}
void draw(polygon fig,polygon figtrans,int transet){
showpoly(fig);
showpoly(figtrans);
int x=getmaxx()/2;
int y=getmaxy()/2;
getch();
setcolor(BLACK);
for(int i=0;i<fig.vertex_num;i++){
line(x+fig.vertices[i%fig.vertex_num].x,y+fig.vertices[i%fig.vertex_num].y,x+
fig.vertices[(i+1)%fig.vertex_num].x,y+fig.vertices[(i+1)%fig.vertex_num].y);
}
getch();
if(transet!=0){
setcolor(RED);
for(i=0;i<figtrans.vertex_num;i++){
line(x+figtrans.vertices[i%figtrans.vertex_num].x,y+figtrans.vertices[i%figtr

ans.vertex_num].y,x+figtrans.vertices[(i+1)%figtrans.vertex_num].x,y+figtrans
.vertices[(i+1)%figtrans.vertex_num].y);
}
getch();
}
}
void multiply(float a[][3],float b[][3])
{
float c[3][3]={{0,0}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int k=0;k<3;k++){
for(int j=0;j<3;j++){
a[k][j]=c[k][j];
}
}
}
struct transm{
float matrix[3][3];
int seq;
};
void showmatrix(transm matrix){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<matrix.matrix[i][j]<<"
}
cout<<"\n";
}
}
void setzeros(transm &matrix){
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
if(i==j)

";

matrix.matrix[i][j]=1;
else
matrix.matrix[i][j]=0;
}
}
void getfinaltrans(transm &transmatrix,transm rotation,transm
translation,transm scaling){
setzeros(transmatrix);
cout<<rotation.seq<<"

"<<translation.seq<<"

"<<scaling.seq<<"\n";

for(int counter=3;counter>0;counter--){
if(rotation.seq==counter){
showmatrix(transmatrix);
multiply(transmatrix.matrix,rotation.matrix);
showmatrix(transmatrix);
getch();
}
else if(translation.seq==counter){
multiply(transmatrix.matrix,translation.matrix);
showmatrix(transmatrix);
getch();
}
else if(scaling.seq==counter){
multiply(transmatrix.matrix,scaling.matrix);
showmatrix(transmatrix);
getch();
}
}
}
void settrans(transm &rotation,transm &translation,transm &scaling){
int choice=0;
int counter =0;
rotation.seq=0;
translation.seq=0;
scaling.seq=0;
while(choice <4){
counter++;
cout<<"choose transformations to setup in their respective order \n
1.scaling \n 2. rotation \n 3. translation \n 4.exit";
cin>>choice;

switch(choice){
case 1:float scalex;float scaley;
cout<<"enter scaling along x";
cin>>scalex;
cout<<"enter scaling along y";
cin>>scaley;
scaling.matrix[0][0]=scalex;
scaling.matrix[1][1]=scaley;
scaling.matrix[2][2]=1;
scaling.seq=counter;
break;
case 2:
float angle;
cout<<"enter degrees you want to rotate it in, anticlockwise";
cin>>angle;
angle=(3.14*angle)/180;
rotation.matrix[0][0]=cos(angle);
rotation.matrix[1][1]=cos(angle);
rotation.matrix[0][1]=-sin(angle);
rotation.matrix[1][0]=sin(angle);
rotation.matrix[2][2]=1;
rotation.seq=counter;
getch();
break;
case 3:
float transx,transy;
cout<<"enter translation along x";
cin>>transx;
cout<<"enter translation along y";
cin>>transy;
translation.matrix[0][0]=1;
translation.matrix[0][2]=transx;
translation.matrix[1][1]=1;
translation.matrix[1][2]=transy;
translation.matrix[2][2]=1;
translation.seq=counter;
break;
case4:
break;

}
}
}
void get_transformed_vertices(polygon &figtrans,polygon trans,transm
transmatrix){
showmatrix(transmatrix);
for(int i=0;i<trans.vertex_num;i++){
int a[3]={0,0,0};
int b[3];
b[0]=trans.vertices[i].x;
b[1]=trans.vertices[i].y;
b[2]=trans.vertices[i].w;
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
a[j]+=transmatrix.matrix[j][k]*b[k];
}
}
figtrans.vertices[i].x=a[0];
figtrans.vertices[i].y=a[1];
figtrans.vertices[i].w=a[2];
}
figtrans.vertex_num=trans.vertex_num;
showpoly(trans);
getch();
cout<<"\n";
showpoly(figtrans);
getch();
}
int main(){
int gdriver = EGA, gmode = EGAHI, errorcode;
int color, maxcolor, x, y;
char msg[80];
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();

}
int choice1=0;
int vertices;
polygon fig;
polygon figtrans;
int transset=0;
transm rotation,translation,scaling,transmatrix;
setzeros(rotation);
setzeros(translation);
setzeros(scaling);
while(choice1<4){
cout<<"CHOOSE \n 1. Enter Polygon \n 2. Draw Polygon \n 3. Specify
different Transformations \n 4. exit";
cin>>choice1;
switch(choice1){
case 1:cout<<"enter number of vertices of figure";
cin>>fig.vertex_num;
for(int i=0;i<fig.vertex_num;i++){
cout<<"enter x-cordinate of vertex "<<i+1;
cin>>fig.vertices[i].x;
cout<<"enter y-cordinate of vertex "<<i+1;
cin>>fig.vertices[i].y;
}
break;
case 2:
clrscr();
draw(fig,figtrans,transset);
break;
case 3:settrans(rotation,translation,scaling);
getfinaltrans(transmatrix,rotation,translation,scaling);
get_transformed_vertices(figtrans,fig,transmatrix);
transset=1;
}
}
closegraph();
return 01;
}

8. Program to demonstrate 3D transformation with hidden surfaces.


#include<iostream>
#include<graphics.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include <windows.h>
using namespace std;
struct point
{
int x, y, z, w;
};
struct vec
{
int i, j, k;
};
class plane
{
public:
point vertex3d[4];
point vertex2d[4];
point inside3Dpoint, inside2Dpoint;
void draw(int color, int orix, int oriy);
vec normal;
void setnormal();
void drawdotted(int color,int orix, int oriy);
void set3dvertices(point a, point b, point c, point d);
void set2dvertices(point a, point b, point c, point d);
void Set3DInsidePoint();
};
void plane::Set3DInsidePoint(){
inside3Dpoint.x = (vertex3d[0].x + vertex3d[1].x + vertex3d[2].x +
vertex3d[3].x) / 4;

inside3Dpoint.y = (vertex3d[0].y + vertex3d[1].y + vertex3d[2].y +


vertex3d[3].y) / 4;
inside3Dpoint.z = (vertex3d[0].z + vertex3d[1].z + vertex3d[2].z +
vertex3d[3].z) / 4;
}
class frustum
{
public:
int pvisibility[6];
plane planes[6];
point vertex[8];
point vertex_original[8];
void getvertices();
void SetPerspectivePlanesVisibility(int perspz);
void drawviews(int perspz);
void SetPlanesVisibility(vec light);
void getviertices();
void SetPlanes2dVertices(point transvert[8],point insidevertices[]);
void SetPlanes3dVertices();
void drawplanesview(point transvert[8], point insidepoints[6], int
orix, int oriy);
void rotatex(float);
void rotatey(float);
void setplanesnormals();
void Set3DInsidePoint();
void setplanes2Dinsidepoint(point insidepoints[6]);
private:
void frontview();
void topview();
void sideview();
void isoview();
void obliqueview();
void perspview(int copz);
};
void frustum::setplanes2Dinsidepoint(point insidepoints[6]){
for (int i = 0; i < 6; i++){
planes[i].inside2Dpoint = insidepoints[i];
}
}
point transform(point a, float tr[4][4])

{
point t;
a.w;
a.w;
a.w;
a.w;

t.x = tr[0][0] * a.x + tr[0][1] * a.y + tr[0][2] * a.z + tr[0][3] *


t.y = tr[1][0] * a.x + tr[1][1] * a.y + tr[1][2] * a.z + tr[1][3] *
t.z = tr[2][0] * a.x + tr[2][1] * a.y + tr[2][2] * a.z + tr[2][3] *
t.w = tr[3][0] * a.x + tr[3][1] * a.y + tr[3][2] * a.z + tr[3][3] *
return t;

}
void frustum::drawviews(int perspz)
{
cleardevice();
frontview();
perspview(perspz);
topview();
sideview();
isoview();
obliqueview();
}
void frustum::frontview()
{
vec light = { 0, 0, -1 };
SetPlanesVisibility(light);
point transvert[8];
float transm[4][4] = { 0 };
transm[0][0] = 1;
transm[1][1] = 1;
transm[3][3] = 1;
for (int i = 0; i < 8; i++)
{
transvert[i] = transform(vertex[i], transm);
}
point insidepoints[6];
for (int i = 0; i<6; i++){
insidepoints[i] = transform(planes[i].inside3Dpoint, transm);
}
drawplanesview(transvert, insidepoints, 105, 120);

}
void frustum::perspview(int copz){
SetPerspectivePlanesVisibility(copz);
point transvert[8];
float transm[4][4] = { 0 };
transm[0][0]=-copz;
transm[1][1]=-copz;
transm[3][2]=1;
transm[3][3]=-copz;
for (int i = 0; i < 8; i++)
{
transvert[i] = transform(vertex[i], transm);
}
for (int i = 0; i < 8; i++)
{
transvert[i].x /=transvert[i].w;
transvert[i].y /=transvert[i].w;
transvert[i].z /=transvert[i].w;
}
point insidepoints[6];
for (int i = 0; i<6; i++){
insidepoints[i] = transform(planes[i].inside3Dpoint, transm);
}
drawplanesview(transvert, insidepoints, 525, 240);
}
void frustum::sideview()
{
vec light = { -1, 0, 0 };
SetPlanesVisibility(light);
point transvert[8];
float transm[4][4] = { 0 };
transm[0][2] = -1;
transm[1][1] = 1;
transm[2][0] = 0;
transm[3][3] = 1;
for (int i = 0; i < 8; i++)
{
transvert[i] = transform(vertex[i], transm);
}

point insidepoints[6];
for (int i = 0; i<6; i++){
insidepoints[i] = transform(planes[i].inside3Dpoint, transm);
}
drawplanesview(transvert, insidepoints, 315, 120);
}
void frustum::topview()
{
vec light = { 0, 1, 0 };
SetPlanesVisibility(light);
point transvert[8];
float transm[4][4] = { 0 };
transm[0][0] = 1;
transm[1][2] = 1;
transm[2][1] = 0;
transm[3][3] = 1;
for (int i = 0; i < 8; i++)
{
transvert[i] = transform(vertex[i], transm);
}
point insidepoints[6];
for (int i = 0; i<6; i++){
insidepoints[i] = transform(planes[i].inside3Dpoint, transm);
}
drawplanesview(transvert, insidepoints, 525, 120);
}
void frustum::isoview()
{
vec light = { -1, 1, -1 };
SetPlanesVisibility(light);
point transvert[8];
float transm[4][4] = { 0 };
transm[0][0] = 0.707107;
transm[0][2] = -0.707107;
transm[1][0] = 0.408248;
transm[1][1] = 0.816497;
transm[1][2] = 0.408248;
transm[2][0] = 0.57735;
transm[2][1] = -0.57735;

transm[2][2] = 0.57735;
transm[3][3] = 1;
for (int i = 0; i < 8; i++)
{
transvert[i] = transform(vertex[i], transm);
}
point insidepoints[6];
for (int i = 0; i<6; i++){
insidepoints[i] = transform(planes[i].inside3Dpoint, transm);
}
drawplanesview(transvert, insidepoints, 105, 240);
}
void frustum::obliqueview()
{
vec light = { -1, 1, -3 };
SetPlanesVisibility(light);
point transvert[8];
float transm[4][4] = { 0 };
transm[0][0] = 1;
transm[0][2] = 0.5*cos(15);
transm[1][1] = 1;
transm[1][2] = 0.5*sin(15);
transm[3][3] = 1;
for (int i = 0; i < 8; i++)
{
transvert[i] = transform(vertex[i], transm);
}
point insidepoints[6];
for (int i = 0; i<6; i++){
insidepoints[i] = transform(planes[i].inside3Dpoint, transm);
}
drawplanesview(transvert, insidepoints, 315, 240);
}
void frustum::SetPlanesVisibility(vec light)
{
for (int i = 0; i < 6; i++)
{

if ((light.i*planes[i].normal.i + light.j*planes[i].normal.j +
light.k*planes[i].normal.k) <= 0)
pvisibility[i] = 1;
else
pvisibility[i] = 0;
}
}
void frustum::SetPerspectivePlanesVisibility(int perspz){
for (int i = 0; i < 6; i++)
{
if ((planes[i].inside3Dpoint.x*planes[i].normal.i +
planes[i].inside3Dpoint.y*planes[i].normal.j + (planes[i].inside3Dpoint.xperspz)*planes[i].normal.k) <= 0)
pvisibility[i] = 1;
else
pvisibility[i] = 0;
}
}
void frustum::drawplanesview(point transvert[8], point insidepoints[6], int
orix, int oriy)
{
int aggx = 0, aggy = 0;
for (int i = 0; i < 8; i++)
{
aggx += transvert[i].x;
aggy += transvert[i].y;
}
point transl;
transl.x = aggx / 8;
transl.y = aggy / 8;
for (int i = 0; i < 8; i++)
{
transvert[i].x -= transl.x;
transvert[i].y -= transl.y;
}
for (int i = 0; i < 6; i++)
{
insidepoints[i].x -= transl.x;

insidepoints[i].y -= transl.y;
}
//for(int i=0;i<8;i++){
// cout<<transvert[i].x<<"
"<<transvert[i].w<<"\n";

"<<transvert[i].y<<"

"<<transvert[i].z<<"

//}
SetPlanes2dVertices(transvert,insidepoints);
for (int i = 0; i < 6; i++)
{
if (!pvisibility[i])
planes[i].drawdotted(i + 2, orix, oriy);
}
for (int i = 0; i < 6; i++)
{
if (pvisibility[i])
planes[i].draw(i + 2, orix, oriy);
}
}
void plane::draw(int color, int orix, int oriy)
{
setcolor(color);
setlinestyle(0, 1, 4);
for (int i = 0; i < 4; i++)
{
line(vertex2d[i].x + orix, vertex2d[i].y + oriy, vertex2d[(i + 1)
% 4].x + orix, vertex2d[(i + 1) % 4].y + oriy);
}
//FloodFill(inside2Dpoint.x+orix, inside2Dpoint.y+oriy, 0, color);
}
void plane::drawdotted(int color,int orix, int oriy)
{
setcolor(color);
setlinestyle(1, 1, 1);
for (int i = 0; i < 4; i++)
{
line(vertex2d[i].x + orix, vertex2d[i].y + oriy, vertex2d[(i + 1)
% 4].x + orix, vertex2d[(i + 1) % 4].y + oriy);
}
}
void frustum::SetPlanes3dVertices()

{
planes[0].set3dvertices(vertex[3], vertex[2], vertex[1], vertex[0]);
planes[1].set3dvertices(vertex[4], vertex[5], vertex[6], vertex[7]);
planes[2].set3dvertices(vertex[6], vertex[5], vertex[1], vertex[2]);
planes[3].set3dvertices(vertex[1], vertex[5], vertex[4], vertex[0]);
planes[4].set3dvertices(vertex[0], vertex[4], vertex[7], vertex[3]);
planes[5].set3dvertices(vertex[2], vertex[3], vertex[7], vertex[6]);
for (int i = 0; i<6; i++)
planes[i].Set3DInsidePoint();
}
void frustum::SetPlanes2dVertices(point transvert[8], point
insidevertices[6])
{
planes[0].set2dvertices(transvert[3], transvert[2], transvert[1],
transvert[0]);
planes[1].set2dvertices(transvert[4], transvert[5], transvert[6],
transvert[7]);
planes[2].set2dvertices(transvert[6], transvert[5], transvert[1],
transvert[2]);
planes[3].set2dvertices(transvert[1], transvert[5], transvert[4],
transvert[0]);
planes[4].set2dvertices(transvert[0], transvert[4], transvert[7],
transvert[3]);
planes[5].set2dvertices(transvert[2], transvert[3], transvert[7],
transvert[6]);
for (int i = 0; i<6; i++){
planes[i].inside2Dpoint = insidevertices[i];
}
}
void frustum::Set3DInsidePoint(){
for (int i = 0; i<6; i++){
planes[i].Set3DInsidePoint();
}
}
void plane::set3dvertices(point a, point b, point c, point d)
{
vertex3d[0] = a;
vertex3d[1] = b;
vertex3d[2] = c;
vertex3d[3] = d;
}

void plane::set2dvertices(point a, point b, point c, point d)


{
vertex2d[0].x = a.x;
vertex2d[1].x = b.x;
vertex2d[2].x = c.x;
vertex2d[3].x = d.x;
vertex2d[0].y = a.y;
vertex2d[1].y = b.y;
vertex2d[2].y = c.y;
vertex2d[3].y = d.y;
vertex2d[0].w = a.w;
vertex2d[1].w = b.w;
vertex2d[2].w = c.w;
vertex2d[3].w = d.w;
}
void frustum::rotatey(float angle)
{
for (int i = 0; i < 8; i++)
{
vertex[i].y = vertex_original[i].y;
vertex[i].x = vertex_original[i].x*cos(angle) +
vertex_original[i].z*sin(angle);
vertex[i].z = vertex_original[i].z*cos(angle) vertex_original[i].x*sin(angle);
}
SetPlanes3dVertices();
setplanesnormals();
}
void frustum::rotatex(float angle)
{
for (int i = 0; i < 8; i++)
{
vertex[i].x = vertex_original[i].x;
vertex[i].z = vertex_original[i].z*cos(angle) +
vertex_original[i].y*sin(angle);
vertex[i].y = vertex_original[i].y*cos(angle) vertex_original[i].z*sin(angle);
}
SetPlanes3dVertices();
setplanesnormals();

}
void frustum::setplanesnormals()
{
for (int i = 0; i < 6; i++)
{
planes[i].setnormal();
}
}
void plane::setnormal()
{
vec v1, v2;
v1.i = vertex3d[1].x - vertex3d[0].x;
v1.j = vertex3d[1].y - vertex3d[0].y;
v1.k = vertex3d[1].z - vertex3d[0].z;
v2.i = vertex3d[2].x - vertex3d[1].x;
v2.j = vertex3d[2].y - vertex3d[1].y;
v2.k = vertex3d[2].z - vertex3d[1].z;
normal.i = v1.j*v2.k - v1.k*v2.j;
normal.j = v1.k*v2.i - v1.i*v2.k;
normal.k = v1.i*v2.j - v2.i*v1.j;
}
void frustum::getvertices()
{
vertex_original[0] = { 10, 10, -10, 1 };
vertex_original[1] = { 10, 50, -10, 1 };
vertex_original[2] = { 50, 50, -10, 1 };
vertex_original[3] = { 50, 10, -10, 1 };
vertex_original[4] = { 0, 0, -90, 1 };
vertex_original[5] = { 0, 60, -90, 1 };
vertex_original[6] = { 60, 60, -90, 1 };
vertex_original[7] = { 60, 0, -90, 1 };
vertex[0] = { 10, 10, -10, 1 };
vertex[1] = { 10, 50, -10, 1 };
vertex[2] = { 50, 50, -10, 1 };
vertex[3] = { 50, 10, -10, 1 };
vertex[4] = { 0, 0, -90, 1 };
vertex[5] = { 0, 60, -90, 1 };
vertex[6] = { 60, 60, -90, 1 };
vertex[7] = { 60, 0, -90, 1 };

}
void views()
{
frustum f;
f.getvertices();
cout << "Enter the z-cordinate for COP";
int perspz;
cin >> perspz;
f.SetPlanes3dVertices();
f.setplanesnormals();
f.drawviews(perspz);
char choice;
choice = _getch();
while (choice != 'q')
{
if (choice == 'd')
{
float i = 0;
while (!_kbhit())
{
f.rotatey(i);
i = (i + 0.01);
f.drawviews(perspz);
Sleep(100);
}
choice = _getch();
}
else if (choice == 'a')
{
float i = 0;
while (!_kbhit())
{
f.rotatey(-i);
i = (i + 0.01);
f.drawviews(perspz);
Sleep(100);
}
choice = _getch();

}
else if (choice == 'w')
{
float i = 0;
while (!_kbhit())
{
f.rotatex(i);
i = (i + 0.01);
f.drawviews(perspz);
Sleep(100);
}
choice = _getch();
}
else if (choice == 's')
{
float i = 0;
while (!_kbhit())
{
f.rotatex(-i);
i = (i + 0.01);
f.drawviews(perspz);
Sleep(100);
}
choice = _getch();
}
else
{
choice = _getch();
}
}
}
int main()
{
int gdriver = EGA, gmode = EGAHI, errorcode;
int color, maxcolor, x, y;
char msg[80];
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
errorcode = graphresult();

if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
}
views();
getch();
return 1;
}

9. Program to print my name in hindi using Hermite Spline


#include<graphics.h>
#include<iostream>
#include<process.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
using namespace std;
struct point
{
float x,y;
};
struct vec
{
float i,j;
};
void hermite(point start,vec start_tang,point stop, vec stop_tang,point
ori,int color)
{
//hermite curve= (2P0-2P1+M0+M1)(t^3)+(-3P0-2M0+3P1-M1)(t^2)+M0t+P0
vec a,b;
a.i=2*start.x-2*stop.x+start_tang.i+stop_tang.i;
a.j=2*start.y-2*stop.y+start_tang.j+stop_tang.j;
b.i=-3*start.x-2*start_tang.i+3*stop.x-stop_tang.i;
b.j=-3*start.y-2*start_tang.j+3*stop.y-stop_tang.j;
point plotpoint;
for(float t=0; t<=1; t+=0.0001)
{
plotpoint.x=a.i*pow(t,3)+b.i*pow(t,2)+start_tang.i*t+start.x;
plotpoint.y=a.j*pow(t,3)+b.j*pow(t,2)+start_tang.j*t+start.y;
putpixel(ori.x+plotpoint.x+200,ori.y+plotpoint.y+200,color);
}
}
void drawgarvit()
{
point p0={0,0};
point p1={0,60};
vec m0={0,1};
vec m1={0,-1};

point ori={0,0};
hermite(p0,m0,p1,m1,ori,3);
ori={30,0};
hermite(p0,m0,p1,m1,ori,3);
ori={60,0};
hermite(p0,m0,p1,m1,ori,3);
ori={90,0};
hermite(p0,m0,p1,m1,ori,3);
ori={120,0};
hermite(p0,m0,p1,m1,ori,3);
p0={-10,0};
p1={130,0};
ori={0,0};
m0={1,0};
m1={1,0};
hermite(p0,m0,p1,m1,ori,3);
p0={0,60};
p1={0,45};
m0={-60,0};
m1={10,1};
ori={0,0};
hermite(p0,m0,p1,m1,ori,3);
p0={0,0};
p1={30,0};
m0={-10,-100};
m1={10,100};
ori={60,0};
hermite(p0,m0,p1,m1,ori,3);
p0={0,0};
p1={10,-25};
m0={-1,-10};
m1={50,1};
ori={90,0};
hermite(p0,m0,p1,m1,ori,3);
p0={0,0};
p1={-15,10};
m0={-20,-50};
m1={0,1};
ori={90,30};

hermite(p0,m0,p1,m1,ori,3);
p0={-15,10};
p1={0,20};
m0={0,1};
m1={20,1};
ori={90,30};
hermite(p0,m0,p1,m1,ori,3);
p0={0,0};
p1={-20,20};
m0={-60,0};
m1={0,100};
ori={120,40};
hermite(p0,m0,p1,m1,ori,3);
}
int main()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
drawgarvit();
getch();
return 1;
}

You might also like