draw_slice.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <math.h>
  2. #include <grass/display.h>
  3. #include <grass/gis.h>
  4. char percent[] = "%";
  5. int draw_slice(struct Colors *colors, int fill_flag, DCELL fill_color1, DCELL fill_color2, int txt_color, double cx, double cy, double r, /* in normalized coords. */
  6. double a1, double a2 /* in degrees */
  7. )
  8. {
  9. double tt, tb, tr, tl;
  10. int height, width;
  11. double yoffset, xoffset;
  12. double x[1000], y[1000];
  13. int lx, ly;
  14. int i = 1;
  15. char txt[512];
  16. double arc, arc_incr = 0.01;
  17. DCELL fill_color;
  18. D_get_src(&tt, &tb, &tl, &tr);
  19. height = tb - tt;
  20. width = tr - tl;
  21. yoffset = tb;
  22. xoffset = tl;
  23. while (a2 / arc_incr > 998)
  24. arc_incr *= 2;
  25. x[0] = (xoffset + cx * width);
  26. y[0] = (yoffset - cy * height);
  27. arc = a1;
  28. if (fill_flag && fill_color1 != fill_color2) {
  29. i = 1;
  30. while (arc <= (a1 + a2)) {
  31. fill_color = fill_color1 + (arc - a1) *
  32. (fill_color2 - fill_color1) / a2;
  33. x[i] = x[0] + r * (width) * cos(arc / 57.296);
  34. y[i] = y[0] - r * (height) * sin(arc / 57.296);
  35. if (i == 2) {
  36. D_d_color(fill_color, colors);
  37. D_polygon_abs(x + i - 2, y + i - 2, 3);
  38. x[i - 1] = x[i];
  39. y[i - 1] = y[i];
  40. }
  41. else
  42. i++;
  43. arc = arc + arc_incr;
  44. }
  45. }
  46. else {
  47. i = 1;
  48. while (arc <= (a1 + a2)) {
  49. x[i] = x[0] + r * (width) * cos(arc / 57.296);
  50. y[i] = y[0] - r * (height) * sin(arc / 57.296);
  51. i++;
  52. arc = arc + arc_incr;
  53. }
  54. if (!fill_flag) {
  55. D_use_color(txt_color);
  56. D_polyline_abs(x, y, i);
  57. }
  58. else {
  59. D_d_color(fill_color1, colors);
  60. D_polygon_abs(x, y, i);
  61. }
  62. }
  63. if (a2 > 15.0) {
  64. /* draw a label */
  65. arc = a1 + a2 / 2;
  66. sprintf(txt, "%2.0f%s", (a2 / 360.0) * 100.0, percent);
  67. D_get_text_box(txt, &tt, &tb, &tl, &tr);
  68. lx = x[0] + (r + 0.03) * (width) * cos(arc / 57.296) - (tr - tl) / 2;
  69. ly = y[0] - (r + 0.03) * (height) * sin(arc / 57.296) + (tb - tt) / 2;
  70. D_pos_abs(lx, ly);
  71. D_use_color(txt_color);
  72. D_text(txt);
  73. }
  74. return 0;
  75. }
  76. int draw_slice_unfilled(struct Colors *colors, int tc, double cx, double cy, /* circle center, in normalized coords. */
  77. double r, /* circle radius, in normalized units */
  78. double a1, /* arc start position, in degrees */
  79. double a2 /* arc size, in degrees */
  80. )
  81. {
  82. draw_slice(colors, 0, 0., 0., tc, cx, cy, r, a1, a2);
  83. return 0;
  84. }
  85. int draw_slice_filled(struct Colors *colors, DCELL fc, /* fill color */
  86. int tc, /* text color */
  87. double cx, double cy, /* circle center, in normalized coords. */
  88. double r, /* circle radius, in normalized units */
  89. double a1, /* arc start position, in degrees */
  90. double a2 /* arc size, in degrees */
  91. )
  92. {
  93. draw_slice(colors, 1, fc, fc, tc, cx, cy, r, a1, a2);
  94. return 0;
  95. }