Thursday, 7 January 2016

Mid-Point Circle Drawing Algorithm Using C++

// Program to implement mid-point Circle Drawing Algorithm Using C++.
#include<iostream>

#include<graphics.h>
using namespace std;

void circle(int ,int ,int ,int );
void circlepoints(int ,int ,int ,int ,int );


void circle(int x0,int y0,int r,int xc)
{
 int x=0;
 int y=r;
 double d=1-r;
 initwindow(640,480);
 circlepoints(x0,x,y0,y,xc);
 cout<<"Value after iteration is::\n";

 while(y>x)
 {
  if(d<0)
  {
   d+=2*x+3;
   cout<<"Value of d is:"<<d<<"\n";
  }
  else
  {
   d+=2*(x-y)+5;
   cout<<"Value of d is:"<<d<<"\n";
   y--;
  }
  x++;
  circlepoints(x0,x,y0,y,xc);
  cout<<x<<" "<<y<<"\n"<<"\n";
 }
 delay(50000);
 closegraph();
}

void circlepoints(int x0,int x,int y0,int y,int xc)
{
 putpixel((x0+x),(y0+y), xc);
 putpixel((x0+x),(y0-y), xc);
 putpixel((x0-x),(y0-y), xc);
 putpixel((x0-x),(y0+y), xc);
 putpixel((x0+y),(y0+x), xc);
 putpixel((x0+y),(y0-x), xc);
 putpixel((x0-y),(y0+x), xc);
 putpixel((x0-y),(y0-x), xc);
}


int main()
{
 int m[16]={BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE};
 int x0,r,y0;
 int xc;

 cout<<"Enter the value of x0\n";
 cin>>x0;

 cout<<"Enter the value of y0\n";
 cin>>y0;

 cout<<"Enter the value of r\n";
 cin>>r;

 cout<<"Enter the color\n";
 for(int i=0;i<16;i++)
 {
       cin>>m[i];
       xc=m[i];
       break;
 }
 circle(x0,y0,r,xc);
 closegraph();
 return 0;
}

// Output of the above program

Mid-Point Circle Drawing Algorithm Using C++
Mid-Point Circle Drawing Algorithm Using C++
Mid-Point Circle Drawing Algorithm Using C++
Mid-Point Circle Drawing Algorithm Using C++



Mid-Point Circle Drawing Algorithm Using C++
Mid-Point Circle Drawing Algorithm Using C++

Mid-Point Circle Drawing Algorithm Using C++
Mid-Point Circle Drawing Algorithm Using C++


Checkout this video on mid-point Circle Drawing Algorithm Using C++ in CodeBlocks 13.12...




1 comment:

  1. error in midpoint cicle algorithm in header graphic.h

    ReplyDelete