pidgin/plugins/gestures/stroke.c

changeset 39918
1c8e11f9274f
parent 39916
6a79856cb8f5
child 40497
2f45a03838e9
equal deleted inserted replaced
39917:45dbd7582a5a 39918:1c8e11f9274f
20 20
21 21
22 void 22 void
23 _gstroke_init (struct gstroke_metrics *metrics) 23 _gstroke_init (struct gstroke_metrics *metrics)
24 { 24 {
25 if (metrics->pointList != NULL) { 25 if (metrics->pointList != NULL) {
26 /* FIXME: does this free the data too?*/ 26 g_slist_free_full(metrics->pointList, g_free);
27 g_slist_free (metrics->pointList); 27 metrics->pointList = NULL;
28 metrics->pointList = NULL; 28 metrics->point_count = 0;
29 metrics->point_count = 0; 29 }
30 }
31 } 30 }
32 31
33 /* figure out which bin the point falls in */ 32 /* figure out which bin the point falls in */
34 static gint 33 static gint
35 _gstroke_bin (p_point point_p, gint bound_x_1, gint bound_x_2, 34 _gstroke_bin (p_point point_p, gint bound_x_1, gint bound_x_2,
195 #if 0 194 #if 0
196 printf ("%d:%d ", x, y); fflush (stdout); 195 printf ("%d:%d ", x, y); fflush (stdout);
197 #endif 196 #endif
198 197
199 if (metrics->point_count < GSTROKE_MAX_POINTS) { 198 if (metrics->point_count < GSTROKE_MAX_POINTS) {
200 new_point_p = g_malloc(sizeof (struct s_point));
201
202 if (metrics->pointList == NULL) { 199 if (metrics->pointList == NULL) {
203 200
204 /* first point in list - initialize metrics */ 201 /* first point in list - initialize metrics */
205 metrics->min_x = 10000; 202 metrics->min_x = 10000;
206 metrics->min_y = 10000; 203 metrics->min_y = 10000;
207 metrics->max_x = -1; 204 metrics->max_x = -1;
208 metrics->max_y = -1; 205 metrics->max_y = -1;
209 206
207 new_point_p = g_new0(struct s_point, 1);
210 metrics->pointList = g_slist_prepend(metrics->pointList, new_point_p); 208 metrics->pointList = g_slist_prepend(metrics->pointList, new_point_p);
211 metrics->point_count = 0; 209 metrics->point_count = 0;
212 210
213 } else { 211 } else {
214 212 p_point last_point = (p_point)g_slist_last(metrics->pointList)->data;
215 #define LAST_POINT ((p_point)(g_slist_last (metrics->pointList)->data))
216 213
217 /* interpolate between last and current point */ 214 /* interpolate between last and current point */
218 delx = x - LAST_POINT->x; 215 delx = x - last_point->x;
219 dely = y - LAST_POINT->y; 216 dely = y - last_point->y;
220 217
221 if (abs(delx) > abs(dely)) { /* step by the greatest delta direction */ 218 if (abs(delx) > abs(dely)) { /* step by the greatest delta direction */
222 iy = LAST_POINT->y; 219 iy = last_point->y;
223 220
224 /* go from the last point to the current, whatever direction it may be */ 221 /* go from the last point to the current, whatever direction it may be */
225 for (ix = LAST_POINT->x; (delx > 0) ? (ix < x) : (ix > x); ix += (delx > 0) ? 1 : -1) { 222 for (ix = last_point->x; (delx > 0) ? (ix < x) : (ix > x); ix += (delx > 0) ? 1 : -1) {
226 223
227 /* step the other axis by the correct increment */ 224 /* step the other axis by the correct increment */
228 iy += fabs(((float) dely / (float) delx)) * (float) ((dely < 0) ? -1.0 : 1.0); 225 iy += fabs(((float) dely / (float) delx)) * (float) ((dely < 0) ? -1.0 : 1.0);
229 226
230 /* add the interpolated point */ 227 /* add the interpolated point */
228 new_point_p = g_new0(struct s_point, 1);
231 new_point_p->x = ix; 229 new_point_p->x = ix;
232 new_point_p->y = iy; 230 new_point_p->y = iy;
233 metrics->pointList = g_slist_append (metrics->pointList, new_point_p); 231 metrics->pointList = g_slist_append (metrics->pointList, new_point_p);
234 232
235 /* update metrics */ 233 /* update metrics */
237 if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix; 235 if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix;
238 if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy; 236 if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy;
239 if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy; 237 if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy;
240 metrics->point_count++; 238 metrics->point_count++;
241 239
242 new_point_p = g_malloc(sizeof(struct s_point));
243 } 240 }
244 } else { /* same thing, but for dely larger than delx case... */ 241 } else { /* same thing, but for dely larger than delx case... */
245 ix = LAST_POINT->x; 242 p_point last_point = (p_point)g_slist_last(metrics->pointList)->data;
243
244 ix = last_point->x;
246 245
247 /* go from the last point to the current, whatever direction it may be 246 /* go from the last point to the current, whatever direction it may be
248 */ 247 */
249 for (iy = LAST_POINT->y; (dely > 0) ? (iy < y) : (iy > y); iy += (dely > 0) ? 1 : -1) { 248 for (iy = last_point->y; (dely > 0) ? (iy < y) : (iy > y); iy += (dely > 0) ? 1 : -1) {
250 249
251 /* step the other axis by the correct increment */ 250 /* step the other axis by the correct increment */
252 ix += fabs(((float) delx / (float) dely)) * (float) ((delx < 0) ? -1.0 : 1.0); 251 ix += fabs(((float) delx / (float) dely)) * (float) ((delx < 0) ? -1.0 : 1.0);
253 252
254 /* add the interpolated point */ 253 /* add the interpolated point */
254 new_point_p = g_new0(struct s_point, 1);
255 new_point_p->y = iy; 255 new_point_p->y = iy;
256 new_point_p->x = ix; 256 new_point_p->x = ix;
257 metrics->pointList = g_slist_append(metrics->pointList, new_point_p); 257 metrics->pointList = g_slist_append(metrics->pointList, new_point_p);
258 258
259 /* update metrics */ 259 /* update metrics */
260 if (((gint) ix) < metrics->min_x) metrics->min_x = (gint) ix; 260 if (((gint) ix) < metrics->min_x) metrics->min_x = (gint) ix;
261 if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix; 261 if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix;
262 if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy; 262 if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy;
263 if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy; 263 if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy;
264 metrics->point_count++; 264 metrics->point_count++;
265
266 new_point_p = g_malloc(sizeof(struct s_point));
267 } 265 }
268 } 266 }
269 267
270 /* add the sampled point */ 268 /* add the sampled point */
269 new_point_p = g_new0(struct s_point, 1);
271 metrics->pointList = g_slist_append(metrics->pointList, new_point_p); 270 metrics->pointList = g_slist_append(metrics->pointList, new_point_p);
272 } 271 }
273 272
274 /* record the sampled point values */ 273 /* record the sampled point values */
275 new_point_p->x = x; 274 new_point_p->x = x;

mercurial