GRASS GIS 8 Programmer's Manual 8.2.1RC1(2022)-exported
path.c
Go to the documentation of this file.
1
2#include <grass/gis.h>
3#include "path.h"
4
5void path_init(struct path *p)
6{
7 p->vertices = NULL;
8 p->count = 0;
9 p->alloc = 0;
10 p->start = -1;
11}
12
13void 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
23void 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
32void path_reset(struct path *p)
33{
34 p->count = 0;
35 p->start = -1;
36}
37
38void 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
52void path_copy(struct path *dst, const struct path *src)
53{
54 int i;
55
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
67void path_begin(struct path *p)
68{
69 p->count = 0;
70 p->start = -1;
71}
72
73void 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
79void path_cont(struct path *p, double x, double y)
80{
81 path_append(p, x, y, P_CONT);
82}
83
84void 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
97void 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 G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149
#define NULL
Definition: ccmath.h:32
char * dst
Definition: lz4.h:599
void path_close(struct path *p)
Definition: path.c:84
void path_stroke(struct path *p, void(*line)(double, double, double, double))
Definition: path.c:97
void path_copy(struct path *dst, const struct path *src)
Definition: path.c:52
void path_reset(struct path *p)
Definition: path.c:32
void path_free(struct path *p)
Definition: path.c:13
void path_begin(struct path *p)
Definition: path.c:67
void path_cont(struct path *p, double x, double y)
Definition: path.c:79
void path_append(struct path *p, double x, double y, int mode)
Definition: path.c:38
void path_alloc(struct path *p, int n)
Definition: path.c:23
void path_move(struct path *p, double x, double y)
Definition: path.c:73
void path_init(struct path *p)
Definition: path.c:5
@ P_CONT
Definition: path.h:7
@ P_MOVE
Definition: path.h:6
@ P_CLOSE
Definition: path.h:8
Definition: path.h:16
int count
Definition: path.h:18
int start
Definition: path.h:20
struct vertex * vertices
Definition: path.h:17
int alloc
Definition: path.h:19
Definition: path.h:11
int mode
Definition: path.h:13
double x
Definition: path.h:12
double y
Definition: path.h:12
#define x