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

Computer Graphics Practical Code

This document contains code snippets for implementing various computer graphics algorithms: 1. A line drawing algorithm using Bresenham's line drawing method. 2. An algorithm for drawing a circle using midpoint circle algorithm. 3. Code for drawing Bezier and Hermite curves.

Uploaded by

Paritosh Tonk
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)
34 views

Computer Graphics Practical Code

This document contains code snippets for implementing various computer graphics algorithms: 1. A line drawing algorithm using Bresenham's line drawing method. 2. An algorithm for drawing a circle using midpoint circle algorithm. 3. Code for drawing Bezier and Hermite curves.

Uploaded by

Paritosh Tonk
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/ 5

1. Program to implement DDA 2. Bresenhams Line Drawing 3. Program to implement Mid- 8. bezier curve 8.

Hermite curve
Line Drawing Algorithm. Algorithm. Point Circle Drawing Algorithm. #include<iostream.h> #include #include<iostream.h>
#include<iostream.h> #include<iostream.h> #include<iostream.h> <graphics.h> #include <stdlib.h> #include <graphics.h> #include
#include<conio.h> #include<conio.h> #include<conio.h> #include <stdio.h> #include <conio.h> <stdlib.h> #include <stdio.h> #include
#include<graphics.h> #include<graphics.h> #include<graphics.h> #include<math.h> #define YMAX 320 <conio.h> #include<math.h> //define
#include<math.h> #include<math.h> #include<math.h> #define XMAX 640 screen sizes #define YMAX 320
void midpoint(int x1,int y1,int x2,int void circlep(int h, int k, int x, int y, int double p[100][2]; double tp[200][2]; #define XMAX 640
void line1(float xi, float yi, float xe,
y2) { value) { void bezier_curve() { double double p[100][2]; double tp[100][2];
float ye);
int dx=x2-x1; putpixel(h+x, k+y, value); nt,x=0,y=0; int n,i; cout<<"Enter the void hermite_display() { double
void main() {
int dy=y2-y1; putpixel(h+x, k-y, value); number of points : "; cin>>n; nt,t=0,x=0,y=0; int n,i;
clrscr(); cout<<"Enter coordinates for each cout<<"Enter the number of points :
int d=2*dy-dx; putpixel(h-x, k+y, value);
float x1,x2,y1,y2; point\n"; for(i=0;i<n;i++) { "; cin>>n;
int incrE=2*dy; putpixel(h-x, k-y, value);
cout<<"INITIAL POINTS "; int incrNE=2*(dy-dx); putpixel(h+y, k+x, value); cout<<"(X"<<i<<","<<"Y"<<i<<")"<<":"; cout<<"Enter coordinates for each
cout<<endl<<"Enter X co-ordinate "; putpixel(x1,y1,WHITE); putpixel(h+y, k-x, value); cin>>p[i][0]; point\n";
cin>>x1; cout<<"Enter Y co- int x,y=y1,max; putpixel(h-y, k+x, value); cin>>p[i][1]; } for(i=0;i<n;i++)
ordinate "; if(x1<x2) { putpixel(h-y, k-x, value); cout<<"Enter value of no of {
cin>>y1; cout<<endl<<"FINAL x=x1; } increments in t:"; cin>>nt; cout<<"(X"<<i<<","<<"Y"<<i<<")"<<":";
POINTS "; max=x2; void circle1(int h, int k, int r) { cout<<"Enter 2 tangent points for cin>>p[i][0]; cin>>p[i][1]; }
cout<<endl<<"Enter X co-ordinate } int x=0; each pair of points \n"; cout<<"Enter value of no of
"; else { int y=r; for(i=0;i<2*n-2;i++) { increments in t :"; cin>>nt;
cin>>x2; cout<<"Enter Y co- x=x2; double d=5.0/4.0-r; cout<<"(TX"<<i<<","<<"TY"<<i<<")"<<" cout<<"Enter tangent vectors for
ordinate "; max=x1; } circlep(h,k,x,y,WHITE); :"; cin>>tp[i][0]; cin>>tp[i][1]; } each point \n"; for(i=0;i<n-1;i++) {
cin>>y2; while(x<max) { while(y>x) { for(i=0;i<n-1;i++) { double incr=1/nt; cout<<"(TX"<<i<<","<<"TY"<<i<<")"<<"
if(d<0) { if(d<0) for(double t=0;t<1;t+=incr) { :"; cin>>tp[i][0]; cin>>tp[i][1]; }
int gd=DETECT, gm;
d+=incrE; d+=2.0*x+3.0; x=pow(1- clearviewport();
initgraph(&gd, &gm, "..\\bgi");
x++; else { t,3)*p[i][0]+3*t*pow(1t,2)*tp[i][0]+3* for(i=0;i<n-1;i++) { double incr=1/nt;
clearviewport();
} d+=2.0*(x-y)+5.0; t*t*(1t)*tp[i+1][0]+t*t*t*p[i+1][0]; for(double t=0;t<1;t+=incr) {
line1(x1,y1,x2,y2); y=pow(1- x=(2*t*t*t-
else { y--;
getch(); t,3)*p[i][1]+3*t*pow(1t,2)*tp[i][1]+3* 3*t*t+1)*p[i][0]+(2*t*t*t+3*t*t)*p[i+
d+=incrNE; }
closegraph(); x++; x++; t*t*(1t)*tp[i+1][1]+t*t*t*p[i+1][1]; 1][0]+(t*t*t2*t*t+t)*tp[i][0]+(t*t*t-
} y++; } circlep(h,k,x,y,WHITE); putpixel(x,YMAX-y,15); } } t*t)*tp[i+1][0]; y=(2*t*t*t-
void line1(float xi, float yi, float xe, putpixel(x,y,WHITE); } putpixel(p[n-1][0],YMAX- 3*t*t+1)*p[i][1]+(2*t*t*t+3*t*t)*p[i+
float ye) { } } } p[n1][1],15); } 1][1]+(t*t*t2*t*t+t)*tp[i][1]+(t*t*t-
float dy=ye-yi; float dx=xe-xi; void main() { void main() { void main() { /* request auto t*t)*tp[i+1][1]; putpixel(x,YMAX-
float m=dy/dx; if(m<=1) { int x1,x2,y1,y2; int x,y,r; detection */ int gdriver = DETECT, y,15);
for(int i=xi; i<=xe; i++) { cout<<"INITIAL POINTS "; cout<<"\n Enter X co-ordinate of gmode, errorcode; } }
putpixel(xi,yi,WHITE); cout<<endl<<"Enter X co-ordinate "; centre: "; /* initialize graphics mode */ putpixel(p[n-1][0],YMAX-p[n-
xi++; cin>>x1; cin>>x; initgraph(&gdriver, &gmode, ""); 1][1],15); }
yi+=m; cout<<"Enter Y co-ordinate "; cout<<"\n Enter Y co-ordinate of /* read result of initialization */ int main() { /* request auto
yi=floor(yi+0.5); cin>>y1; centre: "; errorcode = graphresult(); detection */ int gdriver = DETECT,
cout<<endl<<"FINAL POINTS "; cin>>y; if (errorcode != grOk) /* an error gmode, errorcode;
}
cout<<endl<<"Enter X co-ordinate "; cout<<"\n Enter radius:"; occurred */ { printf("Graphics /* initialize graphics mode */
}
cin>>x2; cin>>r; error: %s\n", initgraph(&gdriver, &gmode, "");
else
cout<<"Enter Y co-ordinate "; int gd=DETECT,gm; grapherrormsg(errorcode)); /* read result of initialization */
{ for(int i=yi; i<=ye; i++) { printf("Press any key to halt:"); errorcode = graphresult();
cin>>y2; initgraph(&gd, &gm,"..\\bgi");
putpixel(xi,yi,WHITE); getch(); exit(1); /* return if (errorcode != grOk) /* an error
int gd=DETECT; clearviewport();
yi++; int gm; circle1(x,y,r); with error code */ } bezier_curve(); occurred */ {
xi+=1/m; initgraph(&gd,&gm,"..\\bgi"); getch(); getch(); } printf("Graphics error: %s\n",
xi=floor(xi+0.5); clearviewport(); closegraph(); grapherrormsg(errorcode));
} midpoint(x1,y1,x2,y2); } printf("Press any key to halt:");
} getch(); getch(); exit(1); /* return
} closegraph(); with error code */ }
} hermite_display(); getch(); }
4. Program to clip a Line using region[0][1]=1; } if(sum[1]>=1) {
Cohen-Sutherland Line Clipping else if(y1>ymax) { cout<<"\nPoint ("<<x2<<","<<y2<<")
Algorithm. region[0][0]=1; } is rejected";
#include<iostream.h> if(x2<xmin) { if(x2>xmax) { x=xmax;
#include<conio.h> region[1][3]=1; } y=floor(m*(x-x1)+y1); if(y>=ymin
#include<graphics.h> else if(x2>xmax) { && y<=ymax) {
#include<math.h> region[1][2]=1; } cout<<"\nPoint ("<<x<<","<<y<<") is
void else if(y2<ymin) { accepted";
cohen_clip(float,float,float,float,float,f region[1][1]=1; } c_reg[2]=x;
loat,float,float); else if(y2>ymax) { c_reg[3]=y; } }
void main() { region[1][0]=1; } else if(x2<xmin) {
float gotoxy(85,74); x=xmin;
x_min,y_min,x_max,y_max,x1,y1,x2,y cout<<"\nOutcode of y=floor(m*(x-x1)+y1);
2; ("<<x1<<","<<y1<<") is: "; if(y>=ymin && y<=ymax) {
cout<<"Enter lower left coordinates of for(int j=0;j<4;j++) { cout<<"\nPoint ("<<x<<","<<y<<") is
the clipping window :"; cout<<region[0][j]; accepted";
cin>>x_min>>y_min; sum[0]+=region[0][j]; } c_reg[2]=x;
cout<<"Enter upper right coordinates cout<<"\nOutcode of c_reg[3]=y; } }
of the clipping window :"; ("<<x2<<","<<y2<<") is: "; if(y2>ymax) {
cin>>x_max>>y_max; for(j=0;j<4;j++) { y=ymax;
cout<<"Enter initial coordinates of line cout<<region[1][j]; x=floor((1/m)*(y-y1)+x1);
: "; sum[1]+=region[1][j]; } if(x>=xmin && x<=xmax) {
cin>>x1>>y1; for(j=0;j<4;j++) { cout<<"\nPoint ("<<x<<","<<y<<") is
cout<<"Enter final coordinates of line : prod+=region[0][j]*region[1][j];} accepted";
"; if(prod==0 && (sum[0]>=1 || c_reg[2]=x;
cin>>x2>>y2; sum[1]>=1)) { c_reg[3]=y; } }
int gd=DETECT; int gm; cout<<"\nThe line is partially visible else if(y2<ymin) {
initgraph(&gd,&gm,"..\\bgi"); (both may or may not) "; y=ymin;
clearviewport(); float m=(y2-y1)/(x2-x1); x=floor((1/m)*(y-y1)+x1);
rectangle(x_min,y_min,x_max,y_max) cout<<"\n m is "<<m; if(x>=xmin && x<=xmax) {
; float x,y; cout<<"\nPoint ("<<x<<","<<y<<") is
line(x1,y1,x2,y2); if(sum[0]>=1) { accepted";
cohen_clip(x_min,y_min,x_max,y_ma cout<<"\nPoint ("<<x1<<","<<y1<<") c_reg[2]=x;
x,x1,y1,x2,y2); is rejected"; c_reg[3]=y; } } }
getch(); if(x1>xmax || x1<xmin) { if( xmin<=x && x<=xmax && ymin<=y
} if(x1>xmax) && y<=ymax)
void cohen_clip(float xmin,float { x=xmax;} cout<<"\n may case :: line is partially
ymin,float xmax, float ymax,float else { x=xmin;} visible";
x1,float y1,float x2,float y2) { y=floor(m*(x-x1)+y1); else
int region[2][4],prod=0,sum[2]={0,0}; if(y>=ymin && y<=ymax) { cout<<"\n may not case:: line is not in
float c_reg[4]; cout<<"\nPoint ("<<x<<","<<y<<") is clipped region";
c_reg[0]=x1; accepted"; setcolor(RED);
c_reg[1]=y1; c_reg[0]=x; line(c_reg[0],c_reg[1],c_reg[2],c_reg[3
c_reg[2]=x2; c_reg[1]=y; } } ]); }
c_reg[3]=y2; if(y1>ymax || y1<ymin) { else if(prod==0 && !(sum[0]>=1 ||
for(int i=0;i<2;i++) { if(y1>ymax) { y=ymax;} sum[1]>=1)) {
for(int j=0;j<4;j++) { else { y=ymin;} cout<<"\nThe line is fully visible";
region[i][j]=0; } } x=floor((1/m)*(y-y1)+x1); }
if(x1<xmin) { if(x>=xmin && x<=xmax) { else if(prod>=0) {
region[0][3]=1; } cout<<"\nPoint ("<<x<<","<<y<<") is cout<<"\nThe line does not fall in
else if(x1>xmax) { accepted"; clipped region"; setcolor(WHITE); }
region[0][2]=1; } c_reg[0]=x; }
else if(y1<ymin) { c_reg[1]=y; } } }
5. Program to clip a Polygon m=(y2-y1)/(x2-x1); polyy[i]=polyy[0];
using Sutherland-Hodgeman else polyy[i+1]=polyy[1];
Algorithm. m=100000; for(i=0;i < 2*n+2;i++)
#include<iostream.h> if(x1 <= xmax && x2 <= xmax) { poly[i]=round(polyy[i]);
#include<conio.h> arr[k]=x2; initgraph(&gd,&gm,"..\\BGI");
#include<graphics.h> arr[k+1]=y2; setcolor(RED);
#define round(a) ((int)(a+0.5)) k+=2; } rectangle(xmin,ymax,xmax,ymin);
int k; if(x1 > xmax && x2 <= xmax) { cout<<"\t\tUNCLIPPED POLYGON";
float xmin,ymin,xmax,ymax,arr[20],m; arr[k]=xmax; setcolor(WHITE);
void clipl(float x1,float y1,float x2,float arr[k+1]=y1+m*(xmax-x1); fillpoly(n,poly);
y2) { arr[k+2]=x2; getch();
if(x2-x1) arr[k+3]=y2; cleardevice();
m=(y2-y1)/(x2-x1); k+=4; } k=0;
else if(x1 <= xmax && x2 > xmax) { for(i=0;i < 2*n;i+=2)
m=100000; arr[k]=xmax; clipl(polyy[i],polyy[i+1],polyy[i+2],poly
if(x1 >= xmin && x2 >= xmin) { arr[k+1]=y1+m*(xmax-x1); y[i+3]);
arr[k]=x2; k+=2; } } n=k/2;
arr[k+1]=y2; void clipb(float x1,float y1,float for(i=0;i < k;i++)
k+=2; } x2,float y2) { polyy[i]=arr[i];
if(x1 < xmin && x2 >= xmin) { if(y2-y1) polyy[i]=polyy[0];
arr[k]=xmin; m=(x2-x1)/(y2-y1); polyy[i+1]=polyy[1];
arr[k+1]=y1+m*(xmin-x1); else k=0;
arr[k+2]=x2; m=100000; for(i=0;i < 2*n;i+=2)
arr[k+3]=y2; if(y1 >= ymin && y2 >= ymin) { clipt(polyy[i],polyy[i+1],polyy[i+2],poly
k+=4; } arr[k]=x2; y[i+3]);
if(x1 >= xmin && x2 < xmin) { arr[k+1]=y2; n=k/2;
arr[k]=xmin; k+=2; } for(i=0;i < k;i++)
arr[k+1]=y1+m*(xmin-x1); if(y1 < ymin && y2 >= ymin) { polyy[i]=arr[i];
k+=2; } } arr[k]=x1+m*(ymin-y1); polyy[i]=polyy[0];
void clipt(float x1,float y1,float arr[k+1]=ymin; polyy[i+1]=polyy[1];
x2,float y2) { arr[k+2]=x2; k=0;
if(y2-y1) arr[k+3]=y2; for(i=0;i < 2*n;i+=2)
m=(x2-x1)/(y2-y1); k+=4; } clipr(polyy[i],polyy[i+1],polyy[i+2],poly
else if(y1 >= ymin && y2 < ymin) { y[i+3]);
m=100000; arr[k]=x1+m*(ymin-y1); n=k/2;
if(y1 <= ymax && y2 <= ymax) { arr[k+1]=ymin; for(i=0;i < k;i++)
arr[k]=x2; k+=2; polyy[i]=arr[i];
arr[k+1]=y2; } } polyy[i]=polyy[0];
k+=2; void main() { polyy[i+1]=polyy[1];
} int gd=DETECT,gm,n,poly[20]; k=0;
if(y1 > ymax && y2 <= ymax) { float xi,yi,xf,yf,polyy[20]; for(i=0;i < 2*n;i+=2)
arr[k]=x1+m*(ymax-y1); clrscr(); clipb(polyy[i],polyy[i+1],polyy[i+2],pol
arr[k+1]=ymax; cout<<"Coordinates of rectangular yy[i+3]);
arr[k+2]=x2; clip window :\nxmin,ymin :"; for(i=0;i < k;i++)
arr[k+3]=y2; cin>>xmin>>ymin; poly[i]=round(arr[i]);
k+=4; } cout<<"xmax,ymax :"; if(k)
if(y1 <= ymax && y2 > ymax) { cin>>xmax>>ymax; fillpoly(k/2,poly);
arr[k]=x1+m*(ymax-y1); cout<<"\n\nPolygon to be clipped setcolor(RED);
arr[k+1]=ymax; :\nNumber of sides :"; rectangle(xmin,ymax,xmax,ymin);
k+=2; } } cin>>n; cout<<"\tCLIPPED POLYGON";
void clipr(float x1,float y1,float cout<<"Enter the coordinates :"; getch();
x2,float y2) { for(int i=0;i<2*n;i++) closegraph();
if(x2-x1) cin>>polyy[i]; }
6. Program to apply 2D line(p[0][i]+320, -p[1][i]+240, void POLYGON :: translation(int cout << "\n\n\t\tPolygon after int Sx,Sy,type,i,j;
Transformations on a 2D object p[0][i+1]+320, -p[1][i+1]+240); p[10][10],int n) { Reflection..."; for(i=0;i<3;i++)
using Homogeneous Co- else int tx,ty,i,j; draw_poly(Result,n); } for(j=0;j<3;j++) {
line(p[0][i]+320, -p[1][i]+240, cout << "\n\n\t\tEnter X-Translation void POLYGON :: rotation(int if(i == j) X[i][j] = 1;
Ordinates.
p[0][0]+320, -p[1][0]+240); } tx: "; p[][10],int n) { else X[i][j] = 0; }
#include<iostream.h>
getch(); cin >> tx; float cout << "\n\n **** Shearing Types
#include<stdlib.h>
closegraph(); } cout << "\n\n\t\tEnter Y-Translation type,rotate[10][10],result[10][10],i,j,A ****";
#include<dos.h>
void POLYGON :: draw_polyfloat(float ty: "; ng,Sinang,Cosang; cout << "\n\n\t\t1.X-Direction Shear
#include<conio.h>
p[][10], int n) { cin >> ty; cout << "\n\n\t\tEnter the angle of \n\n\t\t2.Y-Direction Shear "; cout <<
#include<graphics.h>
int i,gd = DETECT,gm; for(i=0;i<3;i++) rotation in degrees: "; "\n\n\t\tEnter your choice(1-2): ";
#include<math.h>
initgraph(&gd,&gm,"..\\BGI"); for(j=0;j<3;j++) cin >> Ang; cin >> type;
class POLYGON {
line(320,0,320,480); X[i][j] = 0; cout << "\n\n **** Rotation Types if(type == 1) {
private: int
line(0,240,640,240); X[0][0] = X[1][1] = X[2][2] = 1; ****"; cout << "\n\n\t\tEnter X-Shear Sx: ";
p[10][10],Result[10][10],X[10][10];
for(i=0;i<n;i++) { X[0][2] = tx; cout << "\n\n\t\t1.Clockwise Rotation cin >> Sx; X[0][1] = Sx; }
public: int accept_poly(int [][10]);
if(i!=n-1) X[1][2] = ty; \n\n\t\t2.Anti-Clockwise Rotation "; else {
void draw_poly(int [][10],int);
line(int(p[0][i])+320, -int(p[1][i])+240, matmult(X,p,3,3,n,Result); cout << "\n\n\t\tEnter your choice(1- cout << "\n\n\t\tEnter Y-Shear Sy: ";
void draw_polyfloat(float [][10],int);
int(p[0][i+1])+320, - cout << "\n\n\t\tPolygon after 2): "; cin >> Sy; X[1][0] = Sy; }
void matmult(int [][10],int
int(p[1][i+1])+240); Translation..."; cin >> type; matmult(X,p,3,3,n,Result);
[][10],int,int,int,int [][10]);
else draw_poly(Result,n); } Ang = (Ang * 6.2832)/360; cout << "\n\n\t\tPolygon after
void matmultfloat(float [][10],int
line(int(p[0][i])+320, -int(p[1][i])+240, void POLYGON :: reflection(int Sinang = sin(Ang); Shearing..."; draw_poly(Result,n); }
[][10],int,int,int,float [][10]);
int(p[0][0])+320, -int(p[1][0])+240); } p[][10],int n) { Cosang = cos(Ang); int menu() {
void shearing(int [][10],int);
getch(); int type,i,j; for(i=0;i<3;i++) int ch;
void scaling(int [][10],int);
closegraph(); } cout << "\n\n **** Reflection Types for(j=0;j<3;j++) cout << "\n\n **** 2-D
void rotation(int [][10],int);
void POLYGON :: matmult(int ****"; rotate[i][j] = 0; TRANSFORMATION ****"; cout <<
void translation(int [][10],int);
mat1[][10],int mat2[][10],int r1,int cout << "\n\n\t\t1.About X-Axis rotate[0][0] = rotate[1][1] = Cosang; "\n\n\t\t1.Translation
void reflection(int [][10],int); };
c1,int c2,int mat3[][10]) { \n\n\t\t2.About Y-Axis rotate[0][1] = rotate[1][0] = Sinang; \n\n\t\t2.Scaling \n\n\t\t3.Rotation
int POLYGON :: accept_poly(int
int i,j,k; \n\n\t\t3.About Origin \ rotate[2][2] = 1; \n\n\t\t4.Reflection
p[][10]) {
for(i=0;i<10;i++) \n\n\t\t4.About Line y = x if(type == 1) rotate[1][0] = -Sinang; \n\n\t\t5.Shearing \n\n\t\t6.Exit";
int i,n;
for(j=0;j<10;j++) \n\n\t\t5.About Line y = -x \ else cout<<"\n\n\tEnter your choice(1-6):
cout << "\n\n\t\tEnter no.of
mat3[i][j] = 0; \n\n\t\tEnter your choice(1-5): "; rotate[0][1] = Sinang; ";
vertices:";
for(i=0;i<r1;i++) cin >> type; matmultfloat(rotate,p,3,3,n,result); cin >> ch;
cin >> n;
for(j=0;j<c2;j++) for(i=0;i<3;i++) cout << "\n\n\t\tPolygon after return ch; }
for(i=0;i<n;i++) {
for(k=0;k<c1;k++) for(j=0;j<3;j++) { Rotation..."; void main() {
cout << "\n\n\t\tEnter (x,y)Co-
mat3[i][j] = mat3[i][j]+(mat1[i][k] * if(i == j) X[i][j] = 1; draw_polyfloat(result,n); } int ch,n,p[10][10];
ordinate of point P" << i << ": ";
mat2[k][j]); else X[i][j] = 0; } void POLYGON :: scaling(int p[][10],int POLYGON p1;
cin >> p[0][i] >> p[1][i];
mat3[c2][0] = mat3[0][0]; switch(type) { n) { clrscr();
p[2][i] = 1; }
mat3[c2][1] = mat3[0][1]; case 1: X[1][1] = -1; float cout << "\n\n **** 2-D
p[0][n] = p[0][0];
mat3[c2][2] = mat3[0][2]; } break; Sx,Sy,result[10][10],scale[10][10],i,j; TRANSFORMATION ****";
p[1][n] = p[1][0];
void POLYGON :: matmultfloat(float case 2: X[0][0] = -1; cout << "\n\n\t\tEnter X-Scaling Sx: "; n = p1.accept_poly(p);
p[2][n] = 1;
mat1[][10],int mat2[][10],int r1,int break; cin >> Sx; clrscr();
for(i=0;i<n;i++) { cout<<"\n";
c1,int c2,float mat3[][10]) { case 3: X[1][1] = -1; cout << "\n\n\t\tEnter Y-Scaling Sy: "; cout << "\n\n\t\tOriginal Polygon ...";
for(int j=0;j<3;j++) {
int i,j,k; X[0][0] = -1; cin >> Sy; p1.draw_poly(p,n);
cout<<p[i][j]<<"\t"; } }
for(i=0;i<10;i++) break; for(i=0;i<3;i++) do { ch = menu();
getch();
for(j=0;j<10;j++) case 4: for(j=0;j<3;j++) switch(ch) { case 1:
return n; }
mat3[i][j] = 0; X[1][1] = 0; X[0][0] = 0; scale[i][j] = 0; p1.translation(p,n); break;
void POLYGON :: draw_poly(int
for(i=0;i<r1;i++) X[1][0] = 1; X[0][1] = 1; scale[0][0] = Sx; case 2: p1.scaling(p,n); break;
p[][10], int n) {
for(j=0;j<c2;j++) break; scale[1][1] = Sy; case 3: p1.rotation(p,n); break;
int i,gd = DETECT,gm;
for(k=0;k<c1;k++) case 5: X[1][1] = 0; scale[2][2] = 1; case 4: p1.reflection(p,n); break;
initgraph(&gd,&gm,"..\\BGI");
mat3[i][j] = mat3[i][j]+(mat1[i][k] * X[0][0] = 0; matmultfloat(scale,p,3,3,n,result); case 5: p1.shearing(p,n); break;
line(320,0,320,480);
mat2[k][j]); X[1][0] = -1; X[0][1] = -1; cout << "\n\n\t\tPolygon after case 6: exit(0); }
line(0,240,640,240);
mat3[c2][0] = mat3[0][0]; break; } Scaling..."; draw_polyfloat(result,n); } }while(ch!=6);
for(i=0;i<n;i++) {
mat3[c2][1] = mat3[0][1]; matmult(X,p,3,3,n,Result); void POLYGON :: shearing(int getch(); }
if(i!=n-1)
mat3[c2][2] = mat3[0][2]; } p[][10],int n) {
7. 3D transformationon a 3D default: exit(0); } Poly(); for(i=0;i<4;i++) { for(j=0;j<4;j++) { t[0][1]=sx; t[0][2]=sx; t[1][0]=sy; beta=(beta*3.14)/180; }
object parallel and perspective cout<<"\n\tDo you want to perform if(i==j) rotate[i][j]=1; else t[1][2]=sy; t[2][0]=sz; t[2][1]=sz; t[0][0]=cos(alpha);
#include<iostream.h> more operations(y=yes,n=no) : "; rotate[i][j]=0; } } getch(); Matrix_mul(p,t); } t[0][1]=sin(beta)*sin(alpha);
#include<stdlib.h> #include<conio.h> cin>>ch1; }while(ch1=='y'||ch1=='Y'); resett(); cout<<"\nEnter the angle of void Scaling() { float sx=1,sy=1,sz=1; t[1][1]=cos(beta); t[2][0]=sin(alpha);
#include<graphics.h> getch(); return 0; } rotation about X-axis in degrees :"; int ch; t[2][1]=-(cos(alpha)*sin(beta));
#include<math.h> void Matrix_mul(float p[][4],float cin>>ang; ang=(ang*3.14)/180; cout<<"\n\tEnter X scaling factor\t:"; t[2][2]=0; break; } Matrix_mul(p,t);
float q[4][4]) { int i,j,k; for(i=0;i<10;i++) { cosang=cos(ang); sinang=sin(ang); cin>>sx; cout<<"\n\tEnter Y scaling Matrix_mul(orig,pz); }
p[11][4],temp[11][4],orig[11][4],t[4][4 for(j=0;j<4;j++) { temp[i][j]=0; t[1][1]=cosang; t[1][2]=-sinang; factor\t:"; cin>>sy; void Perspective() { float pp,qq,rr;
],pz [4][4]; const int n=8; for(k=0;k<4;k++) { t[2][1]=sinang; t[2][2]=cosang; cout<<"\n\tEnter Z scaling factor\t:"; resett();
void Poly(); void Translation(); void temp[i][j]+=p[i][k]*q[k][j]; } } } Matrix_mul(rotate,t); resett(); cin>>sz; cout<<"\n\tPerspective
Rotation(); void Reflection(); void for(i=0;i<10;i++) { p[i][0]=temp[i][0]; cout<<"\nEnter the angle of rotation t[0][0]=sx; t[1][1]=sy; t[2][2]=sz; projection:\t";
Shearing(); void Scaling(); void p[i][1]=temp[i][1]; p[i][2]=temp[i][2]; about Y-axis in degrees :"; cin>>ang; getch(); cout<<"\n\tEnter center of projection
resett(); void Matrix_mul(float p[i][3]=temp[i][3]; } } ang=(ang*3.14)/180; Matrix_mul(p,t); resett(); } on X axis:\t"; cin>>pp;
p[][4],float q[4][4]); void Normalize(); void Poly() { Normalize(); int ch; cosang=cos(ang); sinang=sin(ang); void Normalize() { int i,j; cout<<"\n\tEnter center of projection
void Perspective(); cout<<"\nEnter the type of t[0][0]=cosang; t[0][2]=-sinang; for(i=0;i<10;i++) { for(j=0;j<4;j++) { on Y axis:\t"; cin>>qq;
void Parallel(); projection\n\t1.Parallel\n\t2.Perspect t[2][0]=sinang; t[2][2]=cosang; p[i][j]=(p[i][j]/p[i][3]); } } cout<<"\n\tEnter center of projection
int main() { int i,ch,j,h; char ch1; ive\t "; cin>>ch; if(ch==1) Parallel(); Matrix_mul(rotate,t); resett(); } on Z axis:\t"; cin>>rr;
cout<<"\nEnter the vertices (x,y,z) of else Perspective(); cout<<"\nEnter the angle of rotation void Parallel() { float pp=-(1/pp); qq=-(1/qq); rr=-(1/rr);
front face(z!=0)\n"; for(i=0;i<4;i++) { Normalize(); int i,gd = DETECT,gm; about Z-axis in degrees :"; cin>>ang; f,alpha,beta,fx,fy,fz; fz=0; resett(); if(pp!=0) t[0][3]=pp; if(qq!=0)
cout<<"\tX"<<i<<" , Y "<<i<<" , Z initgraph(&gd,&gm,""); ang=(ang*3.14)/180; cout<<"\n\tParallel projection:\n"; t[1][3]=qq; if(rr!=0) t[2][3]=rr;
"<<i<<"\t"; line(320,0,320,480); cosang=cos(ang); sinang=sin(ang); int ch,ch1; cout<<"\n\t 1. Matrix_mul(p,t); Matrix_mul(p,pz);
cin>>p[i][0]>>p[i][1]>>p[i][2]; line(0,240,640,240); t[0][0]=cosang; t[0][1]=sinang; Orthographic(on Z=0)\n\t 2. Oblique Matrix_mul(orig,pz); getch();
p[i][3]=orig[i][3]=1; orig[i][0]=p[i][0]; for(i=0;i<4;i++) { setcolor(15); t[1][0]=-sinang; t[1][1]=cosang; projection\n\t3. Axonometric }
orig[i][1]=p[i][1]; orig[i][2]=p[i][2]; } line(int(orig[i][0])+320, Matrix_mul(rotate,t); resett(); projection\n\t Enter your choice:";
cout<<"\n\tEnter the depth: "; int(orig[i][1])+240, Matrix_mul(p,rotate); } cin>>ch;
cin>>h; p[4][3]=orig[4][3]=1; int(orig[i+1][0])+320, - void Translation() { int tx,ty,tz; switch(ch) { case 1: t[2][2]=0;
orig[4][0]=p[4][0]=p[0][0]; int(orig[i+1][1])+240); cout<<"\n\tEnter X-Translation tx: "; break; case 2:
orig[4][1]=p[4][1]=p[0][1]; line(int(orig[i+5][0])+320, cin>>tx; cout<<"\n\tEnter Y- cout<<"\n\t1.Cabinet\n\t2.Cavillier\n\
orig[4][2]=p[4][2]=p[0][2]; int(orig[i+5][1])+240, Translation ty: "; tEnte r your choice:"; cin>>ch1;
for(i=5;i<10;i++) { int(orig[i+6][0])+320, cin>>ty; cout<<"\n\tEnter Z- if(ch1==1)
p[i][0]=orig[i][0]=p[i-5][0]; int(orig[i+6][1])+240); Translation tz: "; cin>>tz; f=0.5; else f=1;
p[i][1]=orig[i][1]=p[i-5][1]; setcolor(10); line(int(p[i][0])+320, t[3][0]=tx; t[3][1]=ty; t[3][2]=tz; cout<<"\n\tEnter angle alpha :";
p[i][2]=orig[i][2]=p[i-5][2]-h; int(p[i][1])+240, int(p[i+1][0])+320, Matrix_mul(p,t); } cin>>alpha;
p[i][3]=orig[i][3]=1; } int(p[i+1][1])+240); void Reflection() { int ch; float alpha=(alpha*3.14)/180; t[2][0]=-
cout<<"\nEntered matrix\n"; line(int(p[i+5][0])+320, m,c,ang,cosang,sinang; cout<<"\n f*cos(alpha); t[2][1]=-f*sin(alpha);
for(i=0;i<=n;i++) { if(i!=4) int(p[i+5][1])+240, int(p[i+6][0])+320, **** Reflection Types ****"; break; case 3: cout<<"\n\t1.
cout<<p[i][0]<<"\t"<<p[i][1]<<"\t"<<p[ int(p[i+6][1])+240); } cout<<"\n\t1.About X-Y plane Trimetric \n\t2. Dimetric\n\t
i ][2]<<"\t"<<p[i][3]<<"\n"; } for(i=0;i<4;i++) { setcolor(15); \n\t2.About Y-Z plane\n\t3.About X-Z 3.Isometric\n\t Enter your choice: ";
getch(); for(i=0;i<4;i++) { line(int(orig[i][0])+320, plane\t\n"; cin>>ch; cin>>ch1; if(ch1==1) {
for(j=0;j<4;j++) { if(i==j) pz[i][j]=1; int(orig[i][1])+240, switch(ch) { case 1: t[2][2]=-1; cout<<"\nEnter angle(in degrees) of
else pz[i][j]=0; } } pz[2][2]=0; do{ int(orig[i+5][0])+320, - break; case 2: t[0][0]=-1; break; rotation around Y then angle of
resett(); clrscr(); int(orig[i+5][1])+240); setcolor(10); case 3: t[1][1]=-1; break; } rotation around X :";
cout<<"\n\n\t\t1.Translation line(int(p[i][0])+320, int(p[i][1])+240, Matrix_mul(p,t); } cin>>alpha>>beta;
\n\n\t\t2.Scaling\n\n\t\t3.Rotation int(p[i+5][0])+320, int(p[i+5][1])+240); void Shearing() { float sx,sy,sz=0; alpha=(alpha*3.14)/180;
\n\n\t\t4.Reflection } cout<<"\n\t1.X-Direction Shear beta=(beta*3.14)/180; } else
\n\n\t\t5.Shearing \n\n\t\t6.Exit"; getch(); closegraph(); \n\t2.YDirection Shear \n\t3. if(ch1==2) { cout<<"\nEnter
cout<<"\n\n\tEnter your choice(1-6): for(i=0;i<10;i++) { for(int j=0;j<4;j++) Combined Shearing "; forshortening factor Fz"; cin>>f;
"; cin>>ch; { p[i][j]=orig[i][j]; } } } cout<<"\n\tEnter shear X factor\t:"; beta=asin(fz/1.414);
switch(ch) { case 1: Translation(); void resett() { int i,j; for(i=0;i<4;i++) { cin>>sx; cout<<"\n\tEnter shear Y alpha=asin(fz/(sqrt(2-fz*fz))); } else
break; case 2: Scaling(); break; for(j=0;j<4;j++) { if(i==j) t[i][j]=1; factor\t:"; cin>>sy; if(ch1==3) { cout<<"\nIsometric
case 3: Rotation(); break; case 4: else t[i][j]=0; } } } cout<<"\n\tEnter shear Z factor\t:"; Projection"; alpha=45;
Reflection(); break; case 5: void Rotation() { int i,j; float cin>>sz; beta=35.26439;
Shearing(); break; case 6: exit(0); ang,sinang,cosang,rotate[4][4]; alpha=(alpha*3.14)/180;

You might also like