aboutsummaryrefslogtreecommitdiffstats
path: root/libart_lgpl/art_rect.c
blob: ec53d2d70f0764f2ebd09c05b9a975eeab5120c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Libart_LGPL - library of basic graphic primitives
 * Copyright (C) 1998 Raph Levien
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include "art_rect.h"

#include <math.h>

#ifndef MAX
#define MAX(a, b)  (((a) > (b)) ? (a) : (b))
#endif /* MAX */

#ifndef MIN
#define MIN(a, b)  (((a) < (b)) ? (a) : (b))
#endif /* MIN */

/* rectangle primitives stolen from gzilla */

/**
 * art_irect_intersection: Find intersection of two integer rectangles.
 * @dest: Where the result is stored.
 * @src1: A source rectangle.
 * @src2: Another source rectangle.
 *
 * Finds the intersection of @src1 and @src2.
 **/
void
art_irect_intersect (ArtIRect *dest, const ArtIRect *src1, const ArtIRect *src2) {
  dest->x0 = MAX (src1->x0, src2->x0);
  dest->y0 = MAX (src1->y0, src2->y0);
  dest->x1 = MIN (src1->x1, src2->x1);
  dest->y1 = MIN (src1->y1, src2->y1);
}

/**
 * art_irect_empty: Determine whether integer rectangle is empty.
 * @src: The source rectangle.
 *
 * Return value: TRUE if @src is an empty rectangle, FALSE otherwise.
 **/
gint
art_irect_empty (const ArtIRect *src) {
  return (src->x1 <= src->x0 || src->y1 <= src->y0);
}

/**
 * art_drect_copy: Make a copy of a rectangle.
 * @dest: Where the copy is stored.
 * @src: The source rectangle.
 *
 * Copies the rectangle.
 **/
void
art_drect_copy (ArtDRect *dest, const ArtDRect *src) {
  dest->x0 = src->x0;
  dest->y0 = src->y0;
  dest->x1 = src->x1;
  dest->y1 = src->y1;
}

/**
 * art_drect_union: Find union of two rectangles.
 * @dest: Where the result is stored.
 * @src1: A source rectangle.
 * @src2: Another source rectangle.
 *
 * Finds the smallest rectangle that includes @src1 and @src2.
 **/
void
art_drect_union (ArtDRect *dest, const ArtDRect *src1, const ArtDRect *src2) {
  if (art_drect_empty (src1)) {
    art_drect_copy (dest, src2);
  } else if (art_drect_empty (src2)) {
    art_drect_copy (dest, src1);
  } else {
    dest->x0 = MIN (src1->x0, src2->x0);
    dest->y0 = MIN (src1->y0, src2->y0);
    dest->x1 = MAX (src1->x1, src2->x1);
    dest->y1 = MAX (src1->y1, src2->y1);
  }
}

/**
 * art_irect_empty: Determine whether rectangle is empty.
 * @src: The source rectangle.
 *
 * Return value: TRUE if @src is an empty rectangle, FALSE otherwise.
 **/
gint
art_drect_empty (const ArtDRect *src) {
  return (src->x1 <= src->x0 || src->y1 <= src->y0);
}

/**
 * art_drect_affine_transform: Affine transform rectangle.
 * @dst: Where to store the result.
 * @src: The source rectangle.
 * @matrix: The affine transformation.
 *
 * Find the smallest rectangle enclosing the affine transformed @src.
 * The result is exactly the affine transformation of @src when
 * @matrix specifies a rectilinear affine transformation, otherwise it
 * is a conservative approximation.
 **/
void
art_drect_affine_transform (ArtDRect *dst,
                            const ArtDRect *src,
                            const gdouble matrix[6])
{
  gdouble x00, y00, x10, y10;
  gdouble x01, y01, x11, y11;

  x00 = src->x0 * matrix[0] + src->y0 * matrix[2] + matrix[4];
  y00 = src->x0 * matrix[1] + src->y0 * matrix[3] + matrix[5];
  x10 = src->x1 * matrix[0] + src->y0 * matrix[2] + matrix[4];
  y10 = src->x1 * matrix[1] + src->y0 * matrix[3] + matrix[5];
  x01 = src->x0 * matrix[0] + src->y1 * matrix[2] + matrix[4];
  y01 = src->x0 * matrix[1] + src->y1 * matrix[3] + matrix[5];
  x11 = src->x1 * matrix[0] + src->y1 * matrix[2] + matrix[4];
  y11 = src->x1 * matrix[1] + src->y1 * matrix[3] + matrix[5];
  dst->x0 = MIN (MIN (x00, x10), MIN (x01, x11));
  dst->y0 = MIN (MIN (y00, y10), MIN (y01, y11));
  dst->x1 = MAX (MAX (x00, x10), MAX (x01, x11));
  dst->y1 = MAX (MAX (y00, y10), MAX (y01, y11));
}

/**
 * art_drect_to_irect: Convert rectangle to integer rectangle.
 * @dst: Where to store resulting integer rectangle.
 * @src: The source rectangle.
 *
 * Find the smallest integer rectangle that encloses @src.
 **/
void
art_drect_to_irect (ArtIRect *dst, ArtDRect *src)
{
  dst->x0 = floor (src->x0);
  dst->y0 = floor (src->y0);
  dst->x1 = ceil (src->x1);
  dst->y1 = ceil (src->y1);
}