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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
|
implement Strokes;
#
# this Limbo code is derived from C code that had the following
# copyright notice, which i reproduce as requested
#
# li_recognizer.c
#
# Copyright 2000 Compaq Computer Corporation.
# Copying or modifying this code for any purpose is permitted,
# provided that this copyright notice is preserved in its entirety
# in all copies or modifications.
# COMPAQ COMPUTER CORPORATION MAKES NO WARRANTIES, EXPRESSED OR
# IMPLIED, AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR
#
#
# Adapted from cmu_recognizer.c by Jay Kistler.
#
# Where is the CMU copyright???? Gotta track it down - Jim Gettys
#
# Credit to Dean Rubine, Jim Kempf, and Ari Rapkin.
#
#
# the copyright notice really did end in the middle of the sentence
#
#
# Limbo version for Inferno by forsyth@vitanuova.com, Vita Nuova, September 2001
#
#
# the code is reasonably close to the algorithms described in
# ``On-line Handwritten Alphanumeric Character Recognition Using Dominant Stroke in Strokes'',
# Xiaolin Li and Dit-Yan Yueng, Department of Computer Science,
# Hong Kong University of Science and Technology, Hong Kong (23 August 1996)
#
include "sys.m";
sys: Sys;
include "strokes.m";
MAXINT: con 16r7FFFFFFF;
# Dynamic programming parameters
DP_BAND: con 3;
SIM_THLD: con 60; # x100
#DIST_THLD: con 3200; # x100
DIST_THLD: con 3300; # x100
# Low-pass filter parameters -- empirically derived
LP_FILTER_WIDTH: con 6;
LP_FILTER_ITERS: con 8;
LP_FILTER_THLD: con 250; # x100
LP_FILTER_MIN: con 5;
# Pseudo-extrema parameters -- empirically derived
PE_AL_THLD: con 1500; # x100
PE_ATCR_THLD: con 135; # x100
# Pre-processing and canonicalization parameters
CANONICAL_X: con 108;
CANONICAL_Y: con 128;
DIST_SQ_THRESHOLD: con 3*3;
# direction-code table; indexed by dx, dy
dctbl := array[] of {array[] of {1, 0, 7}, array[] of {2, MAXINT, 6}, array[] of {3, 4, 5}};
# low-pass filter weights
lpfwts := array[2 * LP_FILTER_WIDTH + 1] of int;
lpfconst := -1;
lidebug: con 0;
stderr: ref Sys->FD;
# x := 0.04 * (i * i);
# wtvals[i] = floor(100.0 * exp(x));
wtvals := array[] of {100, 104, 117, 143, 189, 271, 422};
init()
{
sys = load Sys Sys->PATH;
if(lidebug)
stderr = sys->fildes(2);
for(i := LP_FILTER_WIDTH; i >= 0; i--){
wt := wtvals[i];
lpfwts[LP_FILTER_WIDTH - i] = wt;
lpfwts[LP_FILTER_WIDTH + i] = wt;
}
lpfconst = 0;
for(i = 0; i < (2 * LP_FILTER_WIDTH + 1); i++)
lpfconst += lpfwts[i];
}
Stroke.new(n: int): ref Stroke
{
return ref Stroke(n, array[n] of Penpoint, 0, 0);
}
Stroke.trim(ps: self ref Stroke, n: int)
{
ps.npts = n;
ps.pts = ps.pts[0:n];
}
Stroke.copy(ps: self ref Stroke): ref Stroke
{
n := ps.npts;
a := array[n] of Penpoint;
a[0:] = ps.pts[0:n];
return ref Stroke(n, a, ps.xrange, ps.yrange);
}
#
# return the bounding box of a set of points
# (note: unlike Draw->Rectangle, the region is closed)
#
Stroke.bbox(ps: self ref Stroke): (int, int, int, int)
{
minx := maxx := ps.pts[0].x;
miny := maxy := ps.pts[0].y;
for(i := 1; i < ps.npts; i++){
pt := ps.pts[i];
if(pt.x < minx)
minx = pt.x;
if(pt.x > maxx)
maxx = pt.x;
if(pt.y < miny)
miny = pt.y;
if(pt.y > maxy)
maxy = pt.y;
}
return (minx, miny, maxx, maxy); # warning: closed interval
}
Stroke.center(ps: self ref Stroke)
{
(minx, miny, maxx, maxy) := ps.bbox();
ps.xrange = maxx-minx;
ps.yrange = maxy-miny;
avgxoff := -((CANONICAL_X - ps.xrange + 1) / 2);
avgyoff := -((CANONICAL_Y - ps.yrange + 1) / 2);
ps.translate(avgxoff, avgyoff, 100, 100);
}
Stroke.scaleup(ps: self ref Stroke): int
{
(minx, miny, maxx, maxy) := ps.bbox();
xrange := maxx - minx;
yrange := maxy - miny;
scale: int;
if(((100 * xrange + CANONICAL_X / 2) / CANONICAL_X) >
((100 * yrange + CANONICAL_Y / 2) / CANONICAL_Y))
scale = (100 * CANONICAL_X + xrange / 2) / xrange;
else
scale = (100 * CANONICAL_Y + yrange / 2) / yrange;
ps.translate(minx, miny, scale, scale);
return scale;
}
# scalex and scaley are x 100.
# Note that this does NOT update points.xrange and points.yrange!
Stroke.translate(ps: self ref Stroke, minx: int, miny: int, scalex: int, scaley: int)
{
for(i := 0; i < ps.npts; i++){
ps.pts[i].x = ((ps.pts[i].x - minx) * scalex + 50) / 100;
ps.pts[i].y = ((ps.pts[i].y - miny) * scaley + 50) / 100;
}
}
TAP_PATHLEN: con 10*100; # x100
Classifier.match(rec: self ref Classifier, stroke: ref Stroke): (int, string)
{
if(stroke.npts < 1)
return (-1, nil);
# Check for tap.
# First thing is to filter out ``close points.''
stroke = stroke.filter();
# Unfortunately, we don't have the actual time that each point
# was recorded (i.e., dt is invalid). Hence, we have to use a
# heuristic based on total distance and the number of points.
if(stroke.npts == 1 || stroke.length() < TAP_PATHLEN)
return (-1, "tap");
preprocess_stroke(stroke);
# Compute its dominant points.
dompts := stroke.interpolate().dominant();
best_dist := MAXDIST;
best_i := -1;
best_name: string;
# Score input stroke against every class in classifier.
for(i := 0; i < len rec.cnames; i++){
name := rec.cnames[i];
(sim, dist) := score_stroke(dompts, rec.dompts[i]);
if(dist < MAXDIST)
sys->fprint(stderr, " (%s, %d, %d)", name, sim, dist);
if(dist < DIST_THLD){
if(lidebug)
sys->fprint(stderr, " (%s, %d, %d)", name, sim, dist);
# Is it the best so far?
if(dist < best_dist){
best_dist = dist;
best_i = i;
best_name = name;
}
}
}
if(lidebug)
sys->fprint(stderr, "\n");
return (best_i, best_name);
}
preprocess_stroke(s: ref Stroke)
{
# Filter out points that are too close.
# We did this earlier, when we checked for a tap.
# s = s.filter();
# assert(s.npts > 0);
# Scale up to avoid conversion errors.
s.scaleup();
# Center the stroke.
s.center();
if(lidebug){
(minx, miny, maxx, maxy) := s.bbox();
sys->fprint(stderr, "After pre-processing: [ %d %d %d %d]\n",
minx, miny, maxx, maxy);
printpoints(stderr, s, "\n");
}
}
#
# return the dominant points of Stroke s, assuming s has been through interpolation
#
Stroke.dominant(s: self ref Stroke): ref Stroke
{
regions := s.regions();
# Dominant points are: start, end, extrema of non plain regions, and midpoints of the preceding.
nonplain := 0;
for(r := regions; r != nil; r = r.next)
if(r.rtype != Rplain)
nonplain++;
dom := Stroke.new(1 + 2*nonplain + 2);
# Pick out dominant points.
# start point
dp := 0;
previx := 0;
dom.pts[dp++] = s.pts[previx];
currix: int;
cas := s.contourangles(regions);
for(r = regions; r != nil; r = r.next)
if(r.rtype != Rplain){
max_v := 0;
min_v := MAXINT;
max_ix := -1;
min_ix := -1;
for(i := r.start; i <= r.end; i++){
v := cas[i];
if(v > max_v){
max_v = v; max_ix = i;
}
if(v < min_v){
min_v = v; min_ix = i;
}
if(lidebug > 1)
sys->fprint(stderr, " %d\n", v);
}
if(r.rtype == Rconvex)
currix = max_ix;
else
currix = min_ix;
dom.pts[dp++] = s.pts[(previx+currix)/2]; # midpoint
dom.pts[dp++] = s.pts[currix]; # extreme
previx = currix;
}
# last midpoint, and end point
lastp := s.npts - 1;
dom.pts[dp++] = s.pts[(previx+lastp)/2];
dom.pts[dp++] = s.pts[lastp];
dom.trim(dp);
# Compute chain-code.
compute_chain_code(dom);
return dom;
}
Stroke.contourangles(s: self ref Stroke, regions: ref Region): array of int
{
V := array[s.npts] of int;
V[0] = 18000;
for(r := regions; r != nil; r = r.next){
for(i := r.start; i <= r.end; i++){
if(r.rtype == Rplain){
V[i] = 18000;
}else{
# For now, simply choose the mid-point.
ismidpt := i == (r.start + r.end)/2;
if(ismidpt ^ (r.rtype!=Rconvex))
V[i] = 18000;
else
V[i] = 0;
}
}
}
V[s.npts - 1] = 18000;
return V;
}
Stroke.interpolate(s: self ref Stroke): ref Stroke
{
# Compute an upper-bound on the number of interpolated points
maxpts := s.npts;
for(i := 0; i < s.npts - 1; i++){
a := s.pts[i];
b := s.pts[i+1];
maxpts += abs(a.x - b.x) + abs(a.y - b.y);
}
# Allocate an array of the maximum size
newpts := Stroke.new(maxpts);
# Interpolate each of the segments.
j := 0;
for(i = 0; i < s.npts - 1; i++){
j = bresline(s.pts[i], s.pts[i+1], newpts, j);
j--; # end point gets recorded as start point of next segment!
}
# Add-in last point and trim
newpts.pts[j++] = s.pts[s.npts - 1];
newpts.trim(j);
if(lidebug){
sys->fprint(stderr, "After interpolation:\n");
printpoints(stderr, newpts, "\n");
}
# Compute the chain code for P (the list of points).
compute_unit_chain_code(newpts);
return newpts;
}
# This implementation is due to an anonymous page on the net
bresline(startpt: Penpoint, endpt: Penpoint, newpts: ref Stroke, j: int): int
{
x0 := startpt.x;
x1 := endpt.x;
y0 := startpt.y;
y1 := endpt.y;
stepx := 1;
dx := x1-x0;
if(dx < 0){
dx = -dx;
stepx = -1;
}
dx <<= 1;
stepy := 1;
dy := y1-y0;
if(dy < 0){
dy = -dy;
stepy = -1;
}
dy <<= 1;
newpts.pts[j++] = (x0, y0, 0);
if(dx >= dy){
e := dy - (dx>>1);
while(x0 != x1){
if(e >= 0){
y0 += stepy;
e -= dx;
}
x0 += stepx;
e += dy;
newpts.pts[j++] = (x0, y0, 0);
}
}else{
e := dx - (dy>>1);
while(y0 != y1){
if(e >= 0){
x0 += stepx;
e -= dy;
}
y0 += stepy;
e += dx;
newpts.pts[j++] = (x0, y0, 0);
}
}
return j;
}
compute_chain_code(pts: ref Stroke)
{
for(i := 0; i < pts.npts - 1; i++){
dx := pts.pts[i+1].x - pts.pts[i].x;
dy := pts.pts[i+1].y - pts.pts[i].y;
pts.pts[i].chaincode = (12 - quadr(likeatan(dy, dx))) % 8;
}
}
compute_unit_chain_code(pts: ref Stroke)
{
for(i := 0; i < pts.npts - 1; i++){
dx := pts.pts[i+1].x - pts.pts[i].x;
dy := pts.pts[i+1].y - pts.pts[i].y;
pts.pts[i].chaincode = dctbl[dx+1][dy+1];
}
}
Stroke.regions(pts: self ref Stroke): ref Region
{
# Allocate a 2 x pts.npts array for use in computing the (filtered) Angle set, A_n.
R := array[] of {0 to LP_FILTER_ITERS+1 => array[pts.npts] of int};
curr := R[0];
# Compute the Angle set, A, in the first element of array R.
# Values in R are in degrees, x 100.
curr[0] = 18000; # a_0
for(i := 1; i < pts.npts - 1; i++){
d_i := pts.pts[i].chaincode;
d_iminusone := pts.pts[i-1].chaincode;
if(d_iminusone < d_i)
d_iminusone += 8;
a_i := (d_iminusone - d_i) % 8;
# convert to degrees, x 100
curr[i] = ((12 - a_i) % 8) * 45 * 100;
}
curr[pts.npts-1] = 18000; # a_L-1
# Perform a number of filtering iterations.
next := R[1];
for(j := 0; j < LP_FILTER_ITERS; ){
for(i = 0; i < pts.npts; i++){
next[i] = 0;
for(k := i - LP_FILTER_WIDTH; k <= i + LP_FILTER_WIDTH; k++){
oldval: int;
if(k < 0 || k >= pts.npts)
oldval = 18000;
else
oldval = curr[k];
next[i] += oldval * lpfwts[k - (i - LP_FILTER_WIDTH)]; # overflow?
}
next[i] /= lpfconst;
}
j++;
curr = R[j];
next = R[j+1];
}
# Do final thresholding around PI.
# curr and next are set-up correctly at end of previous loop!
for(i = 0; i < pts.npts; i++)
if(abs(curr[i] - 18000) < LP_FILTER_THLD)
next[i] = 18000;
else
next[i] = curr[i];
curr = next;
# Debugging.
if(lidebug > 1){
for(i = 0; i < pts.npts; i++){
p := pts.pts[i];
sys->fprint(stderr, "%3d: (%d %d) %ud ",
i, p.x, p.y, p.chaincode);
for(j = 0; j < 2 + LP_FILTER_ITERS; j++)
sys->fprint(stderr, "%d ", R[j][i]);
sys->fprint(stderr, "\n");
}
}
# Do the region segmentation.
r := regions := ref Region(regiontype(curr[0]), 0, 0, nil);
for(i = 1; i < pts.npts; i++){
t := regiontype(curr[i]);
if(t != r.rtype){
r.end = i-1;
if(lidebug > 1)
sys->fprint(stderr, " (%d, %d) %d\n", r.start, r.end, r.rtype);
r.next = ref Region(t, i, 0, nil);
r = r.next;
}
}
r.end = i-1;
if(lidebug > 1)
sys->fprint(stderr, " (%d, %d), %d\n", r.start, r.end, r.rtype);
# Filter out convex/concave regions that are too short.
for(r = regions; r != nil; r = r.next)
if(r.rtype == Rplain){
while((nr := r.next) != nil && (nr.end - nr.start) < LP_FILTER_MIN){
# nr must not be plain, and it must be followed by a plain
# assert(nr.rtype != Rplain);
# assert(nr.next != nil && (nr.next).rtype == Rplain);
if(nr.next == nil){
sys->fprint(stderr, "recog: nr.next==nil\n"); # can't happen
break;
}
r.next = nr.next.next;
r.end = nr.next.end;
}
}
# Add-in pseudo-extremes.
for(r = regions; r != nil; r = r.next)
if(r.rtype == Rplain){
arclen := pts.pathlen(r.start, r.end);
dx := pts.pts[r.end].x - pts.pts[r.start].x;
dy := pts.pts[r.end].y - pts.pts[r.start].y;
chordlen := sqrt(100*100 * (dx*dx + dy*dy));
atcr := 0;
if(chordlen)
atcr = (100*arclen + chordlen/2) / chordlen;
if(lidebug)
sys->fprint(stderr, "%d, %d, %d\n", arclen, chordlen, atcr);
# Split region if necessary.
if(arclen >= PE_AL_THLD && atcr >= PE_ATCR_THLD){
mid := (r.start + r.end)/2;
end := r.end;
r.end = mid - 1;
r = r.next = ref Region(Rpseudo, mid, mid,
ref Region(Rplain, mid+1, end, r.next));
}
}
return regions;
}
regiontype(val: int): int
{
if(val == 18000)
return Rplain;
if(val < 18000)
return Rconcave;
return Rconvex;
}
#
# return the similarity of two strokes and,
# if similar, the distance between them;
# if dissimilar, the distance is MAXDIST)
#
score_stroke(a: ref Stroke, b: ref Stroke): (int, int)
{
sim := compute_similarity(a, b);
if(sim < SIM_THLD)
return (sim, MAXDIST);
return (sim, compute_distance(a, b));
}
compute_similarity(A: ref Stroke, B: ref Stroke): int
{
# A is the longer sequence, length N.
# B is the shorter sequence, length M.
if(A.npts < B.npts){
t := A; A = B; B = t;
}
N := A.npts;
M := B.npts;
# Allocate and initialize the Gain matrix, G.
# The size of G is M x (N + 1).
# Note that row 0 is unused.
# Similarities are x 10.
G := array[M] of array of int;
for(i := 1; i < M; i++){
G[i] = array[N+1] of int;
bcode := B.pts[i-1].chaincode;
G[i][0] = 0; # source column
for(j := 1; j < N; j++){
diff := abs(bcode - A.pts[j-1].chaincode);
if(diff > 4)
diff = 8 - diff; # symmetry
v := 0;
if(diff == 0)
v = 10;
else if(diff == 1)
v = 6;
G[i][j] = v;
}
G[i][N] = 0; # sink column
}
# Do the DP algorithm.
# Proceed in column order, from highest column to the lowest.
# Within each column, proceed from the highest row to the lowest.
# Skip the highest column.
for(j := N - 1; j >= 0; j--)
for(i = M - 1; i > 0; i--){
max := G[i][j + 1];
if(i < M-1){
t := G[i + 1][j + 1];
if(t > max)
max = t;
}
G[i][j] += max;
}
return (10*G[1][0] + (N-1)/2) / (N-1);
}
compute_distance(A: ref Stroke, B: ref Stroke): int
{
# A is the longer sequence, length N.
# B is the shorter sequence, length M.
if(A.npts < B.npts){
t := A; A = B; B = t;
}
N := A.npts;
M := B.npts;
# Construct the helper vectors, BE and TE, which say for each column
# what are the ``bottom'' and ``top'' rows of interest.
BE := array[N+1] of int;
TE := array[N+1] of int;
for(j := 1; j <= N; j++){
bot := j + (M - DP_BAND);
if(bot > M) bot = M;
BE[j] = bot;
top := j - (N - DP_BAND);
if(top < 1) top = 1;
TE[j] = top;
}
# Allocate and initialize the Cost matrix, C.
# The size of C is (M + 1) x (N + 1).
# Note that row and column 0 are unused.
# Costs are x 100.
C := array[M+1] of array of int;
for(i := 1; i <= M; i++){
C[i] = array[N+1] of int;
bx := B.pts[i-1].x;
by := B.pts[i-1].y;
for(j = 1; j <= N; j++){
ax := A.pts[j-1].x;
ay := A.pts[j-1].y;
dx := bx - ax;
dy := by - ay;
dist := sqrt(10000 * (dx * dx + dy * dy));
C[i][j] = dist;
}
}
# Do the DP algorithm.
# Proceed in column order, from highest column to the lowest.
# Within each column, proceed from the highest row to the lowest.
for(j = N; j > 0; j--)
for(i = M; i > 0; i--){
min := MAXDIST;
if(i > BE[j] || i < TE[j] || (j == N && i == M))
continue;
if(j < N){
if(i >= TE[j+1]){
tmp := C[i][j+1];
if(tmp < min)
min = tmp;
}
if(i < M){
tmp := C[i+1][j+1];
if(tmp < min)
min = tmp;
}
}
if(i < BE[j]){
tmp := C[i+1][j];
if(tmp < min)
min = tmp;
}
C[i][j] += min;
}
return (C[1][1] + N / 2) / N; # dist
}
# Result is x 100.
Stroke.length(s: self ref Stroke): int
{
return s.pathlen(0, s.npts-1);
}
# Result is x 100.
Stroke.pathlen(s: self ref Stroke, first: int, last: int): int
{
l := 0;
for(i := first + 1; i <= last; i++){
dx := s.pts[i].x - s.pts[i-1].x;
dy := s.pts[i].y - s.pts[i-1].y;
l += sqrt(100*100 * (dx*dx + dy*dy));
}
return l;
}
# Note that this does NOT update points.xrange and points.yrange!
Stroke.filter(s: self ref Stroke): ref Stroke
{
pts := array[s.npts] of Penpoint;
pts[0] = s.pts[0];
npts := 1;
for(i := 1; i < s.npts; i++){
j := npts - 1;
dx := s.pts[i].x - pts[j].x;
dy := s.pts[i].y - pts[j].y;
magsq := dx * dx + dy * dy;
if(magsq >= DIST_SQ_THRESHOLD){
pts[npts] = s.pts[i];
npts++;
}
}
return ref Stroke(npts, pts[0:npts], 0, 0);
}
abs(a: int): int
{
if(a < 0)
return -a;
return a;
}
# Code from Joseph Hall (jnhall@sat.mot.com).
sqrt(n: int): int
{
nn := n;
k0 := 2;
for(i := n; i > 0; i >>= 2)
k0 <<= 1;
nn <<= 2;
k1: int;
for(;;){
k1 = (nn / k0 + k0) >> 1;
if(((k0 ^ k1) & ~1) == 0)
break;
k0 = k1;
}
return (k1 + 1) >> 1;
}
# Helper routines from Mark Hayter.
likeatan(tantop: int, tanbot: int): int
{
# Use tan(theta)=top/bot -. order for t
# t in range 0..16r40000
if(tantop == 0 && tantop == 0)
return 0;
t := (tantop << 16) / (abs(tantop) + abs(tanbot));
if(tanbot < 0)
t = 16r20000 - t;
else if(tantop < 0)
t = 16r40000 + t;
return t;
}
quadr(t: int): int
{
return (8 - (((t + 16r4000) >> 15) & 7)) & 7;
}
printpoints(fd: ref Sys->FD, pts: ref Stroke, sep: string)
{
for(j := 0; j < pts.npts; j++){
p := pts.pts[j];
sys->fprint(fd, " (%d %d) %ud%s", p.x, p.y, pts.pts[j].chaincode, sep);
}
}
|