m2skills

bresenham circle c

Mar 29th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 KB | None | 0 0
  1. /* Program to implement Bresenhams line generation algorithm */
  2.  
  3. #include <graphics.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <dos.h>
  8.  
  9. // method that plots the circle points in all the 8 quadrants
  10. void drawcircle(int xc, int yc, int x,int y)
  11. {
  12.     putpixel(xc+x,yc+y,5);       //quad 1
  13.     putpixel(xc+y,yc+x,6);       //quad 2
  14.     putpixel(xc-x,yc+y,5);       //quad 3
  15.     putpixel(xc-y,yc+x,6);       //quad 4
  16.     putpixel(xc-x,yc-y,5);       //quad 5
  17.     putpixel(xc-y,yc-x,6);       //quad 6
  18.     putpixel(xc+x,yc-y,5);       //quad 7
  19.     putpixel(xc+y,yc-x,6);       //quad 8
  20. }
  21.  
  22. // function that calculates the points of the circle to be plotted
  23. // inputs are the the center co-ordinates and value of the radius
  24. void bresenhemCircle(int xc, int yc, int radius){
  25.    
  26.     // x, y are used to store the new points of the circle
  27.     // parameter decides whether a point is inside , outside or on the circle
  28.     int x, y, parameter;
  29.    
  30.     // initial values for x, y, parameter
  31.     x=0;
  32.     y=radius;
  33.     parameter = 3-2*radius;
  34.    
  35.     // plot the initial point
  36.     drawcircle(xc,yc,x,y);
  37.    
  38.     while(x<y)
  39.     {
  40.         // parameter is less than 0 hence point is inside the circle so we increase the value of x
  41.         if(parameter<0)
  42.         {
  43.             parameter=parameter+4*x+6;
  44.             x++;
  45.         }
  46.         // parameter is 0 or greater than 0
  47.         // so we increase both values of x and y
  48.         else
  49.         {
  50.             parameter=parameter+4*(x-y)+10;
  51.             x++;
  52.             y--;
  53.  
  54.         }
  55.         drawcircle(xc,yc,x,y);
  56.         delay(10);
  57.    
  58.     }
  59. }
  60. void main()
  61. {
  62.     int gd=0,gm, i = 50;
  63.    
  64.     initgraph(&gd,&gm,"C:\\TC\\BGI");
  65.     // circle with center (320, 240) and radius = 150
  66.     bresenhemCircle(320, 240, 150);
  67.    
  68.     while(i < 100){
  69.         bresenhemCircle(320, 240, i);
  70.         i += 5;
  71.     }
  72.     getch();
  73. }
Add Comment
Please, Sign In to add comment