Tuesday, 21 June 2016

Draw a Circle : Mid-Point Algorithm



C Program

  1. #include<graphics.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. void plotpoints(int x, int y, int cx, int cy) {
  5. putpixel(cx + x, cy + y, 4);
  6. putpixel(cx - x, cy + y, 4);
  7. putpixel(cx + x, cy - y, 4);
  8. putpixel(cx - x, cy - y, 4);
  9. putpixel(cx + y, cy + x, 4);
  10. putpixel(cx - y, cy + x, 4);
  11. putpixel(cx + y, cy - x, 4);
  12. putpixel(cx - y, cy - x, 4);
  13. }
  14. void main() {
  15. int cx, cy, x = 0, y, r, p;
  16. int gd = DETECT, gm = DETECT;
  17. clrscr();
  18. printf("Enter the center \n");
  19. scanf("%d%d", &cx, &cy);
  20. printf("Enter the radius : ");
  21. scanf("%d", &r);
  22. y = r;
  23. p = 1 - r;
  24. initgraph(&gd, &gm, "");
  25. cleardevice();
  26. while (x < y) {
  27. plotpoints(x, y, cx, cy);
  28. x++;
  29. if (p < 0)
  30. p += 2 * x + 1; else {
  31. y--;
  32. p += 2 * (x - y) + 1;
  33. }
  34. }
  35. getch();
  36. }

No comments:

Post a Comment