1 /* rec_history.c -- average position, size, and orientation of recent strokes
2 
3    Copyright 2001 Carl Worth
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <math.h>
19 
20 #define M_PI_16 (M_PI_4 / 4.0)
21 
22 #include "rec_history.h"
23 
24 #ifdef DMALLOC
25 #include "dmalloc.h"
26 #endif
27 
rec_history_init(rec_history_t * history,int x,int y,int width,int height,double orientation)28 int rec_history_init(rec_history_t *history, int x, int y,
29 		     int width, int height,
30 		     double orientation)
31 {
32     int i;
33 
34     history->x = x;
35     history->y = y;
36     for (i=0; i < RH_POS_SAMPLES; i++) {
37 	history->x_history[i] = x;
38 	history->y_history[i] = y;
39     }
40 
41     history->width = width;
42     history->height = height;
43     for (i=0; i < RH_SIZE_SAMPLES; i++) {
44 	history->width_history[i] = width;
45 	history->height_history[i] = height;
46     }
47 
48 
49     history->orientation = orientation;
50     for (i=0; i < RH_ORIENTATION_SAMPLES; i++) {
51 	history->orientation_history[i] = orientation;
52     }
53 
54     return 0;
55 }
56 
rec_history_deinit(rec_history_t * history)57 void rec_history_deinit(rec_history_t *history)
58 {
59     /* nothing to see here, move along... */
60 }
61 
rec_history_update_position(rec_history_t * history,int x,int y)62 void rec_history_update_position(rec_history_t *history, int x, int y)
63 {
64     int i;
65 
66     for (i=0; i < RH_POS_SAMPLES - 1; i++) {
67 	history->x_history[i] = history->x_history[i+1];
68 	history->y_history[i] = history->y_history[i+1];
69     }
70     history->x_history[i] = x;
71     history->y_history[i] = y;
72 
73     history->x = 0;
74     history->y = 0;
75     for (i=0; i < RH_POS_SAMPLES; i++) {
76 	history->x += history->x_history[i];
77 	history->y += history->y_history[i];
78     }
79     history->x /= RH_POS_SAMPLES;
80     history->y /= RH_POS_SAMPLES;
81 }
82 
rec_history_update_size(rec_history_t * history,int width,int height)83 void rec_history_update_size(rec_history_t *history, int width, int height)
84 {
85     int i;
86 
87     for (i=0; i < RH_SIZE_SAMPLES - 1; i++) {
88 	history->width_history[i] = history->width_history[i+1];
89 	history->height_history[i] = history->height_history[i+1];
90     }
91     history->width_history[i] = width;
92     history->height_history[i] = height;
93 
94     history->width = 0;
95     history->height = 0;
96     for (i=0; i < RH_SIZE_SAMPLES; i++) {
97 	history->width += history->width_history[i];
98 	history->height += history->height_history[i];
99     }
100     history->width /= RH_SIZE_SAMPLES;
101     history->height /= RH_SIZE_SAMPLES;
102 }
103 
rec_history_nudge_orientation(rec_history_t * history,double correction)104 void rec_history_nudge_orientation(rec_history_t *history, double correction)
105 {
106     int i;
107 
108     for (i=0; i < RH_ORIENTATION_SAMPLES - 1; i++) {
109 	history->orientation_history[i] = history->orientation_history[i+1];
110     }
111     history->orientation_history[i] = history->orientation + correction;
112 
113     history->orientation = 0.0;
114     for (i=0; i < RH_ORIENTATION_SAMPLES; i++) {
115 	history->orientation += history->orientation_history[i];
116     }
117     history->orientation /= (double) RH_ORIENTATION_SAMPLES;
118 }
119