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