pie.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <math.h>
  2. #include <grass/gis.h>
  3. #include <grass/vector.h>
  4. #include <grass/dbmi.h>
  5. #include <grass/display.h>
  6. #include <grass/symbol.h>
  7. #include "global.h"
  8. #define PI M_PI
  9. int
  10. pie(double cx, double cy, int size, double *val, int ncols, COLOR * ocolor,
  11. COLOR * colors)
  12. {
  13. int i, j, n;
  14. double a, end_ang, ang, tot_sum, sum, step, r;
  15. double x, y;
  16. struct line_pnts *Points;
  17. G_debug(4, "pie(): cx = %f cy = %f", cx, cy);
  18. Points = Vect_new_line_struct();
  19. /* Calc sum */
  20. tot_sum = 0;
  21. for (i = 0; i < ncols; i++)
  22. tot_sum += val[i];
  23. step = PI / 180;
  24. r = (D_d_to_u_col(2) - D_d_to_u_col(1)) * size / 2; /* do it better */
  25. /* Draw polygon for each value */
  26. sum = 0;
  27. ang = 0;
  28. for (i = 0; i < ncols; i++) {
  29. sum += val[i];
  30. end_ang = 2 * PI * sum / tot_sum;
  31. Vect_reset_line(Points);
  32. if (val[0] != tot_sum) /* all in one slice, don't draw line to center */
  33. Vect_append_point(Points, cx, cy, 0);
  34. n = (int)ceil((end_ang - ang) / step);
  35. for (j = 0, a = ang; j <= n; j++, a += step) {
  36. if (a > end_ang)
  37. a = end_ang;
  38. x = cx + r * cos(a);
  39. y = cy + r * sin(a);
  40. Vect_append_point(Points, x, y, 0);
  41. }
  42. ang = end_ang;
  43. if (val[0] != tot_sum)
  44. Vect_append_point(Points, cx, cy, 0);
  45. if (!colors[i].none) {
  46. D_RGB_color(colors[i].r, colors[i].g, colors[i].b);
  47. D_polygon_abs(Points->x, Points->y, Points->n_points);
  48. }
  49. D_RGB_color(ocolor->r, ocolor->g, ocolor->b);
  50. D_polyline_abs(Points->x, Points->y, Points->n_points);
  51. }
  52. Vect_destroy_line_struct(Points);
  53. return 0;
  54. }