Software & Finance





Turbo C Graphics - Drawing Circles with Pixels





This graphics program will draw circles by plotting the pixel by pixel. It uses the formula xend = x + r cos(angle) and yend = y + r sin(angle).


Source Code


#include <stdio.h>

#include <graphics.h>

#include <stdlib.h>

#include <conio.h>

#include <bios.h>

#include <math.h>

 

void DrawCircle(int x, int y, int r, int color)

{

      static const double PI = 3.1415926535;

      double i, angle, x1, y1;

 

      for(i = 0; i < 360; i += 0.1)

      {

            angle = i;

            x1 = r * cos(angle * PI / 180);

            y1 = r * sin(angle * PI / 180);

            putpixel(x + x1, y + y1, color);

      }

}

 

void main()

{

      int grd, grm, i;

      int color, xmax, ymax;

      detectgraph(&grd,&grm);

      initgraph(&grd, &grm, "");

 

      color = GREEN;

      setbkcolor(BLUE);

 

      xmax = getmaxx();

      ymax = getmaxy();

 

      setcolor(WHITE);

      rectangle(0,0,xmax,ymax);

 

      for(i = 0; i < 230; i += 10)

      {

            DrawCircle(320, 240, i, color++);

            if(color > WHITE)

                  color = GREEN;

      }

      getch();

      closegraph();

}

Click here to download the source code along with exe file

 

Output


Many circles in different colors will be shown pixel by pixel.