This is my code for drawing a circle using mid point algorithm:-

//Midpoint circle algorithm

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

void drawcircle(int xc,int yc,int r);

int main()
{
 int gdriver=DETECT,gmode;
 initgraph(&gdriver,&gmode,"..//bgi");
 drawcircle(250,250,10);
 getch();
 return 0;
}

void drawcircle(int xc,int yc,int r)
{
 int p,x=0,y=r,incr1=2*x+3,incr2=incr1+2-2*y;
 void drawPixel(int,int,int,int);
 drawPixel(xc,yc,x,y);
 p=1-r;
 while(x<=y)
 {
  if(p<0)
  {
  p+=incr1;
  x+=1;
  }
  else
  {
   p+=incr2;
   y-=1;
   x+=1;
  }
  drawPixel(xc,yc,x,y);
 }
}
void drawPixel(int xc,int yc,int x,int y)
{
  putpixel(xc+x,yc+y,RED);
  putpixel(xc+x,yc-y,RED);
  putpixel(xc-x,yc+y,RED);
  putpixel(xc-x,yc-y,RED);
  putpixel(xc+y,yc+x,RED);
  putpixel(xc+y,yc-x,RED);
  putpixel(xc-y,yc+x,RED);
  putpixel(xc-y,yc-x,RED);
}

The program compiles and runs but produces a distorted square like image in the output..no way near to the circle.. I am using Turbo C compiler.Please help me identify the error. I am following "Computer Graphics" Hearn and Baker book for the program.

I'm only guessing here but should you be using some sort of trig functions to get your points...

You are right.. Trigonometric functions would help in drawing accurate circle but I am not using it just to avoid floating point computations which would make the program run slow..That is why I am adopting this approach which involves only integer calculations.

I just want to know that there is some problem in the program or the algorithm produces this kind of output only.Thanks anyway... Still more replies needed though!!

Your problem is more related to the reason the limit system is a fundamental concept in calculus.

How many square sides do you need to create a perfect circle?
In this case, it's 1 per pixel.

How is that any less taxing than using a single trig function for each point in whatever granularity you specify?

If you don't care about accuracy, floating point computations are neither slow nor problematic with the right limitations. Not only that, but you could easily just do the math with an unsigned int type, flooring any decimal.

Trying to debug that program is not simple - he has set in his logic using very specific values including "magic" numbers. His book doesn't show a diagram of what the circle should look like? Weird!

When I ran it, I also got an ugly red square with barely rounded corners. I believe he was trying to print out several nearby pixels, after calculating for only one, but something went awry. ;)

What he wants to do is to set up something like this, but I'll use a simpler text console mode:

Remember your Pythagorean theorem: A² + B² = C². Where A = height length, B = base length, and C = hypotenuse of a right angle triangle.

The hypotenuse gives us the distance from the center of the circle, to the point on the screen (or element of the array), being considered at the moment.

#include <stdio.h>

int main(void) {
  int i, j;

  printf("\n\n");

  for(i=-10;i<11;i++) {
    for(j=-10;j<11;j++) {
      if((i*i)+(j*j) > 60) {
         putchar('$');
      }
      else
         putchar(' ');
    }
    putchar('\n');
  }
  printf("\n\n\t\t\t    press enter when ready");
  (void) getchar();
  return 0;
}

Which may well print up something that looks like a football oval (), instead of a circle. That depends on the relative height and width of your text window font. You can get a better circle, even concentric circles, but you have to use a more sophisticated program.


If you want to draw circles, and for some reason you don't want use the built in circle drawer in Turbo C, then this is one way to do it.

I wish I should have figured the mistake earlier.. Again a silly mistake and I made the output a distorted square. I was not incrementing p as it was required.Just try the following program and the output is a circle.

//Midpoint circle algorithm

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

void drawcircle(int xc,int yc,int r);

int main()
{
 int gdriver=DETECT,gmode;
 initgraph(&gdriver,&gmode,"..//bgi");
 drawcircle(250,250,100);
 getch();
 return 0;
}

void drawcircle(int xc,int yc,int r)
{
 int p,x=0,y=r,incr1,incr2;
 void drawPixel(int,int,int,int);
 drawPixel(xc,yc,x,y);
 p=1-r;
 while(x<=y)
 {
  if(p<0)
  {
  incr1=2*x+3;
  p+=incr1;
  x+=1;
  }
  else
  {
   incr2=incr1+2-2*y;
   p+=incr2;
   y-=1;
   x+=1;
  }
  drawPixel(xc,yc,x,y);
 }
}
void drawPixel(int xc,int yc,int x,int y)
{
  putpixel(xc+x,yc+y,RED);
  putpixel(xc+x,yc-y,RED);
  putpixel(xc-x,yc+y,RED);
  putpixel(xc-x,yc-y,RED);
  putpixel(xc+y,yc+x,RED);
  putpixel(xc+y,yc-x,RED);
  putpixel(xc-y,yc+x,RED);
  putpixel(xc-y,yc-x,RED);
}

Besides all this, I am forced to avoid the trigonometric and other ways because it is in the curriculum that way and I need to do that way only to score in my exams.Thanks for all your suggestions.I wish I could have identified the mistake earlier.

I have since discovered that this Midpoint Circle Algorithm, is actually quite efficient.

From the center point, it puts out 8 pixels with every iteration:

* One pixel for each end of four arc.

As the four arcs get larger, they will all merge together, to form one circle.

If you put a slight delay between each call to drawpixel(), you'll see the four arcs, and how this algorithm works, much more clearly.

Wonderful algorithm! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.