Shifting and Scaling the circle using Bresenham’s Algorithm

As discussed in previous blog,  Scaling refers to linear transformation that enlarges or diminishes objects. Along with scaling capability, We’ve now added  the coordinate axis and shift capability  to Bresenham’s algorithm.

Shifting a Circle refers to – changing the coordinates of the center of the circle. In all the programs, discussed in previous blogs, we’ve always taken the center of circle at the origin. Here, In the following program,  we let the user define the coordinates (a,b) of the center and thus, giving the shift capability to the Algorithm. We’ve implemented the shift capability by adding the values of a and b in the formula defined for variables row and coulmn ( variables row and column specify particular points of circle).

 

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
    float x,y,rad,scale , asp_hgt;
    int row,col, hgt,wth,a,b;
    char circle[500][500];
    cout<<” Enter the size of the plane: “;
    cin>>hgt>>wth;
    cout<<”\n Enter the scale factor: “;
    cin>>scale;
    cout<<” \n Enter the position of origin: “;
    cin>>a>>b;
    asp_hgt=hgt/1.5;   
    for(int row=0;row<hgt;row++)      
    {
        for(int col=0;col<wth;col++)
        {
            circle[row][col]=’ ‘;
        }
    }

        // for horizontal axis
         for ( col=0; col<=wth; col++)
        {   
            row= asp_hgt/2;
            circle[row][col]=’_’;
        }
    
   //for vertical axis
    for (row=0;row<=asp_hgt;row++)
    {       

             col = wth/2;
            circle[row][col]=’|’;
    } 
    cout<<”Enter the radius: “;
    cin>>rad;
    rad*=scale;               
    //for scaling    
    
    y=rad;
    x=0;
    int sum=2*x*x+2*y*y+4*x-2*y-2*rad*rad+3;
    b=-b;
    do
    {
       if(sum<0)
       {
          x=x+0.5;
          sum=sum+4*x+6;
       }
       else
       {
          x=x+0.5;
          y=y-0.5;
          sum=sum+4*(x-y)+10;
       }
       row=hgt/3+x*0.53+b*0.53;      
       //a and b for shift      

       col=wth/2+y+a;
       circle[row][col]=’*’;
       row=hgt/3-x*0.53+b*0.53;
       col=wth/2+y+a;
       circle[row][col]=’*’;
       row=hgt/3+x*0.53+b*0.53;
       col=wth/2-y+a;
       circle[row][col]=’*’;
       row=hgt/3-x*0.53+b*0.53;
       col=wth/2-y+a;
       circle[row][col]=’*’;
       row=hgt/3+y*0.53+b*0.53;
       col=wth/2+x+a;
       circle[row][col]=’*’;
       row=hgt/3-y*0.53+b*0.53;
       col=wth/2+x+a;
       circle[row][col]=’*’;
       row=hgt/3+y*0.53+b*0.53;
       col=wth/2-x+a;
       circle[row][col]=’*’;
       row=hgt/3-y*0.53+b*0.53;
       col=wth/2-x+a;
       circle[row][col]=’*’;
    }
    while(x<=y);
   
   for(int row=1;row<hgt/1.5;row++)
   {
       for(int col=1;col<wth;col++)
       {
           cout<<circle[row][col];
       }
       cout<<endl;
   }
   return 0;
}

      Here’s the screenshot of what the output looks like :

   Image

Advertisements

C++ Implementation of Bresenham’s Algorithm

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
float x,y,rad;
int row,col, hgt,wth;
char circle[500][500];
cout<<” Enter the size of the plane: “;
// to define the coordinate plane

cin>>hgt>>wth;
for(int r=0;r<hgt;r++)
//define the array ( array of spaces) to store the points to be plotted

{
for(int c=0;c<wth;c++)
{
circle[r][c]=’ ‘;
}
}
cout<<”Enter the radius: “;
cin>>rad;
y=rad;
x=0;
int sum=2*x*x+2*y*y+4*x-2*y-2*rad*rad+3;
//sum, the decision variable, used in bresenham algorithm

do
{
if(sum<0)
{
x=x+0.5;
sum=sum+4*x+6;
// to compute the next point when sum<0

}
else
{
x=x+0.5;
y=y-0.5;
sum=sum+4*(x-y)+10;
// to compute the next point when sum>0

}
row=hgt/3+x*0.53;
// reaching the middle of the graph height-wise as per the 1.5 pixel ratio (2*1.5=3)

col=wth/2+y;
//reaching the middle of the graph width-wise

circle[row][col]=’*’;
row=hgt/3-x*0.53;
//computing the row and the column number to reach the point on the plane and store them in the array circle

col=wth/2+y;
circle[row][col]=’*’;
row=hgt/3+x*0.53;
//rest points are plotted as per the symmetry of the circle (7 more points can be obtained from a single point coordinates)

col=wth/2-y;
circle[row][col]=’*’;
row=hgt/3-x*0.53;
col=wth/2-y;
circle[row][col]=’*’;
row=hgt/3+y*0.53;
col=wth/2+x;
circle[row][col]=’*’;
row=hgt/3-y*0.53;
col=wth/2+x;
circle[row][col]=’*’;
row=hgt/3+y*0.53;
col=wth/2-x;
circle[row][col]=’*’;
row=hgt/3-y*0.53;
col=wth/2-x;
circle[row][col]=’*’;
}
while(x<=y);
// the condition to be satisfied for bresenham algorithm

for(int r=1;r<hgt/1.5;r++)
// print the array to display the circle

{
for(int c=1;c<wth;c++)
{
cout<<circle[r][c];
}
cout<<endl;
}
return 0;
}

 Here’s the screenshot of what the output looks like :

 

Image