You are on page 1of 117

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);

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;
}

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;

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();
}

# 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);

return;
}

/*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
{

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)
{

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);
}
}
}

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;
}

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);
cprintf(" -----------------------*");

cprintf("**********************************************");
cprintf("*****************************************************************************-*");
for(int count=0;count<42;count++)
cprintf("*-*
*-*");
gotoxy(1,46);
cprintf("*****************************************************************************-*");
cprintf("*-----------------------------------------------------------------------------*");
cprintf("*****************************************************************************
***");
gotoxy(1,2);
}

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;
}

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);
cprintf(" ------------------------*");

cprintf("*-*********************************************-*");
cprintf("*****************************************************************************-*");
for(int count=0;count<42;count++)
cprintf("*-*
*-*");
gotoxy(1,46);
cprintf("*****************************************************************************-*");
cprintf("*-----------------------------------------------------------------------------*");
cprintf("*****************************************************************************
***");
gotoxy(1,2);
}

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);

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]+=
(matrix_1[count_2]*matrix_2[count_2][count_1]);

}
}
/*************************************************************************///---------------------------- 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)

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,"***************************************************************************-*");

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.");
}

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);

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( ) ----------------------------

///*************************************************************************/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;

}
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);

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.");
}

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

f
projection_angle

0.3
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},
{360,130,50},
{360,130,-50},
{280,130,-50},
{320,20,0}
};

// base
// base
// base
// base
// top

front left
front right
back right
back left

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);

}
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);
draw_pyramid(edge_points);

}
/************************************************************************///--------------------- 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)));

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

{
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);
outtextxy(218,29,"3D Rotation along X-axis");

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.");
}

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
void
void
void
void
void

apply_reflection_about_line_yex(constint,int []);
apply_reflection_about_line_yemx(constint,int []);
apply_reflection_along_x_axis(constint,int []);
apply_reflection_along_y_axis(constint,int []);
apply_rotation(constint,int [],float);
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 };

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};
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);
}
}
/*************************************************************************///----------------- 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[])
{

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;
}

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)

{
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

{
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,"*------------------------------------------------*");

outtextxy(5,462,"*********************************************************************
*********");
setcolor(12);
outtextxy(229,450,"Press any Key to exit.");
}

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
# define

f
projection_angle

0.3
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},
{370,200,50},
{370,300,50},
{270,300,50},
{270,200,-50},
{370,200,-50},
{370,300,-50},
{270,300,-50}
};

//
//
//
//
//
//
//
//

front left top


front right top
front right bottom
front left bottom
back left top
back right top
back right bottom
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( );

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);
}
}

/************************************************************************///--------------------- 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]={

{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
{

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);
outtextxy(178,29,"Fixed-Point Scaling Transformation");

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.");
}

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
# define

f
projection_angle

0.3
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},
{370,200,50},
{370,300,50},
{270,300,50},
{270,200,-50},
{370,200,-50},
{370,300,-50},
{270,300,-50}
};

//
//
//
//
//
//
//
//

front left top


front right top
front right bottom
front left bottom
back left top
back right top
back right bottom
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);

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( ) ------------------------

///************************************************************************/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}

};
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;
}

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);
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.");
}

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
# define

f
projection_angle

0.3
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},
{370,200,50},
{370,300,50},
{270,300,50},
{270,200,-50},
{370,200,-50},
{370,300,-50},
{270,300,-50}
};

//
//
//
//
//
//
//
//

front left top


front right top
front right bottom
front left bottom
back left top
back right top
back right bottom
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;

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)
{

setfillstyle(1,0);
bar(40,70,600,410);
apply_translation(cube,25,0,0);
setcolor(14);
draw_cube(cube);
}
elseif(key_code_2==80)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_translation(cube,0,25,0);
setcolor(9);
draw_cube(cube);
}
}
}
while(1);
return 0;
}
/*************************************************************************///----------------------- apply_translation( ) ----------------------///*************************************************************************/void
apply_translation(int edge_points[8][3],
constint Tx,constint Ty,constint Tz)
{
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]={ { 1,0,0,0 } , { 0,1,0,0 } ,
{ 0,0,1,0 } ,{ Tx,Ty,Tz,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( ) -----------------------///************************************************************************/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}
};
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;
}
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(205,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(210,29,"Translation Transformation");
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.");
}

Code for Program to illustrate the implementation of X-Direction Shear


Transformation in C++ Programming
#
#
#
#

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

void show_screen( );
void apply_x_direction_shear(constint,int [],constfloat);
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[10]={ 270,340, 270,140, 370,140, 370,340, 270,340 };
setcolor(15);
Polygon(5,polygon_points);
setcolor(15);
settextstyle(0,0,1);
outtextxy(50,415,"*** Use Left and Right Arrow Keys to apply X-Direction
Shear.");
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;
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==0)

{
if(key_code_2==75)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_x_direction_shear(5,polygon_points,-0.1);
setcolor(12);
Polygon(5,polygon_points);
}
elseif(key_code_2==77)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_x_direction_shear(5,polygon_points,0.1);
setcolor(10);
Polygon(5,polygon_points);
}
}
}
while(1);
return 0;
}
/*************************************************************************///-------------------- apply_x_direction_shear( ) -------------------///*************************************************************************/void
apply_x_direction_shear(constint n,int coordinates[],constfloat Sh_x)
{
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} , {Sh_x,1,0} ,{ 0,0,1} };
float matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count*2)]=(matrix_c[0]+0.5);
coordinates[((count*2)+1)]=(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( ) ---------------------------///*************************************************************************/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;
}
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(184,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);

outtextxy(194,29,"X-Direction Shear Transformation");


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.");
}

Code for Program to illustrate the implementation of Y-Direction Shear


Transformation in C++ Programming
#
#
#
#

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

void show_screen( );
void apply_x_direction_shear(constint,int [],constfloat);
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[10]={ 220,190, 420,190, 420,290, 220,290, 220,190 };
setcolor(15);
Polygon(5,polygon_points);
setcolor(15);
settextstyle(0,0,1);
outtextxy(50,415,"*** Use Up and Down Arrow Keys to apply Y-Direction Shear.");
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;
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==0)
{

if(key_code_2==72)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_x_direction_shear(5,polygon_points,-0.1);
setcolor(12);
Polygon(5,polygon_points);
}
elseif(key_code_2==80)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_x_direction_shear(5,polygon_points,0.1);
setcolor(10);
Polygon(5,polygon_points);
}
}
}
while(1);
return 0;
}
/*************************************************************************///-------------------- apply_x_direction_shear( ) -------------------///*************************************************************************/void
apply_x_direction_shear(constint n,int coordinates[],constfloat Sh_y)
{
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,Sh_y,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]+0.5);
coordinates[((count*2)+1)]=(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( ) ---------------------------///*************************************************************************/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;
}
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(184,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);
outtextxy(194,29,"Y-Direction Shear Transformation");

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.");
}

Code for Program to show the implementation of Cohen-Sutherland Line


Clipping Algorithm in C++ Programming
#
#
#
#

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

/*************************************************************************///-------------------------- LineCoordinates ------------------------///*************************************************************************/class


LineCoordinates
{
public:
float x_1;
float y_1;
float x_2;
float y_2;
LineCoordinates(constfloat x1,constfloat y1,
constfloat x2,constfloat y2)
{
x_1=x1;
y_1=y1;
x_2=x2;
y_2=y2;
}
};
/*************************************************************************///------------------------ WindowCoordinates ------------------------///*************************************************************************/class
WindowCoordinates
{
public:
float x_min;
float y_min;
float x_max;
float y_max;
WindowCoordinates(constfloat x1,constfloat y1,
constfloat x2,constfloat y2)
{
x_min=x1;
y_min=y1;
x_max=x2;
y_max=y2;
}
};
/*************************************************************************///---------------------------- RegionCode ---------------------------///*************************************************************************/class
RegionCode
{
public:
int bit_1;
int bit_2;
int bit_3;
int bit_4;
RegionCode( )

{
bit_1=0;
bit_2=0;
bit_3=0;
bit_4=0;
}
constint equal_zero( )
{
if(bit_1==0 && bit_2==0 && bit_3==0 && bit_4==0)
return 1;
return 0;
}
void get_logical_AND(RegionCode rc1,RegionCode rc2)
{
if(rc1.bit_1==1 && rc2.bit_1==1)
bit_1=1;
if(rc1.bit_2==1 && rc2.bit_2==1)
bit_2=1;
if(rc1.bit_3==1 && rc2.bit_3==1)
bit_3=1;
if(rc1.bit_4==1 && rc2.bit_4==1)
bit_4=1;
}
void get_region_code(const WindowCoordinates wc,
constint x,constint y)
{
if((wc.x_min-x)>0)
bit_1=1;
if((x-wc.x_max)>0)
bit_2=1;
if((wc.y_min-y)>0)
bit_3=1;
if((y-wc.y_max)>0)
bit_4=1;
}
};
void show_screen( );
constint clip_line(const WindowCoordinates,LineCoordinates&);
void calculate_intersecting_points(const WindowCoordinates,LineCoordinates&);
void Rectangle(constint,constint,constint,constint);
void Line(constint,constint,constint,constint);
int main( )
{
int driver=VGA;
int mode=VGAHI;

initgraph(&driver,&mode,"..\\Bgi");
show_screen( );
WindowCoordinates WC(180,140,470,340);
setcolor(15);
Rectangle(WC.x_min,WC.y_min,WC.x_max,WC.y_max);
LineCoordinates
LineCoordinates
LineCoordinates
LineCoordinates
LineCoordinates
LineCoordinates

LC_1(150,160,120,320);
LC_2(250,150,200,200);
LC_3(160,200,490,260);
LC_4(300,300,400,380);
LC_5(550,300,450,400);
LC_6(440,110,400,370);

setcolor(7);
Line(LC_1.x_1,LC_1.y_1,LC_1.x_2,LC_1.y_2);
Line(LC_2.x_1,LC_2.y_1,LC_2.x_2,LC_2.y_2);
Line(LC_3.x_1,LC_3.y_1,LC_3.x_2,LC_3.y_2);
Line(LC_4.x_1,LC_4.y_1,LC_4.x_2,LC_4.y_2);
Line(LC_5.x_1,LC_5.y_1,LC_5.x_2,LC_5.y_2);
Line(LC_6.x_1,LC_6.y_1,LC_6.x_2,LC_6.y_2);
char Key=NULL;
do
{
Key=getch( );
}
while(Key!='C' && Key!='c');
settextstyle(0,0,1);
setcolor(0);
outtextxy(163,450," Press 'C' to see the Clipped Lines.
setcolor(15);
outtextxy(165,450,"-----setcolor(12);
outtextxy(213,450,"

Press any Key to exit.

setcolor(10);
if(clip_line(WC,LC_1))
Line(LC_1.x_1,LC_1.y_1,LC_1.x_2,LC_1.y_2);
if(clip_line(WC,LC_2))
Line(LC_2.x_1,LC_2.y_1,LC_2.x_2,LC_2.y_2);
if(clip_line(WC,LC_3))
Line(LC_3.x_1,LC_3.y_1,LC_3.x_2,LC_3.y_2);
if(clip_line(WC,LC_4))
Line(LC_4.x_1,LC_4.y_1,LC_4.x_2,LC_4.y_2);
if(clip_line(WC,LC_5))
Line(LC_5.x_1,LC_5.y_1,LC_5.x_2,LC_5.y_2);
if(clip_line(WC,LC_6))
Line(LC_6.x_1,LC_6.y_1,LC_6.x_2,LC_6.y_2);

");

-------");
");

getch( );
return 0;
}
/*************************************************************************///-------------------------- clip_line( ) ---------------------------///*************************************************************************/constint
clip_line(const WindowCoordinates wc,LineCoordinates &lc)
{
RegionCode rc1,rc2,rc;
rc1.get_region_code(wc,lc.x_1,lc.y_1);
rc2.get_region_code(wc,lc.x_2,lc.y_2);
rc.get_logical_AND(rc1,rc2);
if(rc1.equal_zero( ) && rc2.equal_zero( ))
{
lc.x_1=(int)(lc.x_1+0.5);
lc.y_1=(int)(lc.y_1+0.5);
lc.x_2=(int)(lc.x_2+0.5);
lc.y_2=(int)(lc.y_2+0.5);
return 1;
}
elseif(!rc.equal_zero( ))
return 0;
else
{
calculate_intersecting_points(wc,lc);
clip_line(wc,lc);
}
return 1;
}
/*************************************************************************///---------------- calculate_intersecting_points( ) -----------------///*************************************************************************/void
calculate_intersecting_points(const WindowCoordinates wc,
LineCoordinates &lc)
{
RegionCode rc1,rc2,rc;
rc1.get_region_code(wc,lc.x_1,lc.y_1);
rc2.get_region_code(wc,lc.x_2,lc.y_2);
if(!rc1.equal_zero( ))
{
float m;
float x=lc.x_1;
float y=lc.y_1;
int dx=(lc.x_2-lc.x_1);
if(dx!=0)
m=((lc.y_2-lc.y_1)/(lc.x_2-lc.x_1));
if(rc1.bit_1==1)
{

x=wc.x_min;
y=(lc.y_1+(m*(x-lc.x_1)));
}
elseif(rc1.bit_2==1)
{
x=wc.x_max;
y=(lc.y_1+(m*(x-lc.x_1)));
}
elseif(rc1.bit_3==1)
{
y=wc.y_min;
if(dx!=0)
x=(lc.x_1+((y-lc.y_1)/m));
}
elseif(rc1.bit_4==1)
{
y=wc.y_max;
if(dx!=0)
x=(lc.x_1+((y-lc.y_1)/m));
}
lc.x_1=x;
lc.y_1=y;
}
if(!rc2.equal_zero( ))
{
float m;
float x=lc.x_2;
float y=lc.y_2;
int dx=(lc.x_2-lc.x_1);
if(dx!=0)
m=((lc.y_2-lc.y_1)/(lc.x_2-lc.x_1));
if(rc2.bit_1==1)
{
x=wc.x_min;
y=(lc.y_2+(m*(x-lc.x_2)));
}
elseif(rc2.bit_2==1)
{
x=wc.x_max;
y=(lc.y_2+(m*(x-lc.x_2)));
}
elseif(rc2.bit_3==1)
{
y=wc.y_min;
if(dx!=0)
x=(lc.x_2+((y-lc.y_2)/m));
}
elseif(rc2.bit_4==1)

{
y=wc.y_max;
if(dx!=0)
x=(lc.x_2+((wc.y_max-lc.y_2)/m));
}
lc.x_2=x;
lc.y_2=y;
}
}
/*************************************************************************///-------------------------- Rectangle( ) ---------------------------///*************************************************************************/void
Rectangle(constint x_1,constint y_1,constint x_2,constint y_2)
{
Line(x_1,y_1,x_2,y_1);
Line(x_2,y_1,x_2,y_2);
Line(x_2,y_2,x_1,y_2);
Line(x_1,y_2,x_1,y_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;
}
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(145,26,480,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(152,29,"Cohen-Sutherland Line Clipping Algorithm");
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(163,450,"
}

Press 'C' to see the Clipped Lines.

");

Code for Program of cohen sutherland Line clipping Algorithm in C++


Programming
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
int pixels[2][4];
float xn1,xn2,yn1,yn2,x3,y3,m;
void show_quadrant()
{
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(int i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
}
void su_co(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)
{
int i,j,fl;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
pixels[i][j]=0;
if(y1>ymax)
pixels[0][0]=1;
if(y1<ymin)
pixels[0][1]=1;
if(x1>xmax)
pixels[0][2]=1;
if(x1<xmin)
pixels[0][3]=1;
if(y2>ymax)
pixels[1][0]=1;
if(y2<ymin)
pixels[1][1]=1;
if(x2>xmax)
pixels[1][2]=1;
if(x2<xmin)
pixels[1][3]=1;

for(j=0;j<4;j++)
{
if(pixels[0][j]==0&&pixels[1][j]==0)
continue;
if(pixels[0][j]==1&&pixels[1][j]==1)
{
fl=3;
break;
}
fl=2;
}
switch(fl)
{
case 1:
line(320+x1,240-y1,320+x2,240-y2);
break;
case 3:
cout<<"\n\n\a\" Line Is Not Visible...:-(";
break;
case 2:
m=(y2-y1)/(x2-x1);
xn1=x1;
yn1=y1;
xn2=x2;
yn2=y2;
if(pixels[0][0]==1)
{
xn1=x1+(ymax-y1)/m;
yn1=ymax;
}
if(pixels[0][1]==1)
{
xn1=x1+(ymin-y1)/m;
yn1=ymin;
}
if(pixels[0][2]==1)
{
yn1=y1+(xmax-x1)*m;
xn1=xmax;
}
if(pixels[0][3]==1)
{
yn1=y1+(xmin-x1)*m;
xn1=xmin;
}
if(pixels[1][0]==1)
{
xn2=x2+(ymax-y2)/m;
yn2=ymax;
}
if(pixels[1][1]==1)
{
xn2=x2+(ymin-y2)/m;
yn2=ymin;
}
if(pixels[1][2]==1)
{
yn2=y2+(xmax-x2)*m;
xn2=xmax;

}
if(pixels[1][3]==1)
{
yn2=y2+(xmin-x2)*m;
xn2=xmin;
}
line(320+xn1,240-yn1,320+xn2,240-yn2);
break;
}
}
void midpt(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)
{
int fl=1;
int i,j;
int ox1=x1,ox2=x2,oy1=y1,oy2=y2;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
pixels[i][j]=0;
if(y1>ymax)
pixels[0][0]=1;
if(y1<ymin)
pixels[0][1]=1;
if(x1>xmax)
pixels[0][2]=1;
if(x1<xmin)
pixels[0][3]=1;
if(y2>ymax)
pixels[1][0]=1;
if(y2<ymin)
pixels[1][1]=1;
if(x2>xmax)
pixels[1][2]=1;
if(x2<xmin)
pixels[1][3]=1;
for(j=0;j<4;j++)
{
if(pixels[0][j]==0&&pixels[1][j]==0)
continue;
if(pixels[0][j]==1&&pixels[1][j]==1)
{
fl=3;
break;
}
fl=2;
}
switch(fl)
{
case 1:
line(320+x1,240-y1,320+x2,240-y2);
break;
case 3:
cout<<"\n\n\a\" Line Is Not Visible...:-(";
break;
case 2:
xn1=x1;

yn1=y1;
xn2=x2;
yn2=y2;
fl=0;
x3=x1;
y3=y1;
while(1)
{
if(!(y1>ymax || y1<ymin || x1>xmax || x1<xmin) && (x3 || y3)!=0.1)
break;
x3=(x1+x2)/2;
y3=(y1+y2)/2;
if(!(y3>ymax || y3<ymin || x3>xmax || x3<xmin))
fl=1;
else
fl=0;
if(fl)
{
x2=x3;
y2=y3;
}
else
{
x1=x3;
y1=y3;
}
}
xn1=x3;
yn1=y3;
fl=0;
x1=ox1;
x2=ox2;
y1=oy1;
y2=oy2;
x3=x2;
y3=y2;
while(1)
{
if(!(y2>ymax || y2<ymin || x2>xmax || x2<xmin) && (x3 || y3)!=0.1)
break;
x3=(x1+x2)/2;
y3=(y1+y2)/2;
if(!(y3>ymax || y3<ymin || x3>xmax || x3<xmin))
fl=1;
else
fl=0;
if(fl)
{
x1=x3;
y1=y3;
}
else
{
x2=x3;
y2=y3;
}
}
xn2=x3;
yn2=y3;
line(320+xn1,240-yn1,320+xn2,240-yn2);

break;
}
}
void show_message()
{
char *mess[]={"-","=","["," ","L","i","n","e"," ","C","l","i",
"p","p","i","n","g"," ","]","=","-",};
int xx=29,xxx=50,i,j;
_setcursortype(_NOCURSOR);
for(i=0,j=21;i<13,j>=11;i++,j--)
{
gotoxy(xx,1);
cout<<mess[i];
xx++;
gotoxy(xxx,1);
cout<<mess[j];
xxx--;
delay(50);
}
_setcursortype(_NORMALCURSOR);
}
void main()
{
clrscr();
int gd=DETECT,gm,i,j;
int xmin,ymin,xmax,ymax,x1,y1,x2,y2;
int choice,ed[20],num;
show_message();
cout<<"\n\n\t\t\" Enter The Co-Ordinates Of The Clipping Window.\"";
cout<<"\n\n\t\t\" Enter X(min) & Y(min) \":=";
cin>>xmin>>ymin;
cout<<"\n\t\t\" Enter X(max) & Y(max) \":=";
cin>>xmax>>ymax;
cout<<"\n\t\t\" Enter The Co-Ordinates Of The Line.\"";
cout<<"\n\n\t\t\" Enter X(1) & Y(1) \":=";
cin>>x1>>y1;
cout<<"\n\t\t\" Enter X(2) & Y(2) \":=";
cin>>x2>>y2;
clrscr();
show_message();
cout<<"\n\n\n\t\t1:==>\" Sutherland-Cohen \"";
cout<<"\n\n\t\t2:==>\" Mid-Point Method \"";
cout<<"\n\n\t\t3:==>\" Exit \"";
cout<<"\n\n\t\t\" Enter Your Choice \":=";
cin>>choice;
switch(choice)
{
case 1:
initgraph(&gd,&gm,"..\\bgi");
clearviewport();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
line (320+x1,240-y1,320+x2,240-y2);
getch();
cleardevice();

show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
su_co(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
break;
case 2:
initgraph(&gd,&gm,"..\\bgi");
clearviewport();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
line (320+x1,240-y1,320+x2,240-y2);
getch();
cleardevice();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
midpt(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
break;
case 3:
exit(0);
default:
cout<<"\n\t\a\" Press A Valid Key...!!! \"";
getch();
main();
break;
}
closegraph();
}

Code for Program to fill a Polygon using Scan Line Polygon Fill
Algorithm in C++ Programming
#
#
#
#

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

/*************************************************************************///-------------------------------- Edge -----------------------------///*************************************************************************/class


Edge
{
public:
int yUpper;
float xIntersect;
float dxPerScan;
Edge *next;
};
/*************************************************************************///------------------------- PointCoordinates ------------------------///*************************************************************************/class
PointCoordinates
{
public:
float x;
float y;
PointCoordinates( )
{
x=0;
y=0;
}
};
/*************************************************************************///-------------------------- LineCoordinates ------------------------///*************************************************************************/class
LineCoordinates
{
public:
float x_1;
float y_1;
float x_2;
float y_2;
LineCoordinates( )
{
x_1=0;
y_1=0;
x_2=0;
y_2=0;
}
LineCoordinates(constfloat x1,constfloat y1,
constfloat x2,constfloat y2)
{
x_1=x1;

y_1=y1;
x_2=x2;
y_2=y2;
}
};
/*************************************************************************//**********
***************************************************************///---------------------- Function Prototypes ------------------------///*************************************************************************//********
*****************************************************************/void show_screen( );
void Fill_polygon(constint,constint [],constint);
void insertEdge(Edge *,Edge *);
void makeEdgeRec(const PointCoordinates,const PointCoordinates,
constint,Edge *,Edge *[]);
void buildEdgeList(constint,const PointCoordinates [],Edge *[]);
void buildActiveList(constint,Edge *,Edge *[]);
void fillScan(constint,const Edge *,constint);
void deleteAfter(Edge []);
void updateActiveList(constint,Edge []);
void resortActiveList(Edge []);
constint yNext(constint,constint,const PointCoordinates []);
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 n=10;
int polygon_points[20]={ 220,340 , 220,220 , 250,170 , 270,200 ,
300,140 , 320,240 , 320,290 , 420,220 ,
420,340 , 220,340 };
setcolor(15);
Polygon(10,polygon_points);
Fill_polygon(n,polygon_points,9);
getch( );
return 0;
}
/*************************************************************************///--------------------------- Fill_polygon( ) -----------------------///*************************************************************************/void
Fill_polygon(constint n,constint ppts[],constint fill_color)
{
Edge *edges[480];
Edge *active;

PointCoordinates *pts=new PointCoordinates[n];


for(int count_1=0;count_1<n;count_1++)
{
pts[count_1].x=(ppts[(count_1*2)]);
pts[count_1].y=(ppts[((count_1*2)+1)]);
}
for(int count_2=0;count_2<640;count_2++)
{
edges[count_2]=new Edge;
edges[count_2]->next=NULL;
}
buildEdgeList(n,pts,edges);
active=new Edge;
active->next=NULL;
for(int count_3=0;count_3<480;count_3++)
{
buildActiveList(count_3,active,edges);
if(active->next)
{
fillScan(count_3,active,fill_color);
updateActiveList(count_3,active);
resortActiveList(active);
}
}
Polygon(n,ppts);
delete pts;
}
/*************************************************************************///------------------------------ yNext( ) ---------------------------///*************************************************************************/constint
yNext(constint k,constint cnt,const PointCoordinates pts[])
{
int j;
if((k+1)>(cnt-1))
j=0;
else
j=(k+1);
while(pts[k].y==pts[j].y)
{
if((j+1)>(cnt-1))
j=0;
else
j++;
}
return (pts[j].y);
}

/*************************************************************************///---------------------------- insertEdge( ) ------------------------///*************************************************************************/void


insertEdge(Edge *list,Edge *edge)
{
Edge *p;
Edge *q=list;
p=q->next;
while(p!=NULL)
{
if(edge->xIntersect<p->xIntersect)
p=NULL;
else
{
q=p;
p=p->next;
}
}
edge->next=q->next;
q->next=edge;
}
/*************************************************************************///-------------------------- makeEdgeRec( ) -------------------------///*************************************************************************/void
makeEdgeRec(const PointCoordinates lower,const PointCoordinates upper,
constint yComp,Edge *edge,Edge *edges[])
{
edge->dxPerScan=((upper.x-lower.x)/(upper.y-lower.y));
edge->xIntersect=lower.x;
if(upper.y<yComp)
edge->yUpper=(upper.y-1);
else
edge->yUpper=upper.y;
insertEdge(edges[lower.y],edge);
}
/*************************************************************************///-------------------------- buildEdgeList( ) -----------------------///*************************************************************************/void
buildEdgeList(constint cnt,const PointCoordinates pts[],Edge *edges[])
{
Edge *edge;
PointCoordinates v1;
PointCoordinates v2;
int yPrev=(pts[cnt-2].y);
v1.x=pts[cnt-1].x;
v1.y=pts[cnt-1].y;
for(int count=0;count<cnt;count++)
{
v2=pts[count];

if(v1.y!=v2.y)
{
edge=new Edge;
if(v1.y<v2.y)
makeEdgeRec(v1,v2,yNext(count,cnt,pts),edge,edges);
else
makeEdgeRec(v2,v1,yPrev,edge,edges);
}
yPrev=v1.y;
v1=v2;
}
}
/*************************************************************************///----------------------- buildActiveList( ) ------------------------///*************************************************************************/void
buildActiveList(constint scan,Edge *active,Edge *edges[])
{
Edge *p;
Edge *q;
p=edges[scan]->next;
while(p)
{
q=p->next;
insertEdge(active,p);
p=q;
}
}
/*************************************************************************///--------------------------- fillScan( ) ---------------------------///*************************************************************************/void
fillScan(constint scan,const Edge *active,constint fill_color)
{
Edge *p1;
Edge *p2;
p1=active->next;
while(p1)
{
p2=p1->next;
for(int count=p1->xIntersect;count<=p2->xIntersect;count++)
putpixel(count,scan,fill_color);
p1=p2->next;
}
}
/*************************************************************************///------------------------ deleteAfter( ) ---------------------------///*************************************************************************/void
deleteAfter(Edge * q)
{

Edge *p=q->next;
q->next=p->next;
delete p;
}
/*************************************************************************///------------------------ updateActiveList( ) ----------------------///*************************************************************************/void
updateActiveList(constint scan,Edge *active)
{
Edge *q=active;
Edge *p=active->next;
while(p)
{
if(scan>=p->yUpper)
{
p=p->next;
deleteAfter(q);
}
else
{
p->xIntersect=(p->xIntersect+p->dxPerScan);
q=p;
p=p->next;
}
}
}
/*************************************************************************///------------------------ resortActiveList( ) ----------------------///*************************************************************************/void
resortActiveList(Edge *active)
{
Edge *q;
Edge *p=active->next;
active->next=NULL;
while(p)
{
q=p->next;
insertEdge(active,p);
p=q;
}
}
/*************************************************************************///---------------------------- 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)
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(178,26,450,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(185,29,"Scan Line Polygon Fill Algorithm");
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.");
}

Code for Program to draw a 3D Bezier Curve of nth degree in C++


Programming
#
#
#
#

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

# define
# define

f
projection_angle

0.3
45

void show_screen( );
void Bezier_curve(constint,constint [10][3]);
double nCr(int,int);
double factorial(int);
void get_projected_point(double&,double&,double&);
void multiply_matrices(constfloat[4],constfloat[4][4],float[4]);
void Dashed_line(constint,constint,constint,constint,constint=0);
int main( )
{
int driver=VGA;
int mode=VGAHI;
int n;
do
{
show_screen( );
gotoxy(8,10);
cout<<"Degree of Bezier Curve : n :";
gotoxy(8,11);
cout<<"";
gotoxy(12,13);
cout<<"Enter the value of n (1<n<10) = ";
cin>>n;
if(n>=10)
n=10;
int control_points[10][3]={0};
for(int count=0;count<=n;count++)
{
gotoxy(8,18);
cout<<"Coordinates of Point-"<<count<<"
(x"<<count<<",y"<<count<<",z"<<count<<") :";
gotoxy(8,19);
cout<<"";
gotoxy(12,21);
cout<<"Enter the value of x"<<count<<" = ";
cin>>control_points[count][0];

gotoxy(12,23);
cout<<"Enter the value of y"<<count<<" = ";
cin>>control_points[count][1];
gotoxy(12,25);
cout<<"Enter the value of z"<<count<<" = ";
cin>>control_points[count][2];
gotoxy(8,18);
cout<<"

";

gotoxy(12,21);
cout<<"

";

gotoxy(12,23);
cout<<"

";

gotoxy(12,25);
cout<<"

";

}
initgraph(&driver,&mode,"..\\Bgi");
setcolor(15);
Bezier_curve(n,control_points);
setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any other key to exit.");
int key=int(getch( ));
if(key!=13)
break;
}
while(1);
return 0;
}
/*************************************************************************///------------------------- Bezier_curve( ) -------------------------///*************************************************************************/void
Bezier_curve(constint n,constint cp[10][3])
{
int color=getcolor( );
setcolor(7);
double
double
double
double
double
double

x_1;
y_1;
z_1;
x_2;
y_2;
z_2;

for(int count=0;count<n;count++)
{
x_1=cp[count][0];
y_1=cp[count][1];
z_1=cp[count][2];

x_2=cp[(count+1)][0];
y_2=cp[(count+1)][1];
z_2=cp[(count+1)][2];
get_projected_point(x_1,y_1,z_1);
get_projected_point(x_2,y_2,z_2);
Dashed_line((int)(x_1+0.5),(int)(y_1+0.5),
(int)(x_2+0.5),(int)(y_2+0.5));
}
double x;
double y;
double z;
for(float u=0.0005;u<=1;u+=0.0005)
{
x=0;
y=0;
z=0;
for(int k=0;k<=n;k++)
{
x+=(cp[k][0]*nCr(n,k)*pow(u,k)*powl((1-u),(n-k)));
y+=(cp[k][1]*nCr(n,k)*pow(u,k)*powl((1-u),(n-k)));
z+=(cp[k][2]*nCr(n,k)*pow(u,k)*powl((1-u),(n-k)));
}
get_projected_point(x,y,z);
putpixel((int)(x+0.5),(int)(y+0.5),color);
}
}
/*************************************************************************///----------------------------- nCr( ) ------------------------------///*************************************************************************/double
nCr(int n,int r)
{
double nf;
double rf;
double nrf;
double ncr;
nf=factorial(n);
rf=factorial(r);
nrf=factorial((n-r));
ncr=(nf/(rf*nrf));
return ncr;
}
/*************************************************************************///-------------------------- factorial( ) ---------------------------///*************************************************************************/double
factorial(int number)
{
double factorial=1;
if(number==0 || number==1);

else
{
for(int count=1;count<=number;count++)
factorial=factorial*count;
}
return factorial;
}
/************************************************************************///-------------------- get_projected_point( ) ----------------------///************************************************************************/void
get_projected_point(double& x,double& y,double& 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}
};
float xy[4]={x,y,z,1};
float new_xy[4]={0};
multiply_matrices(xy,Par_v,new_xy);
x=new_xy[0];
y=new_xy[1];
z=new_xy[2];
}
/************************************************************************///--------------------- 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]);
}
}
/*************************************************************************///-------------------------- 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)
{
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
{
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( )
{
restorecrtmode( );
clrscr( );
textmode(C4350);
cprintf("\n***************************************************************************
*****");
cprintf("*-**********************************************************-*");
cprintf("*------------------------------- ");
textbackground(1);
cprintf(" Bezier Curve ");
textbackground(8);
cprintf(" -------------------------------*");

cprintf("*-**********************************************************-*");
cprintf("*****************************************************************************-*");
for(int count=0;count<42;count++)
cprintf("*-*
*-*");
gotoxy(1,46);
cprintf("*****************************************************************************-*");
cprintf("*-----------------------------------------------------------------------------*");
cprintf("*****************************************************************************
***");
gotoxy(1,2);
}

Code for Program to draw a 3D Piece-Wise Bezier Curve of nth degree


with Zeroth Order Continuity in C++ Programming
#
#
#
#

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

# define
# define

f
projection_angle

0.3
45

void show_screen( );
void Bezier_curve(constint,constint [4][3]);
void Piece_wise_bezier_curve(constint,constint [25][3]);
double nCr(int,int);
double factorial(int);
void get_projected_point(double&,double&,double&);
void multiply_matrices(constfloat[4],constfloat[4][4],float[4]);
void Dashed_line(constint,constint,constint,constint,constint=0);
int main( )
{
int driver=VGA;
int mode=VGAHI;
int n;
do
{
show_screen( );
gotoxy(8,10);
cout<<"Number of Control Points : n :";
gotoxy(8,11);
cout<<"";
gotoxy(12,13);
cout<<"Enter the value of n (1<n<=25) = ";
cin>>n;
if(n>=10)
n=10;
int control_points[25][3]={0};
for(int count=0;count<n;count++)
{
gotoxy(8,16);
cout<<"Coordinates of Point-"<<count<<"
(x"<<count<<",y"<<count<<",z"<<count<<") :";
gotoxy(8,17);
cout<<"";
gotoxy(12,19);
cout<<"Enter the value of x"<<count<<" = ";

cin>>control_points[count][0];
gotoxy(12,21);
cout<<"Enter the value of y"<<count<<" = ";
cin>>control_points[count][1];
gotoxy(12,23);
cout<<"Enter the value of z"<<count<<" = ";
cin>>control_points[count][2];
gotoxy(8,16);
cout<<"

";

gotoxy(12,19);
cout<<"

";

gotoxy(12,21);
cout<<"

";

gotoxy(12,23);
cout<<"

";

}
initgraph(&driver,&mode,"..\\Bgi");
setcolor(15);
Piece_wise_bezier_curve(n,control_points);
setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any other key to exit.");
int key=int(getch( ));
if(key!=13)
break;
}
while(1);
return 0;
}
/*************************************************************************///----------------------- Piece_wise_bezier_curve( ) -------------------------///*************************************************************************/void
Piece_wise_bezier_curve(constint n,constint cpts[25][3])
{
int pieces=(n/4);
int extra_points=(n%4);
int point=1;
int cp[4][3]={0};
for(int count_1=0;count_1<pieces;count_1++)
{
point--;
for(int count_2=0;count_2<4;count_2++)
{
cp[count_2][0]=cpts[point][0];
cp[count_2][1]=cpts[point][1];
cp[count_2][2]=cpts[point][2];

point++;
}
Bezier_curve(3,cp);
}
if(extra_points)
{
for(int count_3=(point-1),count_4=0;count_3<n;
count_3++,count_4++)
{
cp[count_4][0]=cpts[count_3][0];
cp[count_4][1]=cpts[count_3][1];
cp[count_4][2]=cpts[count_3][2];
}
Bezier_curve(extra_points,cp);
}
}
/*************************************************************************///------------------------- Bezier_curve( ) -------------------------///*************************************************************************/void
Bezier_curve(constint n,constint cp[4][3])
{
setcolor(7);
double
double
double
double
double
double

x_1;
y_1;
z_1;
x_2;
y_2;
z_2;

for(int count=0;count<n;count++)
{
x_1=cp[count][0];
y_1=cp[count][1];
z_1=cp[count][2];
x_2=cp[(count+1)][0];
y_2=cp[(count+1)][1];
z_2=cp[(count+1)][2];
get_projected_point(x_1,y_1,z_1);
get_projected_point(x_2,y_2,z_2);
Dashed_line((int)(x_1+0.5),(int)(y_1+0.5),
(int)(x_2+0.5),(int)(y_2+0.5));
}
double x;
double y;
double z;
for(float u=0.0005;u<=1;u+=0.0005)
{
x=0;
y=0;
z=0;
for(int k=0;k<=n;k++)

{
x+=(cp[k][0]*nCr(n,k)*pow(u,k)*powl((1-u),(n-k)));
y+=(cp[k][1]*nCr(n,k)*pow(u,k)*powl((1-u),(n-k)));
z+=(cp[k][2]*nCr(n,k)*pow(u,k)*powl((1-u),(n-k)));
}
get_projected_point(x,y,z);
putpixel((int)(x+0.5),(int)(y+0.5),15);
}
}
/*************************************************************************///----------------------------- nCr( ) ------------------------------///*************************************************************************/double
nCr(int n,int r)
{
double nf;
double rf;
double nrf;
double ncr;
nf=factorial(n);
rf=factorial(r);
nrf=factorial((n-r));
ncr=(nf/(rf*nrf));
return ncr;
}
/*************************************************************************///-------------------------- factorial( ) ---------------------------///*************************************************************************/double
factorial(int number)
{
double factorial=1;
if(number==0 || number==1);
else
{
for(int count=1;count<=number;count++)
factorial=factorial*count;
}
return factorial;
}
/************************************************************************///-------------------- get_projected_point( ) ----------------------///************************************************************************/void
get_projected_point(double& x,double& y,double& 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}

};
float xy[4]={x,y,z,1};
float new_xy[4]={0};
multiply_matrices(xy,Par_v,new_xy);
x=new_xy[0];
y=new_xy[1];
z=new_xy[2];
}
/************************************************************************///--------------------- 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]);
}
}
/*************************************************************************///-------------------------- 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)
{
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
{
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( )
{
restorecrtmode( );
clrscr( );
textmode(C4350);
cprintf("\n***************************************************************************
*****");
cprintf("*-***********************************************-*");
cprintf("*------------------------- ");
textbackground(1);
cprintf(" Piece-Wise Bezier Curve ");
textbackground(8);
cprintf(" --------------------------*");
cprintf("*-***********************************************-*");
cprintf("*****************************************************************************-*");
for(int count=0;count<42;count++)
cprintf("*-*
*-*");
gotoxy(1,46);
cprintf("*****************************************************************************-*");
cprintf("*-----------------------------------------------------------------------------*");
cprintf("*****************************************************************************
***");
gotoxy(1,2);
}

A C++ Program to construct Clamped Cubic Spline


Interpolant from the given data.

Code for Program to construct Clamped Cubic Spline Interpolant from


the given data in C++ Programming
#
#
#
#
#
#

include <iostream.h>
include
<stdlib.h>
include
<string.h>
include
<stdio.h>
include
<conio.h>
include
<math.h>

constint max_size=13;
int n=0;
longdouble dfx0=0;
longdouble dfxn=0;
longdouble
longdouble
longdouble
longdouble

an[max_size]={0};
bn[max_size]={0};
cn[max_size]={0};
dn[max_size]={0};

longdouble fx[max_size]={0};
longdouble xn[max_size]={0};
void
void
void
void
void

show_screen( );
clear_screen( );
get_input( );
generate_clamped_cubic_spline( );
show_clamped_cubic_spline( );

/*************************************************************************//**********
***************************************************************///----------------------------- main( ) -----------------------------///*************************************************************************//********
*****************************************************************/int main( )
{
clrscr( );
textmode(C4350);
show_screen( );
get_input( );
generate_clamped_cubic_spline( );
show_clamped_cubic_spline( );
getch( );
return 0;
}
/*************************************************************************//**********
***************************************************************///----------------------- Funcion Definitions -----------------------///*************************************************************************//********
*****************************************************************//*******************
******************************************************///-------------------------show_screen( ) --------------------------///*************************************************************************/void
show_screen( )
{
cprintf("\n***************************************************************************
*****");
cprintf("**************************");
cprintf("*-------------- ");
textbackground(1);
cprintf(" Construction of Clamped Cubic Spline Interpolant ");
textbackground(8);
cprintf(" ------------*");
cprintf("*-**********************-*");
cprintf("*****************************************************************************-*");
for(int count=0;count<42;count++)
cprintf("*-*
*-*");
gotoxy(1,46);

cprintf("*****************************************************************************-*");
cprintf("*-----------------------------------------------------------------------------*");
cprintf("*****************************************************************************
***");
gotoxy(1,2);
}
/*************************************************************************///------------------------ clear_screen( ) --------------------------///*************************************************************************/void
clear_screen( )
{
for(int count=0;count<37;count++)
{
gotoxy(5,8+count);
cout<<"
";
}
gotoxy(1,2);
}
/*************************************************************************///---------------------------- get_input( ) -------------------------///*************************************************************************/void
get_input( )
{
do
{
clear_screen( );
gotoxy(6,9);
cout<<"Number of Distinct Data Points :";
gotoxy(6,10);
cout<<"";
gotoxy(27,13);
cout<<"[ min. n = 3

max. n = 12 ]";

gotoxy(6,12);
cout<<"Enter the max. number of distinct data points = n = ";
cin>>n;
if(n<3 || n>12)
{
gotoxy(12,25);
cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";
gotoxy(12,26);
cout<<"
to try again.";
n=int(getche( ));
if(n==27)
exit(0);
}

}
while(n<3 || n>12);
clear_screen( );
gotoxy(6,9);
cout<<"Data Points & Values of Function :";
gotoxy(6,10);
cout<<"";
gotoxy(16,12);
cout<<"";
gotoxy(16,13);
cout<<"
x

f(x)

f'(x)

";

gotoxy(16,14);
cout<<"";
gotoxy(16,15);
cout<<"

for(int count_1=0;count_1<n;count_1++)
{
gotoxy(16,(wherey( )+1));
cout<<"

gotoxy(16,(wherey( )+1));
cout<<"

";

";

";

}
gotoxy(16,(wherey( )+1));
cout<<"";
gotoxy(16,15);
for(int count_2=0;count_2<n;count_2++)
{
gotoxy(16,(wherey( )+1));
gotoxy(18,wherey( ));
cin>>xn[count_2];
gotoxy(34,(wherey( )-1));
cin>>fx[count_2];
if(count_2==0)
{
gotoxy(50,(wherey( )-1));
cin>>dfx0;
}
elseif(count_2==(n-1))
{
gotoxy(50,(wherey( )-1));
cin>>dfxn;
}
else
{
gotoxy(50,(wherey( )-1));

cout<<"-\n";
}
}
gotoxy(25,43);
cout<<"Press any key to continue...";
getch( );
}
/*************************************************************************///---------------- generate_clamped_cubic_spline( ) -----------------///*************************************************************************/void
generate_clamped_cubic_spline( )
{
// set ai=f(xi)
for i=0,1,2,3,...,nfor(int count_1=0;count_1<n;count_1++)
an[count_1]=fx[count_1];
longdouble temp_1[max_size]={0};
// hilongdouble temp_2[max_size]={0};
// ailongdouble temp_3[max_size]={0};
// lilongdouble temp_4[max_size]={0};
// uilongdouble temp_5[max_size]={0};
// zi// set hi=x(i+1)-xi
for
i=0,1,2,3,...,n-1for(int count_2=0;count_2<(n-1);count_2++)
temp_1[count_2]=(xn[count_2+1]-xn[count_2]);
// set ai0=3(a1-a0)/hi0-3dfx0// set ain=3dfxn-3{an-an(n-1)}/hi(n-1)
temp_2[0]=(((3*(an[1]-an[0]))/temp_1[0])-(3*dfx0));
temp_2[(n-1)]=((3*dfxn)-((3*(an[(n-1)]-an[(n-2)]))/temp_1[(n-2)]));
// set ai=(3/hi)*[a(i+1)-ai]-[3/h(i-1)]*[ai-a(i-1)]
for i=1,1,2,3,...,n1for(int count_3=1;count_3<(n-1);count_3++)
temp_2[count_3]=(((3/temp_1[count_3])*(an[(count_3+1)]-an[count_3]))((3/(temp_1[(count_3-1)])*(an[count_3]-an[(count_3-1)]))));
// set li0=2hi0//
ui0=0.5//
temp_3[0]=(2*temp_1[0]);
temp_4[0]=0.5;
temp_5[0]=(temp_2[0]/temp_1[0]);

zi0=ai0/li0

// for i=1,1,2,3,...,n-1 ,set//


li=[2*{x(i+1)-x(i-1)}]-[h(i-1)*u(i-1)]//
ui=hi/li//
zi=[ai-{h(i-1)*z(i-1)}]/lifor(int count_4=1;count_4<(n-1);count_4++)
{
temp_3[count_4]=((2*(xn[(count_4+1)]-xn[(count_4-1)]))-(temp_1[(count_41)]*temp_4[(count_4-1)]));
temp_4[count_4]=(temp_1[count_4]/temp_3[count_4]);
temp_5[count_4]=((temp_2[count_4]-(temp_1[(count_4-1)]*temp_5[(count_41)]))/temp_3[count_4]);
}
// set lin=h(n-1){2-ui(n-1)}//
zin={ain-hi(n-1)zi(n-1)}/lin//
cn=zin
temp_3[(n-1)]=(temp_1[(n-2)]*(2-temp_4[(n-2)]));
temp_5[(n-1)]=((temp_2[(n-1)]-(temp_1[(n-2)]*temp_5[(n-2)]))/temp_3[(n-1)]);
cn[(n-1)]=temp_5[(n-1)];
// for i=n-1,n-2,...,0
, set//
ci=zi-[ui*c(i+1)]//
bi=[a(i+1)ai]/[hi-{hi*{c(i+1)+{2*ci}}/3]//
di=[c(i+1)-ci]/[3*hi]for(int count_5=(n2);count_5>=0;count_5--)
{
cn[count_5]=(temp_5[count_5]-(temp_4[count_5]*cn[(count_5+1)]));
bn[count_5]=(((an[(count_5+1)]-an[count_5])/temp_1[count_5])((temp_1[count_5]*(cn[(count_5+1)]+(2*cn[count_5])))/3));
dn[count_5]=((cn[(count_5+1)]-cn[count_5])/(3*temp_1[count_5]));
}

}
/*************************************************************************///------------------- show_clamped_cubic_spline( ) ------------------///*************************************************************************/void
show_clamped_cubic_spline( )
{
clear_screen( );
gotoxy(6,9);
cout<<"Clamped Cubic Spline :";
gotoxy(6,10);
cout<<"";
gotoxy(10,12);
cout<<"The required Cubic Polynomials are :";
for(int count=0;count<(n-1);count++)
{
gotoxy(10,(15+(count*2)));
cout<<"S"<<count<<"(x) = ";
longdouble
longdouble
longdouble
longdouble

aix=0;
bix=0;
cix=0;
dix=0;

aix=(an[count]-(bn[count]*xn[count])+(cn[count]*powl(xn[count],2))(dn[count]*powl(xn[count],3)));
bix=(bn[count]-(2*cn[count]*xn[count])+(3*dn[count]*powl(xn[count],3)));
cix=(cn[count]-(3*dn[count]*xn[count]));
dix=dn[count];
cout<<aix;
if(bix>=0)
cout<<" + ";
else
cout<<" - ";
cout<<fabsl(bix)<<"x";
if(cix>=0)
cout<<" + ";
else
cout<<" - ";
cout<<fabsl(cix)<<"x2";
if(dix>=0)
cout<<" + ";
else
cout<<" - ";
cout<<fabsl(dix)<<"x3";
}
gotoxy(1,2);

/*
* File: SierpinskiTriangle.cpp
*/
#include <iostream>
#include <cmath>
#include "simpio.h"
#include "gwindow.h"
#include "gtypes.h"
#include "console.h"
using namespace std;
// Declare prototypes
static GPoint drawSierpinskiTriangle (int edgeLength, int fractalOrder, double cx, double cy,
GWindow &window);
static int getEdgeLength();
static int getFractalOrder();
int main() {
GWindow window(1000,1000);
cout << "Program to draw Sierpinski Triangle fractal" << endl;
int edgeLength = getEdgeLength();
int fractalOrder = getFractalOrder();
drawSierpinskiTriangle (edgeLength, fractalOrder, window.getWidth()/2,
window.getHeight()/2, window);
cout << "Drawing completed" << endl;
return 0;
}
// Edge length - user input
static int getEdgeLength(){
cout << "Enter outer edge length: (>0) " << endl;
int edgeLength = getInteger();
if (edgeLength<1) {
cout << "Edge length has to be greater than 0" << endl;
edgeLength = getEdgeLength();
}
return edgeLength;
}
// Fractal order - user input
static int getFractalOrder(){
cout << "Enter fractal order: (>=0) " << endl;
int fractalOrder = getInteger();
if (fractalOrder<0) {
cout << "Fractal order has to be greater than or equal to 0" << endl;
fractalOrder = getFractalOrder();
}
return fractalOrder;
}

static GPoint drawSierpinskiTriangle (int edgeLength, int fractalOrder, double cx, double cy,
GWindow &window){
// BASE CASE: ORDER = 0
if (fractalOrder==0){
int outlineTriangleHeight = sqrt(edgeLength*edgeLength (edgeLength/2)*(edgeLength/2));
GPoint pt0 (cx - edgeLength/2, cy + outlineTriangleHeight/2);
pt0 = window.drawPolarLine(pt0, edgeLength, 0);
pt0 = window.drawPolarLine(pt0, edgeLength, +120);
pt0 = window.drawPolarLine(pt0, edgeLength, -120);
return pt0;
}
// RECURSIVE STEP
edgeLength = edgeLength/2;
int triangleHeight = sqrt(edgeLength*edgeLength - (edgeLength/2)*(edgeLength/2));
// Three bottom left points for three inner triangles
GPoint pt1 (cx - edgeLength, cy + triangleHeight);
GPoint pt2 (cx, cy + triangleHeight);
GPoint pt3 (cx - edgeLength/2, cy);
// Draw bottom left triangle
pt1 = window.drawPolarLine(pt1, edgeLength, 0);
pt1 = window.drawPolarLine(pt1, edgeLength, +120);
pt1 = window.drawPolarLine(pt1, edgeLength, -120);
int bottomLeftCentreXDimension = cx-edgeLength/2;
int bottomLeftCentreYDimension = cy+triangleHeight/2;
drawSierpinskiTriangle (edgeLength, fractalOrder-1, bottomLeftCentreXDimension,
bottomLeftCentreYDimension, window);
// Draw bottom right triangle
pt2 = window.drawPolarLine(pt2, edgeLength, 0);
pt2 = window.drawPolarLine(pt2, edgeLength, +120);
pt2 = window.drawPolarLine(pt2, edgeLength, -120);
int bottomRightCentreXDimension = cx+edgeLength/2;
int bottomRightCentreYDimension = cy+triangleHeight/2;
drawSierpinskiTriangle (edgeLength, fractalOrder-1, bottomRightCentreXDimension,
bottomRightCentreYDimension, window);
// Draw top triangle
pt3 = window.drawPolarLine(pt3, edgeLength, 0);
pt3 = window.drawPolarLine(pt3, edgeLength, +120);
pt3 = window.drawPolarLine(pt3, edgeLength, -120);
int topXDimension = cx;
int topYDimention = cy - triangleHeight/2;
drawSierpinskiTriangle (edgeLength, fractalOrder-1,
window);
return pt1;
}

topXDimension,

topYDimention,

C++ Code for generating Sierpinski gasket (triangle)


C++ Code for generating Sierpinski gasket (triangle)
My code for generating fractal using C++. Programming fractals is very interesting guys so try it out
n have fun!!
Source code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void fract(int p[],int n);
void main(){ int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cout<<"Enter vertices of triangle:";
int j=0,p[20],i,n;
do{
for(i=0;i<3;i++){
cout<<"\n x"<<i+1<<"& y"<<i+1<<":";
cin>>p[j];
j=j+1;
cin>>p[j];
j=j+1;}
} while(j<6);
p[6]=p[0];
p[7]=p[1];
drawpoly(4,p);
cout<<"enter no: of iterations:";
cin>>n;
fract(p,n);
getch();
}
void fract(int p[],int n)
{int a[8],m[8],b[8],c[8],i,k;
for(i=0;i<8;i=i+2)
{m[i]=(int)(p[i]+p[i+2])/2;
m[i+1]=(int)(p[i+1]+p[i+3])/2;
}
m[6]=m[0];
m[7]=m[1];
drawpoly(4,m);
k=n-1;
if(k!=0){
a[0]=p[0];a[1]=p[1];a[2]=m[0];a[3]=m[1];a[4]=m[4];a[5]=m[5];a[6]=p[0];a[7]=p[1];
fract(a,k);
b[0]=p[2];b[1]=p[3];b[2]=m[0];b[3]=m[1];b[4]=m[2];b[5]=m[3];b[6]=p[2];b[7]=p[3];
fract(b,k);
c[0]=p[4];c[1]=p[5];c[2]=m[2];c[3]=m[3];c[4]=m[4];c[5]=m[5];c[6]=p[4];c[7]=p[5];

fract(c,k);}
}
OUTPUT:

You might also like