1 | /*
|
---|
2 | * Copyright (c) 2012 Petr Koupy
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup draw
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <malloc.h>
|
---|
38 |
|
---|
39 | #include "path.h"
|
---|
40 |
|
---|
41 | struct path {
|
---|
42 | list_t list;
|
---|
43 | double cur_x;
|
---|
44 | double cur_y;
|
---|
45 | };
|
---|
46 |
|
---|
47 | void path_init(path_t *path)
|
---|
48 | {
|
---|
49 | list_initialize(&path->list);
|
---|
50 | path->cur_x = 0;
|
---|
51 | path->cur_y = 0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | void path_clear(path_t *path)
|
---|
55 | {
|
---|
56 | while (!list_empty(&path->list)) {
|
---|
57 | path_step_t *step = (path_step_t *) list_last(&path->list);
|
---|
58 | list_remove(&step->link);
|
---|
59 | free(step);
|
---|
60 | }
|
---|
61 | path->cur_x = 0;
|
---|
62 | path->cur_y = 0;
|
---|
63 | }
|
---|
64 |
|
---|
65 | void path_get_cursor(path_t *path, double *x, double *y)
|
---|
66 | {
|
---|
67 | assert(x);
|
---|
68 | assert(y);
|
---|
69 |
|
---|
70 | *x = path->cur_x;
|
---|
71 | *y = path->cur_y;
|
---|
72 | }
|
---|
73 |
|
---|
74 | void path_move_to(path_t *path, double dx, double dy)
|
---|
75 | {
|
---|
76 | path_step_t *step = (path_step_t *) malloc(sizeof(path_step_t));
|
---|
77 | if (step) {
|
---|
78 | path->cur_x += dx;
|
---|
79 | path->cur_y += dy;
|
---|
80 | link_initialize(&step->link);
|
---|
81 | step->type = PATH_STEP_MOVETO;
|
---|
82 | step->to_x = path->cur_x;
|
---|
83 | step->to_y = path->cur_y;
|
---|
84 | list_append(&step->link, &path->list);
|
---|
85 |
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | void path_line_to(path_t *path, double dx, double dy)
|
---|
90 | {
|
---|
91 | path_step_t *step = (path_step_t *) malloc(sizeof(path_step_t));
|
---|
92 | if (step) {
|
---|
93 | path->cur_x += dx;
|
---|
94 | path->cur_y += dy;
|
---|
95 | link_initialize(&step->link);
|
---|
96 | step->type = PATH_STEP_LINETO;
|
---|
97 | step->to_x = path->cur_x;
|
---|
98 | step->to_y = path->cur_y;
|
---|
99 | list_append(&step->link, &path->list);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | void path_rectangle(path_t *path, double x, double y, double width, double height)
|
---|
104 | {
|
---|
105 | path_move_to(path, x, y);
|
---|
106 | path_line_to(path, width, 0);
|
---|
107 | path_line_to(path, 0, height);
|
---|
108 | path_line_to(path, -width, 0);
|
---|
109 | path_line_to(path, 0, -height);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** @}
|
---|
113 | */
|
---|