source: mainline/uspace/lib/gfx/src/coord.c@ 4d9c807

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4d9c807 was 8b1ce56, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Computing rectangle envelope

Useful for tracking changed areas, for example.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2019 Jiri Svoboda
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 libgfx
30 * @{
31 */
32/**
33 * @file Coordinates
34 */
35
36#include <gfx/coord.h>
37#include <macros.h>
38#include <stdbool.h>
39
40/** Add two vectors.
41 *
42 * @param a First vector
43 * @param b Second vector
44 * @param d Destination
45 */
46void gfx_coord2_add(gfx_coord2_t *a, gfx_coord2_t *b, gfx_coord2_t *d)
47{
48 d->x = a->x + b->x;
49 d->y = a->y + b->y;
50}
51
52/** Subtract two vectors.
53 *
54 * @param a First vector
55 * @param b Second vector
56 * @param d Destination
57 */
58void gfx_coord2_subtract(gfx_coord2_t *a, gfx_coord2_t *b, gfx_coord2_t *d)
59{
60 d->x = a->x - b->x;
61 d->y = a->y - b->y;
62}
63
64/** Sort points of a span.
65 *
66 * Sort the begin and end points so that the begin point has the lower
67 * coordinate (i.e. if needed, the span is transposed, if not, it is simply
68 * copied).
69 *
70 * @param s0 Source span start point
71 * @param s1 Source span end point
72 * @param d0 Destination span start point
73 * @param d1 Destination span end point
74 */
75void gfx_span_points_sort(gfx_coord_t s0, gfx_coord_t s1, gfx_coord_t *d0,
76 gfx_coord_t *d1)
77{
78 if (s0 <= s1) {
79 *d0 = s0;
80 *d1 = s1;
81 } else {
82 *d0 = s1 + 1;
83 *d1 = s0 + 1;
84 }
85}
86
87/** Move (translate) rectangle.
88 *
89 * @param trans Translation
90 * @param src Source rectangle
91 * @param dest Destination rectangle
92 */
93void gfx_rect_translate(gfx_coord2_t *trans, gfx_rect_t *src, gfx_rect_t *dest)
94{
95 gfx_coord2_add(trans, &src->p0, &dest->p0);
96 gfx_coord2_add(trans, &src->p1, &dest->p1);
97}
98
99/** Compute envelope of two rectangles.
100 *
101 * Envelope is the minimal rectangle covering all pixels of both rectangles.
102 */
103void gfx_rect_envelope(gfx_rect_t *a, gfx_rect_t *b, gfx_rect_t *dest)
104{
105 gfx_rect_t sa, sb;
106
107 if (gfx_rect_is_empty(a)) {
108 *dest = *b;
109 return;
110 }
111
112 if (gfx_rect_is_empty(b)) {
113 *dest = *a;
114 return;
115 }
116
117 /* a and b are both non-empty */
118
119 gfx_rect_points_sort(a, &sa);
120 gfx_rect_points_sort(b, &sb);
121
122 dest->p0.x = min(sa.p0.x, sb.p0.x);
123 dest->p0.y = min(sa.p0.y, sb.p0.y);
124 dest->p1.x = max(sa.p1.x, sb.p1.x);
125 dest->p1.y = max(sa.p1.y, sb.p1.y);
126}
127
128/** Sort points of a rectangle.
129 *
130 * Shuffle around coordinates of a rectangle so that p0.x < p1.x and
131 * p0.y < p0.y.
132 *
133 * @param src Source rectangle
134 * @param dest Destination (sorted) rectangle
135 */
136void gfx_rect_points_sort(gfx_rect_t *src, gfx_rect_t *dest)
137{
138 gfx_span_points_sort(src->p0.x, src->p1.x, &dest->p0.x, &dest->p1.x);
139 gfx_span_points_sort(src->p0.y, src->p1.y, &dest->p0.y, &dest->p1.y);
140}
141
142/** Determine if rectangle contains no pixels
143 *
144 * @param rect Rectangle
145 * @return @c true iff rectangle contains no pixels
146 */
147bool gfx_rect_is_empty(gfx_rect_t *rect)
148{
149 return rect->p0.x == rect->p1.x || rect->p0.y == rect->p1.y;
150}
151
152/** @}
153 */
Note: See TracBrowser for help on using the repository browser.