-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestpage_OCR.js
147 lines (134 loc) · 3.9 KB
/
testpage_OCR.js
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
var OCR = (function() {
var strokes; //an array containing the checkpoints for the current character
var progress; //which checkpoint you just got to
var currentStroke; //which stroke your on
var correct; //if your character is currently correct
var _return = {};
_return.initChar = function(c) {
strokes = strokesForChar[c].split(" ");
currentStroke = 0;
correct = true;
};
_return.startStroke = function() {
console.log('start stroke');
progress = -1;
};
_return.partialStroke = function(x, y) {
console.log('partial stroke');
var cardinal = this.cardinalize(x, y);
this.checkProgress(cardinal);
};
_return.finishStroke = function() {
if (currentStroke < strokes.length && (2 * (progress + 1)) > strokes[currentStroke].length) {
//alert('YAY');
} else {
correct = false;
}
currentStroke++;
};
_return.cardinalize = function(x, y) {
var theta = (y > 0) ? Math.acos(x / Math.sqrt(x * x + y * y)) : 2 * Math.PI - Math.acos(x / Math.sqrt(x * x + y * y));
return (Math.round(theta * 4 / Math.PI)) % 8;
};
_return.checkProgress = function(card) {
if (currentStroke < strokes.length && (2 * (progress + 1)) < strokes[currentStroke].length) {
var checkpoint = parseInt(strokes[currentStroke].charAt(2 * (progress + 1)), 10);
if (card == checkpoint || card == (checkpoint - 1) % 8 || card == (checkpoint + 1) % 8) {
progress++;
}
}
};
_return.isCorrect = function() {
return (currentStroke == strokes.length && correct);
};
/* Cardinal directions:
* 5 6 7
* 4 0
* 3 2 1
*/
/* Each string encodes the strokes that make up a character
* E.g. strokes[4] contains the 3 strokes for hiragana[4] (お)
* Each string is made up of strokes (separated by a space),
* and each stroke is made up of a sequence of cardinal
* directions (represented by numbers).
* E.g., strokes[2] has two strokes, with the first stroke being
* a south-east line, and the second stroke being a curve from
* east to south-west.
* Note: 'o' means a smooth transition (like a curve) and 'v' means sharp transition,
* but I haven't implemented this yet
*/
var strokesForChar = [
'0 2 3o0o3',
'2 1',
'1 0o3',
'1 0v3v7o2o0',
'0 2o0o3', //??
'0o3', //??
'0 0 1 3o7',
'3v1',
'2 0 2o3',
'0 3o0',
'0o3 1 1',
'0 0 1 3o7 1 1',
'3v1 1 1',
'2 0 2o3 1 1',
'0 3o0 1 1',
'0 1 3o7',
'2o7',
'0 2o6o2o3',
'0 2 2o0',
'0 3v4v3o0',
'0 1 3o7 1 1',
'2o7 1 1',
'0 2o6o2o3 1 1',
'0 2 2o0 1 1',
'0 3v4v3o0 1 1',
'0 3 0 3o0',
'0 3v7o4',
'0o4',
'0v3o0',
'2 3o0',
'0 3 0 3o0 1 1',
'0 3v7o4 1 1',
'0o4 1 1',
'0v3o0 1 1',
'2 3o0 1 1',
'0 3 1 2o0o1',
'2 0 3o0',
'2o1 3o0o4o1',
'2 0v3v7o4o1',
'2o0o3',
'2 0 2o0o1',
'0v3o0o6v1',
'1 3o1o5 3 1',
'7v1',
'2 0 0 2o4o1',
'2 0 2o0o1 1 1',
'0v3o0o6v1 1 1',
'1 3o1o5 3 1 1 1',
'7v1 1 1',
'2 0 0 2o4o1 1 1',
'2 0 2o0o1 4o0o4',
'0v3o0o6v1 4o0o4',
'1 3o1o5 3 1 4o0o4',
'7v1 4o0o4',
'2 0 0 2o4o1 4o0o4',
'0 0 2o4o1',
'0v3o7o1 2o3',
'0 2o6o2o0o6 1',
'2o1 3o0o3',
'0 0 2o0o5',
'0o7o2o5 2 1',
'2v6o2o5 2',
'0 2o4o1',
'1 2v7o2o4',
'2 2o3',
'0v3v7o4o1',
'2 0v3v7o2o7',
'0v3v7o4',
'2 0v3v7o2o4',
'0 3v7o2 4o0',
'3v7o2o7'
];
return _return;
})();