• No results found

Cpp Programs

N/A
N/A
Protected

Academic year: 2021

Share "Cpp Programs"

Copied!
117
0
0

Loading.... (view fulltext now)

Full text

(1)

Draw a line using DDA Line Drawing Algorithm.

Hardik H Hadvani

on 00:01 1 comments

Write a program to draw a line using DDA Line

DrawingAlgorithm.

(A)Center Line

(B)Dotted Line

Friends this program is for the draw a line using dda line drawing

algorithm using center line or dotted line both code are given to

you as follows.This program is usually used in the computer

graphic and it is run in the turbo c. I hope this code is very true

and if any kindly request please replay me on email.

(A)Center Line

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gd=DETECT,gm,xa,xb,ya,yb,i;

double xin,yin,dx,dy,x,y,steps;

clrscr();

initgraph(&gd,&gm,"");

printf("Enter Xa=");

scanf("%d",&xa);

printf("Enter Ya=");

scanf("%d",&ya);

(2)

printf("Enter Xa=");

scanf("%d",&xb);

printf("Enter Yb=");

scanf("%d",&yb);

cleardevice();

if(xb>xa)

dx=xb-xa;

else

dx=xa-xb;

if(yb>ya)

dy=yb-ya;

else

dy=ya-yb;

if(dx>dy)

steps=dx;

else

steps=dy;

xin=dx/steps;

yin=dy/steps;

x=xa;y=ya;

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

{

if(i%6==4 || i%6==0)

putpixel((int)x,(int)y,BLACK);

else

putpixel((int)x,(int)y,WHITE);

x=x+xin;

y=y+yin;

}

(3)

getch();

closegraph();

}

(B)Dotted Line

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gd=DETECT,gm,xa,xb,ya,yb,i;

double xin,yin,dx,dy,x,y,steps;

clrscr();

initgraph(&gd,&gm,"");

printf("Enter Xa=");

scanf("%d",&xa);

printf("Enter Ya=");

scanf("%d",&ya);

printf("Enter Xa=");

scanf("%d",&xb);

printf("Enter Yb=");

scanf("%d",&yb);

cleardevice();

if(xb>xa)

dx=xb-xa;

else

dx=xa-xb;

(4)

if(yb>ya)

dy=yb-ya;

else

dy=ya-yb;

if(dx>dy)

steps=dx;

else

steps=dy;

xin=dx/steps;

yin=dy/steps;

x=xa;y=ya;

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

{

if(i%5==1 || i%5==2 || i%5==3)

putpixel((int)x,(int)y,WHITE);

x=x+xin;

y=y+yin;

}

getch();

closegraph();

}

(5)

# include <graphics.h> # include <math.h> # include <conio.h> # include <iostream.h>

void DDALine(int x1,int y1,int x2,int y2,int iColor); void main() { int gDriver=DETECT,gMode; int x1,x2,y1,y2,iColor; initgraph(&gDriver,&gMode,"c:\\tc\\bgi"); cleardevice(); cout<<endl<<"Enter x1 : "; cin>>x1; cout<<"Enter y1 : "; cin>>y1; cout<<endl<<"Enter x2 : "; cin>>x2; cout<<"Enter y2 : "; cin>>y2; cout<<endl<<"Enter COLOR : "; cin>>iColor; cleardevice(); DDALine(320,1,320,480,12); DDALine(1,240,640,240,12); circle(320,240,2); DDALine(320+x1,240-y1,320+x2,240-y2,iColor%16); getch(); }

void DDALine(int x1,int y1,int x2,int y2,int iColor) { float dX,dY,iSteps; float xInc,yInc,iCount,x,y; dX = x1 - x2; dY = y1 - y2; if (fabs(dX) > fabs(dY)) { iSteps = fabs(dX); } else { iSteps = fabs(dY); } xInc = dX/iSteps; yInc = dY/iSteps; x = x1; y = y1; circle(x,y,1);

for (iCount=1; iCount<=iSteps; iCount++) { putpixel(floor(x),floor(y),iColor); x -= xInc; y -= yInc; } circle(x,y,1);

(6)

return; }

(7)

/*BRESENHAAM ALGORITHM FOR LINE DRAWING*/

#include

<iostream.h>

#include

<graphics.h>

#include

<stdio.h>

#include

<conio.h>

#include

<stdlib.h>

#include

<math.h>

#include

<dos.h>

void bhm_line(int,int,int,int,int);

void main()

{

int ghdriver=DETECT,ghmode,errorcode,x1,x2,y1,y2;

initgraph(&ghdriver,&ghmode,

"..\\bgi"

);

errorcode = graphresult();

if(errorcode !=grOk)

{

cout<<

"Graphics error:%s\n"

<<grapherrormsg(errorcode);

cout<<

"Press any key to halt:"

;

getch();

exit(

1

);

}

clrscr();

cout<<

"Enter the coordinates (x1,y1): "

;

cin>>x1>>y1;

cout<<

"Enter the coordinates (x2,y2): "

;

cin>>x2>>y2;

bhm_line(x1,y1,x2,y2,

1

);

getch();

}

void bhm_line(int x1,int y1,int x2,int y2,int c)

{

int x,y,dx,dy,dx1,dy1,px,py,xe,ye,i;

dx=x2-x1;

dy=y2-y1;

dx1=fabs(dx);

dy1=fabs(dy);

px=

2

*dy1-dx1;

py=

2

*dx1-dy1;

if(dy1<=dx1)

{

if(dx>=

0

)

{

x=x1;

y=y1;

xe=x2;

}

else

{

(8)

x=x2;

y=y2;

xe=x1;

}

putpixel(x,y,c);

for(i=

0

;x<xe;i++)

{

x=x+

1

;

if(px<

0

)

{

px=px+

2

*dy1;

}

else

{

if((dx<

0

&& dy<

0

) || (dx>

0

&& dy>

0

))

{

y=y+

1

;

}

else

{

y=y-

1

;

}

px=px+

2

*(dy1-dx1);

}

delay(

0

);

putpixel(x,y,c);

}

}

else

{

if(dy>=

0

)

{

x=x1;

y=y1;

ye=y2;

}

else

{

x=x2;

y=y2;

ye=y1;

}

putpixel(x,y,c);

for(i=

0

;y<ye;i++)

{

y=y+

1

;

if(py<=

0

)

{

(9)

py=py+

2

*dx1;

}

else

{

if((dx<

0

&& dy<

0

) || (dx>

0

&& dy>

0

))

{

x=x+

1

;

}

else

{

x=x-

1

;

}

py=py+

2

*(dx1-dy1);

}

delay(

0

);

putpixel(x,y,c);

}

}

}

(10)

Code for Program to draw a circle using Bresenham's Circle Algorithm

in C++ Programming

# include <iostream.h> # include <graphics.h> # include <conio.h> # include <math.h> void show_screen( );

void bresenham_circle(constint,constint,constint); int main( ) { int driver=VGA; int mode=VGAHI; int h=0; int k=0; int r=0; do { show_screen( ); gotoxy(8,10);

cout<<"Central Point of the Circle : (h,k) :"; gotoxy(8,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(12,13);

cout<<"Enter the value of h = "; cin>>h;

gotoxy(12,14);

cout<<"Enter the value of k = "; cin>>k;

gotoxy(8,18);

cout<<"Radius of the Circle : r :"; gotoxy(8,19);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(12,21);

cout<<"Enter the value of r = "; cin>>r;

initgraph(&driver,&mode,"..\\Bgi"); setcolor(15);

bresenham_circle(h,k,r); setcolor(15);

outtextxy(110,460,"Press <Enter> to continue or any other key to exit."); int key=int(getch( ));

if(key!=13) break; }

(11)

while(1); return 0; } /*************************************************************************///--- bresenham_circle( ) /*************************************************************************///--- ---///*************************************************************************/void

bresenham_circle(constint h,constint k,constint r) { int color=getcolor( ); int x=0; int y=r; int p=(3-(2*r)); do { putpixel((h+x),(k+y),color); putpixel((h+y),(k+x),color); putpixel((h+y),(k-x),color); putpixel((h+x),(k-y),color); putpixel((h-x),(k-y),color); putpixel((h-y),(k-x),color); putpixel((h-y),(k+x),color); putpixel((h-x),(k+y),color); x++; if(p<0) p+=((4*x)+6); else { y--; p+=((4*(x-y))+10); } } while(x<=y); } /*************************************************************************///--- show_screen( ) /*************************************************************************///--- ---///*************************************************************************/void show_screen( ) { restorecrtmode( ); textmode(C4350); cprintf("\n*************************************************************************** *****"); cprintf("*********************** -***********************"); cprintf("*--- "); textbackground(1);

cprintf(" Bresenham's Circle Algorithm "); textbackground(8);

(12)

cprintf("*********************** -***********************");

cprintf("*-****************************************************************************-*"); for(int count=0;count<42;count++)

cprintf("*-* *-*"); gotoxy(1,46); cprintf("*-****************************************************************************-*"); cprintf("*---*"); cprintf("***************************************************************************** ***"); gotoxy(1,2); }

(13)

Code for Program to draw a circle using MidPoint Circle Algorithm in

C++ Programming

# include <iostream.h> # include <graphics.h> # include <conio.h> # include <math.h> void show_screen( );

void midpoint_circle(constint,constint,constint); int main( ) { int driver=VGA; int mode=VGAHI; int h=0; int k=0; int r=0; do { show_screen( ); gotoxy(8,10);

cout<<"Central Point of the Circle : (h,k) :"; gotoxy(8,11);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(12,13);

cout<<"Enter the value of h = "; cin>>h;

gotoxy(12,14);

cout<<"Enter the value of k = "; cin>>k;

gotoxy(8,18);

cout<<"Radius of the Circle : r :"; gotoxy(8,19);

cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(12,21);

cout<<"Enter the value of r = "; cin>>r;

initgraph(&driver,&mode,"..\\Bgi"); setcolor(15);

midpoint_circle(h,k,r); setcolor(15);

outtextxy(110,460,"Press <Enter> to continue or any other key to exit."); int key=int(getch( ));

if(key!=13) break; }

(14)

while(1); return 0; } /*************************************************************************///--- midpoint_circle( ) /*************************************************************************///--- ---///*************************************************************************/void

midpoint_circle(constint h,constint k,constint r) { int color=getcolor( ); int x=0; int y=r; int p=(1-r); do { putpixel((h+x),(k+y),color); putpixel((h+y),(k+x),color); putpixel((h+y),(k-x),color); putpixel((h+x),(k-y),color); putpixel((h-x),(k-y),color); putpixel((h-y),(k-x),color); putpixel((h-y),(k+x),color); putpixel((h-x),(k+y),color); x++; if(p<0) p+=((2*x)+1); else { y--; p+=((2*(x-y))+1); } } while(x<=y); } /*************************************************************************///--- show_screen( ) /*************************************************************************///--- ---///*************************************************************************/void show_screen( ) { restorecrtmode( ); textmode(C4350); cprintf("\n*************************************************************************** *****"); cprintf("************************ -**********************-*"); cprintf("*--- "); textbackground(1);

cprintf(" MidPoint Circle Algorithm "); textbackground(8);

(15)

cprintf("************************ -**********************-*");

cprintf("*-****************************************************************************-*"); for(int count=0;count<42;count++)

cprintf("*-* *-*"); gotoxy(1,46); cprintf("*-****************************************************************************-*"); cprintf("*---*"); cprintf("***************************************************************************** ***"); gotoxy(1,2); }

(16)

Code for Program to illustrate the implementation of Rotation

Transformation along a Pivot Point in C++ Programming

# include <iostream.h>

# include <graphics.h> # include <conio.h> # include <math.h>

void show_screen( );

void apply_pivot_point_rotation(constint,int [],float,constint,constint); void multiply_matrices(constfloat[3],constfloat[3][3],float[3]);

void Polygon(constint,constint []);

void Line(constint,constint,constint,constint);

int main( ) { int driver=VGA; int mode=VGAHI; initgraph(&driver,&mode,"..\\Bgi"); show_screen( ); int polygon_points[8]={ 250,290, 320,190, 390,290, 250,290 }; setcolor(15); Polygon(5,polygon_points); setcolor(15); settextstyle(0,0,1);

outtextxy(50,400,"*** (320,240) is taken as Pivot Point."); outtextxy(50,415,"*** Use '+' and '-' Keys to apply Rotation."); int key_code=0; char Key=NULL; do { Key=NULL; key_code=0; Key=getch( ); key_code=int(Key); if(key_code==0) {

Key=getch( ); key_code=int(Key); } if(key_code==27) break; elseif(key_code==43) { setfillstyle(1,0); bar(40,70,600,410);

(17)

apply_pivot_point_rotation(4,polygon_points,5,320,240); setcolor(10); Polygon(4,polygon_points); } elseif(key_code==45) { setfillstyle(1,0); bar(40,70,600,410); apply_pivot_point_rotation(4,polygon_points,-5,320,240); setcolor(12); Polygon(4,polygon_points); } } while(1); return 0; } /*************************************************************************///-- apply_pivot_point_rotation( ) /*************************************************************************///--- ---///*************************************************************************/void

apply_pivot_point_rotation(constint n,int coordinates[], float angle,constint xr,constint yr) {

angle*=(M_PI/180);

for(int count_1=0;count_1<n;count_1++) {

float matrix_a[3]={coordinates[(count_1*2)], coordinates[((count_1*2)+1)],1}; float temp_1=(((1-cos(angle))*xr)+(yr*sin(angle))); float temp_2=(((1-cos(angle))*yr)-(xr*sin(angle))); float matrix_b[3][3]={ { cos(angle),sin(angle),0 } , { -sin(angle),cos(angle),0 } ,

{ temp_1,temp_2,1 } }; float matrix_c[3]={0};

multiply_matrices(matrix_a,matrix_b,matrix_c); coordinates[(count_1*2)]=(int)(matrix_c[0]+0.5); coordinates[((count_1*2)+1)]=(int)(matrix_c[1]+0.5); }

}

/************************************************************************///--- multiply_matrices( ) /************************************************************************///---

---///************************************************************************/void

multiply_matrices(constfloat matrix_1[3],

constfloat matrix_2[3][3],float matrix_3[3]) {

for(int count_1=0;count_1<3;count_1++) {

for(int count_2=0;count_2<3;count_2++) matrix_3[count_1]+=

(18)

} }

/*************************************************************************///- Polygon( ) /*************************************************************************///---

---///*************************************************************************/void

Polygon(constint n,constint coordinates[]) {

if(n>=2) {

Line(coordinates[0],coordinates[1],

coordinates[2],coordinates[3]); for(int count=1;count<(n-1);count++)

Line(coordinates[(count*2)],coordinates[((count*2)+1)], coordinates[((count+1)*2)], coordinates[(((count+1)*2)+1)]); } } /*************************************************************************///-- Line( ) /*************************************************************************///--- ---///*************************************************************************/void

Line(constint x_1,constint y_1,constint x_2,constint y_2) { int color=getcolor( ); int x1=x_1; int y1=y_1; int x2=x_2; int y2=y_2; if(x_1>x_2) { x1=x_2; y1=y_2; x2=x_1; y2=y_1; } int dx=abs(x2-x1); int dy=abs(y2-y1); int inc_dec=((y2>=y1)?1:-1); if(dx>dy) { int two_dy=(2*dy); int two_dy_dx=(2*(dy-dx)); int p=((2*dy)-dx); int x=x1; int y=y1; putpixel(x,y,color); while(x<x2) { x++; if(p<0)

(19)

p+=two_dy; else { y+=inc_dec; p+=two_dy_dx; } putpixel(x,y,color); } } else { int two_dx=(2*dx); int two_dx_dy=(2*(dx-dy)); int p=((2*dx)-dy); int x=x1; int y=y1; putpixel(x,y,color); while(y!=y2) { y+=inc_dec; if(p<0) p+=two_dx; else { x++; p+=two_dx_dy; } putpixel(x,y,color); } } } /*************************************************************************///--- show_screen( ) /*************************************************************************///--- ---///*************************************************************************/void show_screen( ) { setfillstyle(1,1); bar(140,26,485,38); settextstyle(0,0,1); setcolor(15); outtextxy(5,5,"*********************************************************************** *******"); outtextxy(5,17,"*-**************************************************************************-*"); outtextxy(5,29,"* ---*"); outtextxy(5,41,"*-**************************************************************************-*"); outtextxy(5,53,"*-**************************************************************************-*");

(20)

setcolor(11);

outtextxy(150,29,"Rotation Transformation along Pivot Point"); setcolor(15);

for(int count=0;count<=30;count++)

outtextxy(5,(65+(count*12)),"*-* *-*"); outtextxy(5,438,"*-**************************************************************************-*"); outtextxy(5,450,"*--- ---*"); outtextxy(5,462,"********************************************************************* *********"); setcolor(12);

outtextxy(229,450,"Press any Key to exit."); }

(21)

Code for Program to illustrate the implementation of Rotation

Transformation in C++ Programming

# include <iostream.h> # include <graphics.h> # include <conio.h> # include <math.h> void show_screen( );

void apply_rotation(constint,int [],float);

void multiply_matrices(constfloat[3],constfloat[3][3],float[3]); void Polygon(constint,constint []);

void Line(constint,constint,constint,constint);

int main( ) { int driver=VGA; int mode=VGAHI; initgraph(&driver,&mode,"..\\Bgi"); show_screen( ); int polygon_points[8]={ 250,290, 320,190, 390,290, 250,290 }; setcolor(15); Polygon(5,polygon_points); setcolor(15); settextstyle(0,0,1);

outtextxy(50,415,"*** Use '+' and '-' Keys to apply Rotation."); int key_code=0; char Key=NULL; do { Key=NULL; key_code=0; Key=getch( ); key_code=int(Key); if(key_code==0) {

Key=getch( ); key_code=int(Key); } if(key_code==27) break; elseif(key_code==43) { setfillstyle(1,0); bar(40,70,600,410);

(22)

apply_rotation(4,polygon_points,5); setcolor(10); Polygon(4,polygon_points); } elseif(key_code==45) { setfillstyle(1,0); bar(40,70,600,410); apply_rotation(4,polygon_points,-5); setcolor(12); Polygon(4,polygon_points); } } while(1); return 0; } /*************************************************************************/// apply_rotation( ) /*************************************************************************///--- ---///*************************************************************************/void

apply_rotation(constint n,int coordinates[],float angle) {

angle*=(M_PI/180);

for(int count_1=0;count_1<n;count_1++) {

float matrix_a[3]={coordinates[(count_1*2)], coordinates[((count_1*2)+1)],1}; float matrix_b[3][3]={ { cos(angle),sin(angle),0 } , { -sin(angle),cos(angle),0 } ,

{ 0,0,1 } }; float matrix_c[3]={0};

multiply_matrices(matrix_a,matrix_b,matrix_c); coordinates[(count_1*2)]=(int)(matrix_c[0]+0.5); coordinates[((count_1*2)+1)]=(int)(matrix_c[1]+0.5); }

}

/************************************************************************///--- multiply_matrices( ) /************************************************************************///---

---///************************************************************************/void

multiply_matrices(constfloat matrix_1[3],

constfloat matrix_2[3][3],float matrix_3[3]) {

for(int count_1=0;count_1<3;count_1++) {

for(int count_2=0;count_2<3;count_2++) matrix_3[count_1]+= (matrix_1[count_2]*matrix_2[count_2][count_1]); } } /*************************************************************************///- Polygon( ) /*************************************************************************///---

(23)

---///*************************************************************************/void Polygon(constint n,constint coordinates[])

{

if(n>=2) {

Line(coordinates[0],coordinates[1],

coordinates[2],coordinates[3]); for(int count=1;count<(n-1);count++)

Line(coordinates[(count*2)],coordinates[((count*2)+1)], coordinates[((count+1)*2)], coordinates[(((count+1)*2)+1)]); } } /*************************************************************************///-- Line( ) /*************************************************************************///--- ---///*************************************************************************/void

Line(constint x_1,constint y_1,constint x_2,constint y_2) { int color=getcolor( ); int x1=x_1; int y1=y_1; int x2=x_2; int y2=y_2; if(x_1>x_2) { x1=x_2; y1=y_2; x2=x_1; y2=y_1; } int dx=abs(x2-x1); int dy=abs(y2-y1); int inc_dec=((y2>=y1)?1:-1); if(dx>dy) { int two_dy=(2*dy); int two_dy_dx=(2*(dy-dx)); int p=((2*dy)-dx); int x=x1; int y=y1; putpixel(x,y,color); while(x<x2) { x++; if(p<0) p+=two_dy; else { y+=inc_dec; p+=two_dy_dx;

(24)

} putpixel(x,y,color); } } else { int two_dx=(2*dx); int two_dx_dy=(2*(dx-dy)); int p=((2*dx)-dy); int x=x1; int y=y1; putpixel(x,y,color); while(y!=y2) { y+=inc_dec; if(p<0) p+=two_dx; else { x++; p+=two_dx_dy; } putpixel(x,y,color); } } } /*************************************************************************///--- show_screen( ) /*************************************************************************///--- ---///*************************************************************************/void show_screen( ) { setfillstyle(1,1); bar(212,26,412,38); settextstyle(0,0,1); setcolor(15); outtextxy(5,5,"*********************************************************************** *******"); outtextxy(5,17,"*-**************************************************************************-*"); outtextxy(5,29,"*- ---*"); outtextxy(5,41,"*-**************************************************************************-*"); outtextxy(5,53,"*-**************************************************************************-*"); setcolor(11); outtextxy(222,29,"Rotation Transformation"); setcolor(15);

(25)

for(int count=0;count<=30;count++) outtextxy(5,(65+(count*12)),"*-* *-*"); outtextxy(5,438,"*-**************************************************************************-*"); outtextxy(5,450,"*--- ---*"); outtextxy(5,462,"********************************************************************* *********"); setcolor(12);

outtextxy(229,450,"Press any Key to exit."); }

(26)

# include <iostream.h> # include <graphics.h> # include <conio.h> # include <math.h> # define f 0.3 # define projection_angle 45 void show_screen( );

void apply_rotation_along_x_axis(constint [5][3],constint); void multiply_matrices(constfloat[4],constfloat[4][4],float[4]); void draw_pyramid(int [5][3]);

void get_projected_point(int&,int&,int&); void Line(constint,constint,constint,constint);

int main( ) { int driver=VGA; int mode=VGAHI; initgraph(&driver,&mode,"..\\Bgi"); show_screen( ); int pyramid[5][3]={

{280,130,50}, // base front left {360,130,50}, // base front right {360,130,-50}, // base back right {280,130,-50}, // base back left {320,20,0} // top }; setcolor(15); draw_pyramid(pyramid); setcolor(15); settextstyle(0,0,1);

outtextxy(50,415,"*** Use + & - Keys to apply Rotation along x-axis."); int angle=0; int key_code=0; char Key=NULL; do { Key=NULL; key_code=0; Key=getch( ); key_code=int(Key); if(key_code==0) {

Key=getch( ); key_code=int(Key);

(27)

} if(key_code==27) break; elseif(key_code==43) angle-=5; elseif(key_code==45) angle+=5; setfillstyle(1,0); bar(40,70,600,410); apply_rotation_along_x_axis(pyramid,angle); } while(1); return 0; } /*************************************************************************//********** ***************************************************************/// Funcion Definitions ***************************************************************///***************************************************************///***************************************************************///***************************************************************///***************************************************************///***************************************************************/// ---///*************************************************************************//******** *****************************************************************//******************* ******************************************************///--- apply_rotation_along_x_axis( ) ---///*************************************************************************/void

apply_rotation_along_x_axis(constint control_points[5][3], constint theta)

{

int edge_points[5][3]={0}; float angle=(theta*(M_PI/180)); for(int count=0;count<5;count++) { edge_points[count][0]=control_points[count][0]; edge_points[count][1]=control_points[count][1]; edge_points[count][2]=control_points[count][2]; float matrix_a[4]={edge_points[count][0],edge_points[count][1], edge_points[count][2],1}; float matrix_b[4][4]={ { 1,0,0,0 } , { 0,cos(angle),sin(angle),0 } , { 0,-sin(angle),cos(angle),0 } , { 0,0,0,1 } }; float matrix_c[4]={0}; multiply_matrices(matrix_a,matrix_b,matrix_c); edge_points[count][0]=(int)(matrix_c[0]+0.5); edge_points[count][1]=(int)(matrix_c[1]+0.5); edge_points[count][2]=(int)(matrix_c[2]+0.5); }

setcolor(10);

(28)

}

/************************************************************************///--- multiply_matrices( ) /************************************************************************///---

---///************************************************************************/void

multiply_matrices(constfloat matrix_1[4],

constfloat matrix_2[4][4],float matrix_3[4]) {

for(int count_1=0;count_1<4;count_1++) {

for(int count_2=0;count_2<4;count_2++) matrix_3[count_1]+= (matrix_1[count_2]*matrix_2[count_2][count_1]); } } /************************************************************************///- draw_pyramid( ) /************************************************************************///--- ---///************************************************************************/void

draw_pyramid(int points[5][3]) {

int edge_points[5][3]; for(int i=0;i<5;i++) { edge_points[i][0]=points[i][0]; edge_points[i][1]=points[i][1]; edge_points[i][2]=points[i][2]; get_projected_point(edge_points[i][0], edge_points[i][1],edge_points[i][2]); edge_points[i][1]+=240; } Line(edge_points[0][0],edge_points[0][1], edge_points[1][0],edge_points[1][1]); Line(edge_points[1][0],edge_points[1][1], edge_points[2][0],edge_points[2][1]); Line(edge_points[2][0],edge_points[2][1], edge_points[3][0],edge_points[3][1]); Line(edge_points[3][0],edge_points[3][1], edge_points[0][0],edge_points[0][1]); Line(edge_points[0][0],edge_points[0][1], edge_points[4][0],edge_points[4][1]); Line(edge_points[1][0],edge_points[1][1], edge_points[4][0],edge_points[4][1]); Line(edge_points[2][0],edge_points[2][1], edge_points[4][0],edge_points[4][1]); Line(edge_points[3][0],edge_points[3][1], edge_points[4][0],edge_points[4][1]); } /************************************************************************///--- get_projected_point( ) /************************************************************************///--- ---///************************************************************************/void

get_projected_point(int& x,int& y,int& z) {

float fcos0=(f*cos(projection_angle*(M_PI/180))); float fsin0=(f*sin(projection_angle*(M_PI/180)));

(29)

float Par_v[4][4]={ {1,0,0,0}, {0,1,0,0}, {fcos0,fsin0,0,0}, {0,0,0,1} }; float xy[4]={x,y,z,1}; float new_xy[4]={0}; multiply_matrices(xy,Par_v,new_xy); x=(int)(new_xy[0]+0.5);

y=(int)(new_xy[1]+0.5); z=(int)(new_xy[2]+0.5); }

/*************************************************************************///-- Line( ) /*************************************************************************///---

---///*************************************************************************/void

Line(constint x_1,constint y_1,constint x_2,constint y_2) { int color=getcolor( ); int x1=x_1; int y1=y_1; int x2=x_2; int y2=y_2; if(x_1>x_2) { x1=x_2; y1=y_2; x2=x_1; y2=y_1; } int dx=abs(x2-x1); int dy=abs(y2-y1); int inc_dec=((y2>=y1)?1:-1); if(dx>dy) { int two_dy=(2*dy); int two_dy_dx=(2*(dy-dx)); int p=((2*dy)-dx); int x=x1; int y=y1; putpixel(x,y,color); while(x<x2) { x++; if(p<0) p+=two_dy; else

(30)

{ y+=inc_dec; p+=two_dy_dx; } putpixel(x,y,color); } } else { int two_dx=(2*dx); int two_dx_dy=(2*(dx-dy)); int p=((2*dx)-dy); int x=x1; int y=y1; putpixel(x,y,color); while(y!=y2) { y+=inc_dec; if(p<0) p+=two_dx; else { x++; p+=two_dx_dy; } putpixel(x,y,color); } } } /*************************************************************************///--- show_screen( ) /*************************************************************************///--- ---///*************************************************************************/void show_screen( ) { setfillstyle(1,1); bar(210,26,420,38); settextstyle(0,0,1); setcolor(15); outtextxy(5,5,"*********************************************************************** *******"); outtextxy(5,17,"*-**************************************************************************-*"); outtextxy(5,29,"*--- ---*"); outtextxy(5,41,"*-**************************************************************************-*"); outtextxy(5,53,"*-**************************************************************************-*"); setcolor(11);

(31)

setcolor(15);

for(int count=0;count<=30;count++)

outtextxy(5,(65+(count*12)),"*-* *-*"); outtextxy(5,438,"*-**************************************************************************-*"); outtextxy(5,450,"*--- ---*"); outtextxy(5,462,"********************************************************************* *********"); setcolor(12);

outtextxy(229,450,"Press any Key to exit."); }

(32)

Code for Program to illustrate the implementation of Reflection

Transformation about the line y=x and y=-x in C++ Programming

# include <iostream.h>

# include <graphics.h> # include <conio.h> # include <math.h>

void show_screen( );

void apply_reflection_about_line_yex(constint,int []); void apply_reflection_about_line_yemx(constint,int []); void apply_reflection_along_x_axis(constint,int []); void apply_reflection_along_y_axis(constint,int []); void apply_rotation(constint,int [],float);

void multiply_matrices(constfloat[3],constfloat[3][3],float[3]); void Polygon(constint,constint []);

void Line(constint,constint,constint,constint);

void Dashed_line(constint,constint,constint,constint,constint=0);

int main( ) { int driver=VGA; int mode=VGAHI; initgraph(&driver,&mode,"..\\Bgi"); show_screen( ); setcolor(15); Line(320,100,320,400); Line(315,105,320,100); Line(320,100,325,105); Line(315,395,320,400); Line(320,400,325,395); Line(150,240,500,240); Line(150,240,155,235); Line(150,240,155,245); Line(500,240,495,235); Line(500,240,495,245); Dashed_line(160,400,460,100,0); Dashed_line(180,100,480,400,0); settextstyle(2,0,4); outtextxy(305,85,"y-axis"); outtextxy(305,402,"y'-axis"); outtextxy(505,233,"x-axis"); outtextxy(105,233,"x'-axis");

outtextxy(350,100,"Reflection about the line y=x"); outtextxy(115,100,"Reflection about the line y=-x"); int x_polygon[8]={ 340,200, 420,120, 370,120, 340,200 };

(33)

int y_polygon[8]={ 300,200, 220,120, 270,120, 300,200 }; setcolor(15); Polygon(4,x_polygon); Polygon(4,y_polygon); apply_reflection_about_line_yex(4,x_polygon); apply_reflection_about_line_yemx(4,y_polygon); setcolor(7); Polygon(4,x_polygon); Polygon(4,y_polygon); getch( ); return 0; } /*************************************************************************///--- apply_reflection_about_line_yex( ) /*************************************************************************///---/*************************************************************************/// ---///*************************************************************************/void

apply_reflection_about_line_yex(constint n,int coordinates[]) { apply_rotation(n,coordinates,45); apply_reflection_along_x_axis(n,coordinates); apply_rotation(n,coordinates,-45); } /*************************************************************************///--- apply_reflection_about_line_yemx( ) /*************************************************************************///---/*************************************************************************///- ---///*************************************************************************/void

apply_reflection_about_line_yemx(constint n,int coordinates[]) { apply_rotation(n,coordinates,45); apply_reflection_along_y_axis(n,coordinates); apply_rotation(n,coordinates,-45); } /*************************************************************************/// apply_rotation( ) /*************************************************************************///--- ---///*************************************************************************/void

apply_rotation(constint n,int coordinates[],float angle) {

float xr=320; float yr=240; angle*=(M_PI/180);

for(int count_1=0;count_1<n;count_1++) {

float matrix_a[3]={coordinates[(count_1*2)], coordinates[((count_1*2)+1)],1}; float temp_1=(((1-cos(angle))*xr)+(yr*sin(angle))); float temp_2=(((1-cos(angle))*yr)-(xr*sin(angle))); float matrix_b[3][3]={ { cos(angle),sin(angle),0 } , { -sin(angle),cos(angle),0 } ,

{ temp_1,temp_2,1 } }; float matrix_c[3]={0};

(34)

coordinates[(count_1*2)]=(int)(matrix_c[0]+0.5); coordinates[((count_1*2)+1)]=(int)(matrix_c[1]+0.5); }

}

/*************************************************************************///- apply_reflection_along_x_axis( ) /*************************************************************************///---

---///*************************************************************************/void

apply_reflection_along_x_axis(constint n,int coordinates[]) {

for(int count=0;count<n;count++) { float matrix_a[3]={coordinates[(count*2)], coordinates[((count*2)+1)],1}; float matrix_b[3][3]={ {1,0,0} , {0,-1,0} ,{ 0,0,1} }; float matrix_c[3]={0}; multiply_matrices(matrix_a,matrix_b,matrix_c); coordinates[(count*2)]=matrix_c[0]; coordinates[((count*2)+1)]=(480+matrix_c[1]); } } /*************************************************************************///- apply_reflection_along_y_axis( ) /*************************************************************************///--- ---///*************************************************************************/void

apply_reflection_along_y_axis(constint n,int coordinates[]) {

for(int count=0;count<n;count++) { float matrix_a[3]={coordinates[(count*2)], coordinates[((count*2)+1)],1}; float matrix_b[3][3]={ {-1,0,0} , {0,1,0} ,{ 0,0,1} }; float matrix_c[3]={0}; multiply_matrices(matrix_a,matrix_b,matrix_c); coordinates[(count*2)]=(640+matrix_c[0]); coordinates[((count*2)+1)]=matrix_c[1]; } } /************************************************************************///--- multiply_matrices( ) /************************************************************************///--- ---///************************************************************************/void

multiply_matrices(constfloat matrix_1[3],

constfloat matrix_2[3][3],float matrix_3[3]) {

for(int count_1=0;count_1<3;count_1++) {

for(int count_2=0;count_2<3;count_2++) matrix_3[count_1]+= (matrix_1[count_2]*matrix_2[count_2][count_1]); } } /*************************************************************************///- Polygon( ) /*************************************************************************///--- ---///*************************************************************************/void

Polygon(constint n,constint coordinates[]) {

(35)

if(n>=2) {

Line(coordinates[0],coordinates[1],

coordinates[2],coordinates[3]); for(int count=1;count<(n-1);count++)

Line(coordinates[(count*2)],coordinates[((count*2)+1)], coordinates[((count+1)*2)], coordinates[(((count+1)*2)+1)]); } } /*************************************************************************///-- Line( ) /*************************************************************************///--- ---///*************************************************************************/void

Line(constint x_1,constint y_1,constint x_2,constint y_2) { int color=getcolor( ); int x1=x_1; int y1=y_1; int x2=x_2; int y2=y_2; if(x_1>x_2) { x1=x_2; y1=y_2; x2=x_1; y2=y_1; } int dx=abs(x2-x1); int dy=abs(y2-y1); int inc_dec=((y2>=y1)?1:-1); if(dx>dy) { int two_dy=(2*dy); int two_dy_dx=(2*(dy-dx)); int p=((2*dy)-dx); int x=x1; int y=y1; putpixel(x,y,color); while(x<x2) { x++; if(p<0) p+=two_dy; else { y+=inc_dec; p+=two_dy_dx; }

(36)

putpixel(x,y,color); } } else { int two_dx=(2*dx); int two_dx_dy=(2*(dx-dy)); int p=((2*dx)-dy); int x=x1; int y=y1; putpixel(x,y,color); while(y!=y2) { y+=inc_dec; if(p<0) p+=two_dx; else { x++; p+=two_dx_dy; } putpixel(x,y,color); } } } /*************************************************************************///- Dashed_line( ) /*************************************************************************///--- ---///*************************************************************************/void

Dashed_line(constint x_1,constint y_1,constint x_2, constint y_2,constint line_type) { int count=0; int color=getcolor( ); int x1=x_1; int y1=y_1; int x2=x_2; int y2=y_2; if(x_1>x_2) { x1=x_2; y1=y_2; x2=x_1; y2=y_1; } int dx=abs(x2-x1); int dy=abs(y2-y1); int inc_dec=((y2>=y1)?1:-1); if(dx>dy)

(37)

{ int two_dy=(2*dy); int two_dy_dx=(2*(dy-dx)); int p=((2*dy)-dx); int x=x1; int y=y1; putpixel(x,y,color); while(x<x2) { x++; if(p<0) p+=two_dy; else { y+=inc_dec; p+=two_dy_dx; }

if((count%2)!=0 && line_type==0) putpixel(x,y,color);

elseif((count%5)!=4 && line_type==1) putpixel(x,y,color);

elseif((count%10)!=8 && (count%10)!=9 && line_type==2) putpixel(x,y,color);

elseif((count%20)!=18 && (count%20)!=19 && line_type==3) putpixel(x,y,color);

elseif((count%12)!=7 && (count%12)!=8 &&

(count%12)!=10 && (count%12)!=11 && line_type==4) putpixel(x,y,color); count++; } } else { int two_dx=(2*dx); int two_dx_dy=(2*(dx-dy)); int p=((2*dx)-dy); int x=x1; int y=y1; putpixel(x,y,color); while(y!=y2) { y+=inc_dec; if(p<0) p+=two_dx; else

(38)

{ x++;

p+=two_dx_dy; }

if((count%2)!=0 && line_type==0) putpixel(x,y,color);

elseif((count%5)!=4 && line_type==1) putpixel(x,y,color);

elseif((count%10)!=8 && (count%10)!=9 && line_type==2) putpixel(x,y,color);

elseif((count%20)!=18 && (count%20)!=19 && line_type==3) putpixel(x,y,color);

elseif((count%12)!=7 && (count%12)!=8 &&

(count%12)!=10 && (count%12)!=11 && line_type==4) putpixel(x,y,color); count++; } } } /*************************************************************************///--- show_screen( ) /*************************************************************************///--- ---///*************************************************************************/void show_screen( ) { setfillstyle(1,1); bar(208,26,430,38); settextstyle(0,0,1); setcolor(15); outtextxy(5,5,"*********************************************************************** *******"); outtextxy(5,17,"*-**************************************************************************-*"); outtextxy(5,29,"*--- ---*"); outtextxy(5,41,"*-**************************************************************************-*"); outtextxy(5,53,"*-**************************************************************************-*"); setcolor(11); outtextxy(218,29,"Reflection Transformation"); setcolor(15);

for(int count=0;count<=30;count++)

outtextxy(5,(65+(count*12)),"*-* *-*"); outtextxy(5,438,"*-**************************************************************************-*"); outtextxy(5,450,"*--- ---*");

(39)

outtextxy(5,462,"********************************************************************* *********");

setcolor(12);

outtextxy(229,450,"Press any Key to exit."); }

(40)

Code for Program to illustrate the implementation of Scaling

Transformation along a Fixed Point in C++ Programming

# include <iostream.h> # include <graphics.h> # include <conio.h> # include <math.h> # define f 0.3 # define projection_angle 45 void show_screen( );

void apply_fixed_point_scaling(int [8][3],constfloat,constfloat, constfloat,constint,constint,constint);

void multiply_matrices(constfloat[4],constfloat[4][4],float[4]); void draw_cube(int [8][3]);

void get_projected_point(int&,int&,int&); void Line(constint,constint,constint,constint);

int main( ) { int driver=VGA; int mode=VGAHI; initgraph(&driver,&mode,"..\\Bgi"); show_screen( ); int cube[8][3]={

{270,200,50}, // front left top {370,200,50}, // front right top {370,300,50}, // front right bottom {270,300,50}, // front left bottom {270,200,-50}, // back left top {370,200,-50}, // back right top {370,300,-50}, // back right bottom {270,300,-50} // back left bottom };

setcolor(15); draw_cube(cube); setcolor(15);

settextstyle(0,0,1);

outtextxy(50,415,"*** Use + & - Keys to apply Fixed-Point Scaling."); int key_code=0; char Key=NULL; do { Key=NULL; key_code=0; Key=getch( );

(41)

key_code=int(Key); if(key_code==0) {

Key=getch( ); key_code=int(Key); } if(key_code==27) break; elseif(key_code==43) { setfillstyle(1,0); bar(40,70,600,410); apply_fixed_point_scaling(cube,1.1,1.1,1.1,320,240,0); setcolor(10); draw_cube(cube); } elseif(key_code==45) { setfillstyle(1,0); bar(40,70,600,410); apply_fixed_point_scaling(cube,0.9,0.9,0.9,320,240,0); setcolor(12); draw_cube(cube); } } while(1); return 0; } /*************************************************************************///--- apply_fixed_point_scaling( ) /*************************************************************************///--- ---///*************************************************************************/void

apply_fixed_point_scaling(int edge_points[8][3],

constfloat Sx,constfloat Sy,constfloat Sz, constint xf,constint xy,constint xz) {

for(int count=0;count<8;count++) {

float matrix_a[4]={edge_points[count][0],edge_points[count][1], edge_points[count][2],1};

float matrix_b[4][4]={ { Sx,0,0,0 } , { 0,Sy,0,0 } , { 0,0,Sz,0 } ,{ ((1-Sx)*xf),

((1-Sy)*xy),((1-Sz)*xz),1 } }; float matrix_c[4]={0};

multiply_matrices(matrix_a,matrix_b,matrix_c); edge_points[count][0]=(int)(matrix_c[0]+0.5); edge_points[count][1]=(int)(matrix_c[1]+0.5); edge_points[count][2]=(int)(matrix_c[2]+0.5); }

(42)

/************************************************************************///--- multiply_matrices( ) /************************************************************************///---

---///************************************************************************/void

multiply_matrices(constfloat matrix_1[4],

constfloat matrix_2[4][4],float matrix_3[4]) {

for(int count_1=0;count_1<4;count_1++) {

for(int count_2=0;count_2<4;count_2++) matrix_3[count_1]+= (matrix_1[count_2]*matrix_2[count_2][count_1]); } } /************************************************************************///-- draw_cube( ) /************************************************************************///--- ---///************************************************************************/void

draw_cube(int edge_points[8][3]) {

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

get_projected_point(edge_points[i][0], edge_points[i][1],edge_points[i][2]); Line(edge_points[0][0],edge_points[0][1], edge_points[1][0],edge_points[1][1]); Line(edge_points[1][0],edge_points[1][1], edge_points[2][0],edge_points[2][1]); Line(edge_points[2][0],edge_points[2][1], edge_points[3][0],edge_points[3][1]); Line(edge_points[3][0],edge_points[3][1], edge_points[0][0],edge_points[0][1]); Line(edge_points[4][0],edge_points[4][1], edge_points[5][0],edge_points[5][1]); Line(edge_points[5][0],edge_points[5][1], edge_points[6][0],edge_points[6][1]); Line(edge_points[6][0],edge_points[6][1], edge_points[7][0],edge_points[7][1]); Line(edge_points[7][0],edge_points[7][1], edge_points[4][0],edge_points[4][1]); Line(edge_points[0][0],edge_points[0][1], edge_points[4][0],edge_points[4][1]); Line(edge_points[1][0],edge_points[1][1], edge_points[5][0],edge_points[5][1]); Line(edge_points[2][0],edge_points[2][1], edge_points[6][0],edge_points[6][1]); Line(edge_points[3][0],edge_points[3][1], edge_points[7][0],edge_points[7][1]); } /************************************************************************///--- get_projected_point( ) /************************************************************************///--- ---///************************************************************************/void

get_projected_point(int& x,int& y,int& z) {

float fcos0=(f*cos(projection_angle*(M_PI/180))); float fsin0=(f*sin(projection_angle*(M_PI/180))); float Par_v[4][4]={

(43)

{1,0,0,0}, {0,1,0,0}, {fcos0,fsin0,0,0}, {0,0,0,1} }; float xy[4]={x,y,z,1}; float new_xy[4]={0}; multiply_matrices(xy,Par_v,new_xy); x=(int)(new_xy[0]+0.5);

y=(int)(new_xy[1]+0.5); z=(int)(new_xy[2]+0.5); }

/*************************************************************************///-- Line( ) /*************************************************************************///---

---///*************************************************************************/void

Line(constint x_1,constint y_1,constint x_2,constint y_2) { int color=getcolor( ); int x1=x_1; int y1=y_1; int x2=x_2; int y2=y_2; if(x_1>x_2) { x1=x_2; y1=y_2; x2=x_1; y2=y_1; } int dx=abs(x2-x1); int dy=abs(y2-y1); int inc_dec=((y2>=y1)?1:-1); if(dx>dy) { int two_dy=(2*dy); int two_dy_dx=(2*(dy-dx)); int p=((2*dy)-dx); int x=x1; int y=y1; putpixel(x,y,color); while(x<x2) { x++; if(p<0) p+=two_dy; else {

(44)

y+=inc_dec; p+=two_dy_dx; } putpixel(x,y,color); } } else { int two_dx=(2*dx); int two_dx_dy=(2*(dx-dy)); int p=((2*dx)-dy); int x=x1; int y=y1; putpixel(x,y,color); while(y!=y2) { y+=inc_dec; if(p<0) p+=two_dx; else { x++; p+=two_dx_dy; } putpixel(x,y,color); } } } /*************************************************************************///--- show_screen( ) /*************************************************************************///--- ---///*************************************************************************/void show_screen( ) { setfillstyle(1,1); bar(170,26,460,38); settextstyle(0,0,1); setcolor(15); outtextxy(5,5,"*********************************************************************** *******"); outtextxy(5,17,"*-**************************************************************************-*"); outtextxy(5,29,"*--- ---*"); outtextxy(5,41,"*-**************************************************************************-*"); outtextxy(5,53,"*-**************************************************************************-*"); setcolor(11);

(45)

setcolor(15);

for(int count=0;count<=30;count++)

outtextxy(5,(65+(count*12)),"*-* *-*"); outtextxy(5,438,"*-**************************************************************************-*"); outtextxy(5,450,"*--- ---*"); outtextxy(5,462,"********************************************************************* *********"); setcolor(12);

outtextxy(229,450,"Press any Key to exit."); }

(46)

Code for Program to illustrate the implementation of Scaling

Transformation in C++ Programming

# include <iostream.h> # include <graphics.h> # include <conio.h> # include <math.h> # define f 0.3 # define projection_angle 45 void show_screen( );

void apply_scaling(int [8][3],constfloat,constfloat,constfloat); void multiply_matrices(constfloat[4],constfloat[4][4],float[4]); void draw_cube(int [8][3]);

void get_projected_point(int&,int&,int&); void Line(constint,constint,constint,constint);

int main( ) { int driver=VGA; int mode=VGAHI; initgraph(&driver,&mode,"..\\Bgi"); show_screen( ); int cube[8][3]={

{270,200,50}, // front left top {370,200,50}, // front right top {370,300,50}, // front right bottom {270,300,50}, // front left bottom {270,200,-50}, // back left top {370,200,-50}, // back right top {370,300,-50}, // back right bottom {270,300,-50} // back left bottom };

setcolor(15); draw_cube(cube); setcolor(15);

settextstyle(0,0,1);

outtextxy(50,415,"*** Use + & - Keys to apply Scaling."); int key_code=0; char Key=NULL; do { Key=NULL; key_code=0; Key=getch( ); key_code=int(Key);

(47)

if(key_code==0) {

Key=getch( ); key_code=int(Key); } if(key_code==27) break; elseif(key_code==43) { setfillstyle(1,0); bar(40,70,600,410); apply_scaling(cube,1.1,1.1,1.1); setcolor(10); draw_cube(cube); } elseif(key_code==45) { setfillstyle(1,0); bar(40,70,600,410); apply_scaling(cube,0.9,0.9,0.9); setcolor(12); draw_cube(cube); } } while(1); return 0; } /*************************************************************************///- apply_scaling( ) /*************************************************************************///--- ---///*************************************************************************/void

apply_scaling(int edge_points[8][3],constfloat Sx, constfloat Sy,constfloat Sz) {

for(int count=0;count<8;count++) {

float matrix_a[4]={edge_points[count][0],edge_points[count][1], edge_points[count][2],1};

float matrix_b[4][4]={ { Sx,0,0,0 } , { 0,Sy,0,0 } , { 0,0,Sz,0 } ,{ 0,0,0,1 } };

float matrix_c[4]={0};

multiply_matrices(matrix_a,matrix_b,matrix_c); edge_points[count][0]=(int)(matrix_c[0]+0.5); edge_points[count][1]=(int)(matrix_c[1]+0.5); edge_points[count][2]=(int)(matrix_c[2]+0.5); }

}

/************************************************************************///--- multiply_matrices( ) /************************************************************************///---

(48)

---///************************************************************************/void multiply_matrices(constfloat matrix_1[4],

constfloat matrix_2[4][4],float matrix_3[4]) {

for(int count_1=0;count_1<4;count_1++) {

for(int count_2=0;count_2<4;count_2++) matrix_3[count_1]+= (matrix_1[count_2]*matrix_2[count_2][count_1]); } } /************************************************************************///-- draw_cube( ) /************************************************************************///--- ---///************************************************************************/void

draw_cube(int edge_points[8][3]) {

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

get_projected_point(edge_points[i][0], edge_points[i][1],edge_points[i][2]); Line(edge_points[0][0],edge_points[0][1], edge_points[1][0],edge_points[1][1]); Line(edge_points[1][0],edge_points[1][1], edge_points[2][0],edge_points[2][1]); Line(edge_points[2][0],edge_points[2][1], edge_points[3][0],edge_points[3][1]); Line(edge_points[3][0],edge_points[3][1], edge_points[0][0],edge_points[0][1]); Line(edge_points[4][0],edge_points[4][1], edge_points[5][0],edge_points[5][1]); Line(edge_points[5][0],edge_points[5][1], edge_points[6][0],edge_points[6][1]); Line(edge_points[6][0],edge_points[6][1], edge_points[7][0],edge_points[7][1]); Line(edge_points[7][0],edge_points[7][1], edge_points[4][0],edge_points[4][1]); Line(edge_points[0][0],edge_points[0][1], edge_points[4][0],edge_points[4][1]); Line(edge_points[1][0],edge_points[1][1], edge_points[5][0],edge_points[5][1]); Line(edge_points[2][0],edge_points[2][1], edge_points[6][0],edge_points[6][1]); Line(edge_points[3][0],edge_points[3][1], edge_points[7][0],edge_points[7][1]); } /************************************************************************///--- get_projected_point( ) /************************************************************************///--- ---///************************************************************************/void

get_projected_point(int& x,int& y,int& z) { float fcos0=(f*cos(projection_angle*(M_PI/180))); float fsin0=(f*sin(projection_angle*(M_PI/180))); float Par_v[4][4]={ {1,0,0,0}, {0,1,0,0}, {fcos0,fsin0,0,0}, {0,0,0,1}

(49)

};

float xy[4]={x,y,z,1}; float new_xy[4]={0};

multiply_matrices(xy,Par_v,new_xy); x=(int)(new_xy[0]+0.5);

y=(int)(new_xy[1]+0.5); z=(int)(new_xy[2]+0.5); }

/*************************************************************************///-- Line( ) /*************************************************************************///---

---///*************************************************************************/void

Line(constint x_1,constint y_1,constint x_2,constint y_2) { int color=getcolor( ); int x1=x_1; int y1=y_1; int x2=x_2; int y2=y_2; if(x_1>x_2) { x1=x_2; y1=y_2; x2=x_1; y2=y_1; } int dx=abs(x2-x1); int dy=abs(y2-y1); int inc_dec=((y2>=y1)?1:-1); if(dx>dy) { int two_dy=(2*dy); int two_dy_dx=(2*(dy-dx)); int p=((2*dy)-dx); int x=x1; int y=y1; putpixel(x,y,color); while(x<x2) { x++; if(p<0) p+=two_dy; else { y+=inc_dec; p+=two_dy_dx; }

(50)

putpixel(x,y,color); } } else { int two_dx=(2*dx); int two_dx_dy=(2*(dx-dy)); int p=((2*dx)-dy); int x=x1; int y=y1; putpixel(x,y,color); while(y!=y2) { y+=inc_dec; if(p<0) p+=two_dx; else { x++; p+=two_dx_dy; } putpixel(x,y,color); } } } /*************************************************************************///--- show_screen( ) /*************************************************************************///--- ---///*************************************************************************/void show_screen( ) { setfillstyle(1,1); bar(218,26,413,38); settextstyle(0,0,1); setcolor(15); outtextxy(5,5,"*********************************************************************** *******"); outtextxy(5,17,"*-**************************************************************************-*"); outtextxy(5,29,"*-- ---*"); outtextxy(5,41,"*-**************************************************************************-*"); outtextxy(5,53,"*-**************************************************************************-*"); setcolor(11); outtextxy(226,29,"Scaling Transformation"); setcolor(15);

(51)

outtextxy(5,(65+(count*12)),"*-* *-*"); outtextxy(5,438,"*-**************************************************************************-*"); outtextxy(5,450,"*--- ---*"); outtextxy(5,462,"********************************************************************* *********"); setcolor(12);

outtextxy(229,450,"Press any Key to exit."); }

(52)

Code for Program to illustrate the implementation of Translation

Transformation in C++ Programming

# include <iostream.h> # include <graphics.h> # include <conio.h> # include <math.h> # define f 0.3 # define projection_angle 45 void show_screen( );

void apply_translation(int[8][3],constint,constint,constint); void multiply_matrices(constfloat[4],constfloat[4][4],float[4]); void draw_cube(int [8][3]);

void get_projected_point(int&,int&,int&); void Line(constint,constint,constint,constint);

int main( ) { int driver=VGA; int mode=VGAHI; initgraph(&driver,&mode,"..\\Bgi"); show_screen( ); int cube[8][3]={

{270,200,50}, // front left top {370,200,50}, // front right top {370,300,50}, // front right bottom {270,300,50}, // front left bottom {270,200,-50}, // back left top {370,200,-50}, // back right top {370,300,-50}, // back right bottom {270,300,-50} // back left bottom };

setcolor(15); draw_cube(cube); setcolor(15);

settextstyle(0,0,1);

outtextxy(50,415,"*** Use Arrows, + & - Keys to apply Translation."); int key_code_1=0; int key_code_2=0; char Key_1=NULL; char Key_2=NULL; do { Key_1=NULL; Key_2=NULL; key_code_1=0;

(53)

key_code_2=0; Key_1=getch( );

key_code_1=int(Key_1); if(key_code_1==0) {

Key_2=getch( );

key_code_2=int(Key_2); } if(key_code_1==27) break; elseif(key_code_1==43) { setfillstyle(1,0); bar(40,70,600,410); apply_translation(cube,0,0,25); setcolor(7); draw_cube(cube); } elseif(key_code_1==45) { setfillstyle(1,0); bar(40,70,600,410); apply_translation(cube,0,0,-25); setcolor(8); draw_cube(cube); } elseif(key_code_1==0) { if(key_code_2==72) { setfillstyle(1,0); bar(40,70,600,410); apply_translation(cube,0,-25,0); setcolor(10); draw_cube(cube); } elseif(key_code_2==75) { setfillstyle(1,0); bar(40,70,600,410); apply_translation(cube,-25,0,0); setcolor(12); draw_cube(cube); } elseif(key_code_2==77) {

References

Related documents

• Set a text rotation angle by dragging the pointing device until the angle between the cursor and the insertion point represents the text rotation angle you

speaking, there isn't a whole lot of difference between D and F, but there are very big differences between A+ and A-. However, generally speaking, if you have a game that falls

Shop.org Show Management will not be responsible for any injury, loss, or damage that may occur to the exhibitor’s employees or property and exhibitors shall indemnify and

Community college students (and college students in general) do not bear the full burden of what it costs to educate them. It cost JJC, the State of Illinois, and local taxpayers

Our Last Theorem in Absolute Geometry: If two lines in the same plane are cut by a transversal so that a pair of alternate interior angles are congruent, the lines are

➢ Click on reshape feature draw a line where you want to split and right click to complete line Note: Drawing line in reshape feature line first point and last point

If you receive this error, please check that the start date entered is within the period of at least one of your professional jobs. If it does, your details may not have been

There are various algorithm which can be used for plotting such as Line drawing algorithm, Digital Differential Analyzer Line algorithm (DDA), Midpoint Circle