CPP Programs
CPP Programs
Hardik H Hadvani
on 00:01
1 comments
(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;
}
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);
}
}
}
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);
}
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);
}
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.");
}
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.");
}
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]);
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.");
}
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}
};
//
//
//
//
//
//
//
//
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);
}
}
{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.");
}
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}
};
//
//
//
//
//
//
//
//
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.");
}
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}
};
//
//
//
//
//
//
//
//
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]);
}
}
}
/*************************************************************************///------------------------------ 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.");
}
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]);
}
}
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);
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]);
}
}
{
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.");
}
include <iostream.h>
include <graphics.h>
include
<conio.h>
include
<math.h>
{
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,"
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,"
}
");
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>
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;
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.");
}
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);
}
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);
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
}
/*************************************************************************///------------------- 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,
fract(c,k);}
}
OUTPUT: