path.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <grass/gis.h>
  2. #include "driverlib.h"
  3. static void begin_subpath(struct path *p)
  4. {
  5. if (p->o_count >= p->o_alloc) {
  6. p->o_alloc += 100;
  7. p->offsets = G_realloc(p->offsets, p->o_alloc * sizeof(int));
  8. }
  9. p->offsets[p->o_count++] = p->count;
  10. p->cur_offset = p->count;
  11. }
  12. static void add_point(struct path *p, double x, double y)
  13. {
  14. if (p->count >= p->alloc) {
  15. p->alloc = p->alloc ? p->alloc * 2 : 100;
  16. p->px = G_realloc(p->px, p->alloc * sizeof(double));
  17. p->py = G_realloc(p->py, p->alloc * sizeof(double));
  18. }
  19. p->px[p->count] = x;
  20. p->py[p->count] = y;
  21. p->count++;
  22. }
  23. void path_begin(struct path *p)
  24. {
  25. p->count = 0;
  26. p->o_count = 0;
  27. begin_subpath(p);
  28. }
  29. void path_move(struct path *p, double x, double y)
  30. {
  31. if (p->count > p->cur_offset)
  32. begin_subpath(p);
  33. add_point(p, x, y);
  34. }
  35. void path_cont(struct path *p, double x, double y)
  36. {
  37. add_point(p, x, y);
  38. }
  39. void path_close(struct path *p)
  40. {
  41. if (p->count <= p->cur_offset + 2)
  42. return;
  43. add_point(p, p->px[p->cur_offset], p->py[p->cur_offset]);
  44. begin_subpath(p);
  45. }
  46. void path_fill(struct path *p, void (*polygon)(const double *, const double *, int))
  47. {
  48. int i;
  49. if (p->count > p->cur_offset)
  50. begin_subpath(p);
  51. for (i = 0; i < p->o_count - 1; i++) {
  52. int start = p->offsets[i];
  53. int end = p->offsets[i+1];
  54. (*polygon)(&p->px[start], &p->py[start], end - start);
  55. }
  56. path_reset(p);
  57. }
  58. void path_stroke(struct path *p, void (*line)(double, double, double, double))
  59. {
  60. int i, j;
  61. if (p->count > p->cur_offset)
  62. begin_subpath(p);
  63. for (i = 0; i < p->o_count - 1; i++)
  64. for (j = p->offsets[i] + 1; j < p->offsets[i+1]; j++)
  65. (*line)(p->px[j-1], p->py[j-1], p->px[j], p->py[j]);
  66. path_reset(p);
  67. }
  68. void path_reset(struct path *p)
  69. {
  70. p->count = 0;
  71. p->o_count = 0;
  72. }