Changeset 8b1ce56 in mainline for uspace/lib/gfx/src/coord.c


Ignore:
Timestamp:
2019-11-21T13:45:43Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
670cfcf
Parents:
bf22cb78
git-author:
Jiri Svoboda <jiri@…> (2019-11-20 18:45:40)
git-committer:
Jiri Svoboda <jiri@…> (2019-11-21 13:45:43)
Message:

Computing rectangle envelope

Useful for tracking changed areas, for example.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/gfx/src/coord.c

    rbf22cb78 r8b1ce56  
    3535
    3636#include <gfx/coord.h>
     37#include <macros.h>
     38#include <stdbool.h>
    3739
    3840/** Add two vectors.
     
    6062}
    6163
     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
    6287/** Move (translate) rectangle.
    6388 *
     
    7297}
    7398
     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
    74152/** @}
    75153 */
Note: See TracChangeset for help on using the changeset viewer.