GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-535c39c9fc
driver/path.c
Go to the documentation of this file.
1 #include <grass/gis.h>
2 #include "path.h"
3 
4 void path_init(struct path *p)
5 {
6  p->vertices = NULL;
7  p->count = 0;
8  p->alloc = 0;
9  p->start = -1;
10 }
11 
12 void path_free(struct path *p)
13 {
14  if (p->vertices)
15  G_free(p->vertices);
16 
17  p->count = 0;
18  p->alloc = 0;
19  p->start = -1;
20 }
21 
22 void path_alloc(struct path *p, int n)
23 {
24  if (p->alloc >= n)
25  return;
26 
27  p->alloc = n;
28  p->vertices = G_realloc(p->vertices, p->alloc * sizeof(struct vertex));
29 }
30 
31 void path_reset(struct path *p)
32 {
33  p->count = 0;
34  p->start = -1;
35 }
36 
37 void path_append(struct path *p, double x, double y, int mode)
38 {
39  struct vertex *v;
40 
41  if (p->count >= p->alloc)
42  path_alloc(p, p->alloc ? p->alloc * 2 : 100);
43 
44  v = &p->vertices[p->count++];
45 
46  v->x = x;
47  v->y = y;
48  v->mode = mode;
49 }
50 
51 void path_copy(struct path *dst, const struct path *src)
52 {
53  int i;
54 
55  path_reset(dst);
56  path_alloc(dst, src->count);
57 
58  for (i = 0; i < src->count; i++) {
59  struct vertex *v = &src->vertices[i];
60  path_append(dst, v->x, v->y, v->mode);
61  }
62 
63  dst->start = src->start;
64 }
65 
66 void path_begin(struct path *p)
67 {
68  p->count = 0;
69  p->start = -1;
70 }
71 
72 void path_move(struct path *p, double x, double y)
73 {
74  p->start = p->count;
75  path_append(p, x, y, P_MOVE);
76 }
77 
78 void path_cont(struct path *p, double x, double y)
79 {
80  path_append(p, x, y, P_CONT);
81 }
82 
83 void path_close(struct path *p)
84 {
85  struct vertex *v;
86 
87  if (p->start < 0)
88  return;
89 
90  v = &p->vertices[p->start];
91  path_append(p, v->x, v->y, P_CLOSE);
92 
93  p->start = -1;
94 }
95 
96 void path_stroke(struct path *p, void (*line)(double, double, double, double))
97 {
98  int i;
99 
100  for (i = 1; i < p->count; i++) {
101  struct vertex *v0 = &p->vertices[i - 1];
102  struct vertex *v1 = &p->vertices[i];
103 
104  if (v1->mode != P_MOVE)
105  (*line)(v0->x, v0->y, v1->x, v1->y);
106  }
107 
108  path_reset(p);
109 }
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_realloc(p, n)
Definition: defs/gis.h:96
void path_close(struct path *p)
Definition: driver/path.c:83
void path_stroke(struct path *p, void(*line)(double, double, double, double))
Definition: driver/path.c:96
void path_copy(struct path *dst, const struct path *src)
Definition: driver/path.c:51
void path_reset(struct path *p)
Definition: driver/path.c:31
void path_free(struct path *p)
Definition: driver/path.c:12
void path_begin(struct path *p)
Definition: driver/path.c:66
void path_cont(struct path *p, double x, double y)
Definition: driver/path.c:78
void path_append(struct path *p, double x, double y, int mode)
Definition: driver/path.c:37
void path_alloc(struct path *p, int n)
Definition: driver/path.c:22
void path_move(struct path *p, double x, double y)
Definition: driver/path.c:72
void path_init(struct path *p)
Definition: driver/path.c:4
@ P_CONT
Definition: path.h:6
@ P_MOVE
Definition: path.h:5
@ P_CLOSE
Definition: path.h:7
Definition: path.h:15
int count
Definition: path.h:17
int start
Definition: path.h:19
struct vertex * vertices
Definition: path.h:16
int alloc
Definition: path.h:18
Definition: path.h:10
int mode
Definition: path.h:12
double x
Definition: path.h:11
double y
Definition: path.h:11
#define x