Moved some stuff around.
[jben2_gui.git] / c / kpengine / util.c
blob7002b1592a6a0a6387dd05de7c0ff97785f7bf0c
1 /* -*- mode: C; c-file-style: "bsd"; tab-width: 4 -*- */
2 /* util.c - Functions shared between handwriting engine and UI
3 * JStroke 1.x - Japanese Kanji handwriting recognition technology demo.
4 * Copyright (C) 1997 Robert E. Wells
5 * http://wellscs.com/pilot
6 * mailto:robert@wellscs.com
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program (gpl.html); if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Derived from prior work by Todd David Rudick on JavaDict and StrokeDic.
23 * Makes use of KANJIDIC data from Jim Breen of Monash University.
24 * Further credit details available at http://wellscs.com/pilot
25 * See readme.txt, changelo, and gpl.html for more information.
26 * -------------------------------------------------------------------------*/
28 #include "jstroke.h"
29 #include "jstrokerc.h"
31 /* ----- AppEmptyList ------------------------------------------------------*/
33 ListMem* AppEmptyList() {
34 UInt iTotSize;
35 ListMem* pListMem;
36 CharPtr cpList;
38 iTotSize = sizeof(ListMem) + sizeof(CharPtr) + 1;
40 if (!(cpList = MemPtrNew(iTotSize))) {
41 ErrBox("ERROR: no mem in top picks");
42 return NULL;
45 pListMem = (ListMem *) cpList;
46 cpList += sizeof(ListMem);
47 cpList += sizeof(CharPtr);
49 pListMem->m_argc = 1;
51 pListMem->m_argv[0] = cpList;
52 pListMem->m_argv[1] = NULL;
54 *cpList = '\0';
56 return pListMem;
59 /* ----- Angle32 -------------------------------------------------------------
60 * For given int xdif and ydif, calculate atan2 (the angle from origin)
61 * in 32nd's of a circle from 0 to 32, rather than radians. Note that it
62 * returns 32 iff xdif and ydif are both zero, an ill-defined case.
63 * Origin and direction are clockwise:
64 * 0 => 12:00, 8 => 3:00, 16 => 6:00, 24 => 9:00.
65 * Why 32nds? So we can divide them into 8 pieces evenly, and so we get
66 * 4x over-sampling for the 8th's in the path descriptions...
67 * -rwells, 970713.
70 Long Angle32(Long xdif, Long ydif) {
71 Boolean xneg, yneg, xyflip;
72 Long i32nd, xtmp, islope;
74 if ((xneg = (xdif < 0)))
75 xdif = -xdif;
76 if ((yneg = (ydif < -0.1)))
77 ydif = -ydif;
78 if ((xyflip = (ydif < xdif))) {
79 xtmp = xdif; xdif = ydif; ydif = xtmp;
82 if (xdif == 0) {
83 if (ydif == 0)
84 return 32;
85 else
86 i32nd = 0;
88 else {
89 /* The 4 comparison values were generated with the accompanying
90 * perl script, then open coded here for speed and reasonable
91 * space efficiency. The values were chosen to make the results
92 * match those of atan2 in rounded double
93 * precision floating point. -rwells, 970713.
96 islope = (100 * xdif) / ydif;
97 if (islope < 54) { /* test #2, first test. */
98 if (islope < 10) /* test #0, second test. */
99 i32nd = 0; /* got #0 after 2 tests. */
100 else if (islope < 31) /* test #1, third test. */
101 i32nd = 1; /* got #1 after 3 tests. */
102 else
103 i32nd = 2; /* got #2 after 3 tests. */
105 else if (islope < 83) /* test #3, second test. */
106 i32nd = 3; /* got #3 after 2 tests. */
107 else
108 i32nd = 4; /* got #4 after 2 tests. */
111 if (xyflip)
112 i32nd = (8 - i32nd);
113 if (yneg)
114 i32nd = (16 - i32nd);
115 if (xneg)
116 i32nd = (32 - i32nd);
118 return i32nd % 32;
121 #ifdef FOR_PILOT_COMPAT
123 /* ----- ErrBox ------------------------------------------------------------*/
125 void ErrBox(CharPtr msg) {
126 fprintf(stderr,"%s\n",msg);
129 /* ----- ErrBox2 -----------------------------------------------------------*/
131 void ErrBox2(CharPtr msg1, CharPtr msg2) {
132 fprintf(stderr,"%s\n%s\n",msg1,msg2);
135 #else /* !FOR_PILOT_COMPAT */
137 /* ----- ErrBox ------------------------------------------------------------*/
139 void ErrBox(CharPtr msg) {
140 (void) FrmCustomAlert(alertID_ErrBox, msg, "", "");
143 /* ----- ErrBox2 -----------------------------------------------------------*/
145 void ErrBox2(CharPtr msg1, CharPtr msg2) {
146 (void) FrmCustomAlert(alertID_ErrBox, msg1, msg2, "");
149 #endif /* FOR_PILOT_COMPAT */
150 /* ----- End of util.c ---------------------------------------------------- */