Задача: Рисование окружности (по Брезенхэму)
Исходник: Рисование окружности (по Брезенхэму), язык: C [code #132, hits: 11559]
автор: - [добавлен: 17.05.2006]
  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <math.h>
  6. // this function initializes graphics mode
  7. // it will work only if you're using Borland C++ compiler & BGI drivers
  8. // if you're using another compiler you should overwrite body of this function
  9. void init_gr(void)
  10. {
  11. /* request autodetection */
  12. int gdriver = DETECT, gmode, errorcode;
  13. /* initialize graphics mode */
  14. initgraph(&gdriver, &gmode, "");
  15. /* read result of initialization */
  16. errorcode = graphresult();
  17. if (errorcode != grOk) /* an error occurred */
  18. {
  19. printf("Graphics error: %s\n", grapherrormsg(errorcode));
  20. printf("Press any key to halt:");
  21. getch();
  22. exit(1); /* return with error code */
  23. }
  24. }
  25. // this function shuts graphics mode down
  26. // it will work only if you're using Borland C++ compiler & BGI drivers
  27. // if you're using another compiler you should overwrite body of this function
  28. void end_gr(void)
  29. {
  30. closegraph();
  31. }
  32. // this function puts pixel on the screen in (x,y) position using color 'color'
  33. // it will work only if you're using Borland C++ compiler & BGI drivers
  34. // if you're using another compiler you should overwrite body of this function
  35. void PutPixel(int x, int y, int color)
  36. {
  37. putpixel(x,y,color);
  38. }
  39. float const ratio=1.0; // you can change this to draw ellipses
  40. // This function plots points that belongs to the circle
  41. // It recieves offsets from center for the fist quadrant
  42. // and plots symmetrical points in all four quadrants
  43. void plot_circle(int x,int y, int x_center, int y_center, int color)
  44. {
  45. int x_start,y_start,x_end,y_end,x1,y1;
  46. // the distanse between start and end can be greater than 1 if ratio!=1
  47. y_start=y*ratio;
  48. y_end=(y+1)*ratio;
  49. x_start=x*ratio;
  50. x_end=(x+1)*ratio;
  51. for (x1=x_start;x1<x_end;++x1)
  52. {
  53. // plot points in all four quadrants
  54. PutPixel(x1+x_center,y+y_center,color);
  55. PutPixel(x1+x_center,y_center-y,color);
  56. PutPixel(x_center-x1,y+y_center,color);
  57. PutPixel(x_center-x1,y_center-y,color);
  58. }
  59. for (y1=y_start;y1<y_end;++y1)
  60. {
  61. // plot points in all four quadrants
  62. PutPixel(y1+x_center,x+y_center,color);
  63. PutPixel(y1+x_center,y_center-x,color);
  64. PutPixel(x_center-y1,x+y_center,color);
  65. PutPixel(x_center-y1,y_center-x,color);
  66. }
  67. }
  68. // This is main function that draws circle using function
  69. void Circle(int x1,int y1,int radius, int color)
  70. {
  71. int x,y,delta;
  72. // Y * we start from * and increase x step by step
  73. // | decreasing y when needed
  74. // |
  75. // |
  76. // --------------------
  77. // | X
  78. // |
  79. // |
  80. // |
  81. y=radius;
  82. delta=3-2*radius; // delta is an error
  83. // calculate values for first quadrant
  84. for (x=0;x<y;x++) // x is a main axe
  85. {
  86. // plot points symmetrically in all quadrants
  87. plot_circle(x,y,x1,y1,color);
  88. if (delta<0) delta+=4*x+6;
  89. else
  90. {
  91. delta+=4*(x-y)+10;
  92. y--; // it's time to decrease y
  93. }
  94. }
  95. x=y;
  96. if (y!=0) plot_circle(x,y,x1,y1,color);
  97. }
  98. int main(void)
  99. {
  100. // initializing graphics mode
  101. init_gr();
  102. /* examples */
  103. Circle(200,200,100,14);
  104. Circle(300,200,100,15);
  105. Circle(400,200,100,13);
  106. Circle(250,100,100,12);
  107. Circle(350,100,100,11);
  108. Circle(50,400,25,2);
  109. Circle(500,400,25,2);
  110. /* clean up */
  111. getch();
  112. end_gr();
  113. return 0;
  114. }
//////////////////////////////////////////////////////////////////////////////
//
// Bresengham circle drawing
// (c) Johna Smith, 1996
//
// Method description:
// In this algorithm all floating point math changed to sequences
// of additions and substractions. The main idea of this algorithm
// is to increase x and y by the value of the error between them.
//
// ! This program is NOT optimized for best performance !
// To do so don't use putpixel function - change bytes in video memory directly.
//
//////////////////////////////////////////////////////////////////////////////

+добавить реализацию