Advertisement
Riz1Ahmed

Bresenham Circle Algorithm

Sep 11th, 2020
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<graphics.h>
  3. float abs (float n){ return n>0 ? n:-n;}
  4. float max(float x,float y){return x>y ? x:y;}
  5.  
  6. void setPixel(int x,int y,int x0,int y0){
  7.     putpixel(x0 + x, y0 + y, YELLOW);
  8.     putpixel(x0 - x, y0 + y, YELLOW);
  9.     putpixel(x0 + x, y0 - y, YELLOW);
  10.     putpixel(x0 - x, y0 - y, YELLOW);
  11.     putpixel(x0 + y, y0 + x, YELLOW);
  12.     putpixel(x0 - y, y0 + x, YELLOW);
  13.     putpixel(x0 + y, y0 - x, YELLOW);
  14.     putpixel(x0 - y, y0 - x, YELLOW);
  15. }
  16. void MidPointCircleAlgorithm(int x0, int y0, int r){
  17.     int x=0, y=r, p=1-r;
  18.     while(x<=y){
  19.         printf("p=%d xy(%d,%d)\n",p,x,y);
  20.         setPixel(x,y,x0,y0);
  21.         delay(10);//So that we can see how draw line
  22.  
  23.         if (p<0)       x++,    p=p+2*x+1;
  24.         else if (p>=0) x++,y--,p=p+2*(x-y)+1;
  25.     }
  26. }
  27. int main(){
  28.     int gd = DETECT, gm;
  29.     initgraph (&gd, &gm, "");//Initialize graphics function
  30.     puts("***Mid Point Cirle Algorithm***\n");
  31.  
  32.     int x, y, r;
  33.     while(1){
  34.         printf("Enter Center(x,y) and Radius(r) of circle (Ex:50 50 20): ");
  35.         scanf("%d %d %d", &x, &y,&r);
  36.         MidPointCircleAlgorithm(x, y, r);
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement