comparison src/geo.c @ 5:9c331ec9e210

SVG path is partially supported
author Thinker K.F. Li <thinker@branda.to>
date Sat, 26 Jul 2008 01:37:35 +0800
parents
children 7cfecdce94cc
comparison
equal deleted inserted replaced
4:399517bf65dc 5:9c331ec9e210
1 /*! \brief Determine who should be re-drawed.
2 * \file
3 * When part of graphic are chagned, not mater size, shape, or position,
4 * the components effected or overlaid should be re-drawed. This module
5 * figures out components that should be re-drawed.
6 */
7 #include <stdio.h>
8 #include "mb_types.h"
9
10 struct _geo {
11 co_aix x, y;
12 co_aix w, h;
13 co_aix rel_x, rel_y;
14 co_aix orig_x, orig_y;
15 int n_subs;
16 struct _geo *subs;
17 };
18
19 static int is_pos_in(co_aix x, co_aix y,
20 co_aix rectx, co_aix recty,
21 co_aix rectw, co_aix recth) {
22 co_aix rel_x, rel_y;
23
24 rel_x = x - rectx;
25 if(rel_x < 0 || rel_x >= rectw)
26 return 0;
27 rel_y = y - recty;
28 if(rel_y < 0 || rel_y >= recth)
29 return 0;
30 return 1;
31 }
32
33 static int is_scale_overlay(co_aix x1, co_aix w1, co_aix x2, co_aix w2) {
34 if(x1 > x2) {
35 if((x1 - x2) >= w2)
36 return 0;
37 } else {
38 if((x2 - x1) >= w1)
39 return 0;
40 }
41 return 1;
42 }
43
44 static int is_overlay(geo_t *r1, geo_t *r2) {
45 co_aix rel_x, rel_y;
46
47 if(!is_scale_overlay(r1->x, r1->w, r2->x, r2->w))
48 return 0;
49 if(!is_scale_overlay(r1->y, r1->h, r2->y, r2->h))
50 return 0;
51
52 return 1;
53 }
54
55 void geo_init(geo_t *g, int n_pos, co_aix pos[][2]) {
56 co_aix min_x, max_x;
57 co_aix min_y, max_y;
58 co_aix x, y;
59 int i;
60
61 min_x = max_x = pos[0][0];
62 min_y = max_y = pos[0][1];
63 for(i = 1; i < n_pos; i++) {
64 x = pos[i][0];
65 if(x < min_x)
66 min_x = x;
67 else if(x > max_x)
68 max_x = x;
69 y = pos[i][1];
70 if(y < min_y)
71 min_y = y;
72 else if(y > max_y)
73 max_y = y;
74 }
75 g->x = min_x;
76 g->w = max_x - min_x;
77 g->y = min_y;
78 g->h = max_y - min_y;
79 }
80
81 void geo_mark_overlay(geo_t *g, int n_others, geo_t **others,
82 int *n_overlays, geo_t **overlays) {
83 int i, ov_idx;
84
85 ov_idx = 0;
86 for(i = 0; i < n_others; i++) {
87 if(is_overlay(g, others[i]))
88 overlays[ov_idx++] = others[i];
89 }
90 *n_overlays = ov_idx;
91 }
92
93
94 #ifdef UNITTEST
95
96 #include <CUnit/Basic.h>
97
98 void test_geo_init(void) {
99 co_aix data[][2] = {
100 {33, 25}, {49, 12},
101 {14, 28}, {39, 56}};
102 geo_t g;
103
104 geo_init(&g, 4, data);
105 CU_ASSERT(g.x == 14);
106 CU_ASSERT(g.w == 35);
107 CU_ASSERT(g.y == 12);
108 CU_ASSERT(g.h == 44);
109 }
110
111 void test_geo_mark_overlay(void) {
112 geo_t _geos[3], *geos[3], *overlays[3];
113 geo_t g;
114 int i, n_ov;
115
116 for(i = 0; i < 3; i++) {
117 _geos[i].x = i * 50;
118 _geos[i].y = i * 50;
119 _geos[i].w = 55;
120 _geos[i].h = 66;
121 geos[i] = _geos + i;
122 }
123 g.x = 88;
124 g.y = 79;
125 g.w = 70;
126 g.h = 70;
127
128 /* overlay with geos[1] and geos[2] */
129 geo_mark_overlay(&g, 3, geos, &n_ov, overlays);
130 CU_ASSERT(n_ov == 2);
131 CU_ASSERT(overlays[0] == geos[1]);
132 CU_ASSERT(overlays[1] == geos[2]);
133
134 /* right side of geos[1], and up side of geos[2] */
135 g.x = 105;
136 g.y = 50;
137 g.w = 50;
138 g.h = 51;
139 geo_mark_overlay(&g, 3, geos, &n_ov, overlays);
140 CU_ASSERT(n_ov == 1);
141 CU_ASSERT(overlays[0] == geos[2]);
142 }
143
144 CU_pSuite get_geo_suite(void) {
145 CU_pSuite suite;
146
147 suite = CU_add_suite("Suite_geo", NULL, NULL);
148 CU_ADD_TEST(suite, test_geo_init);
149 CU_ADD_TEST(suite, test_geo_mark_overlay);
150
151 return suite;
152 }
153
154 #endif