[6d5e378] | 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 <bool.h>
|
---|
| 38 | #include <malloc.h>
|
---|
| 39 |
|
---|
| 40 | #include "path.h"
|
---|
| 41 |
|
---|
| 42 | struct path {
|
---|
| 43 | list_t list;
|
---|
| 44 | double cur_x;
|
---|
| 45 | double cur_y;
|
---|
| 46 | };
|
---|
| 47 |
|
---|
| 48 | void path_init(path_t *path)
|
---|
| 49 | {
|
---|
| 50 | list_initialize(&path->list);
|
---|
| 51 | path->cur_x = 0;
|
---|
| 52 | path->cur_y = 0;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | void path_clear(path_t *path)
|
---|
| 56 | {
|
---|
| 57 | while (!list_empty(&path->list)) {
|
---|
| 58 | path_step_t *step = (path_step_t *) list_last(&path->list);
|
---|
| 59 | list_remove(&step->link);
|
---|
| 60 | free(step);
|
---|
| 61 | }
|
---|
| 62 | path->cur_x = 0;
|
---|
| 63 | path->cur_y = 0;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | void path_get_cursor(path_t *path, double *x, double *y)
|
---|
| 67 | {
|
---|
| 68 | assert(x);
|
---|
| 69 | assert(y);
|
---|
| 70 |
|
---|
| 71 | *x = path->cur_x;
|
---|
| 72 | *y = path->cur_y;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | void path_move_to(path_t *path, double dx, double dy)
|
---|
| 76 | {
|
---|
| 77 | path_step_t *step = (path_step_t *) malloc(sizeof(path_step_t));
|
---|
| 78 | if (step) {
|
---|
| 79 | path->cur_x += dx;
|
---|
| 80 | path->cur_y += dy;
|
---|
| 81 | link_initialize(&step->link);
|
---|
| 82 | step->type = PATH_STEP_MOVETO;
|
---|
| 83 | step->to_x = path->cur_x;
|
---|
| 84 | step->to_y = path->cur_y;
|
---|
| 85 | list_append(&step->link, &path->list);
|
---|
| 86 |
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | void path_line_to(path_t *path, double dx, double dy)
|
---|
| 91 | {
|
---|
| 92 | path_step_t *step = (path_step_t *) malloc(sizeof(path_step_t));
|
---|
| 93 | if (step) {
|
---|
| 94 | path->cur_x += dx;
|
---|
| 95 | path->cur_y += dy;
|
---|
| 96 | link_initialize(&step->link);
|
---|
| 97 | step->type = PATH_STEP_LINETO;
|
---|
| 98 | step->to_x = path->cur_x;
|
---|
| 99 | step->to_y = path->cur_y;
|
---|
| 100 | list_append(&step->link, &path->list);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | void path_rectangle(path_t *path, double x, double y, double width, double height)
|
---|
| 105 | {
|
---|
| 106 | path_move_to(path, x, y);
|
---|
| 107 | path_line_to(path, width, 0);
|
---|
| 108 | path_line_to(path, 0, height);
|
---|
| 109 | path_line_to(path, -width, 0);
|
---|
| 110 | path_line_to(path, 0, -height);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /** @}
|
---|
| 114 | */
|
---|