1 /******************************************************************************
2 *
3 * File: outlines.cpp (Formerly outlines.c)
4 * Description: Combinatorial Splitter
5 * Author: Mark Seaman, OCR Technology
6 *
7 * (c) Copyright 1989, Hewlett-Packard Company.
8 ** Licensed under the Apache License, Version 2.0 (the "License");
9 ** you may not use this file except in compliance with the License.
10 ** You may obtain a copy of the License at
11 ** http://www.apache.org/licenses/LICENSE-2.0
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 *
18 *****************************************************************************/
19 /*----------------------------------------------------------------------
20 I n c l u d e s
21 ----------------------------------------------------------------------*/
22 #include "outlines.h"
23 #include "wordrec.h"
24
25 namespace tesseract {
26 /*----------------------------------------------------------------------
27 F u n c t i o n s
28 ----------------------------------------------------------------------*/
29 /**********************************************************************
30 * near_point
31 *
32 * Find the point on a line segment that is closest to a point not on
33 * the line segment. Return that point in near_pt. Returns whether
34 * near_pt was newly created.
35 **********************************************************************/
near_point(EDGEPT * point,EDGEPT * line_pt_0,EDGEPT * line_pt_1,EDGEPT ** near_pt)36 bool Wordrec::near_point(EDGEPT *point, EDGEPT *line_pt_0, EDGEPT *line_pt_1, EDGEPT **near_pt) {
37 TPOINT p;
38
39 float slope;
40 float intercept;
41
42 float x0 = line_pt_0->pos.x;
43 float x1 = line_pt_1->pos.x;
44 float y0 = line_pt_0->pos.y;
45 float y1 = line_pt_1->pos.y;
46
47 if (x0 == x1) {
48 /* Handle vertical line */
49 p.x = static_cast<int16_t>(x0);
50 p.y = point->pos.y;
51 } else {
52 /* Slope and intercept */
53 slope = (y0 - y1) / (x0 - x1);
54 intercept = y1 - x1 * slope;
55
56 /* Find perpendicular */
57 p.x = static_cast<int16_t>((point->pos.x + (point->pos.y - intercept) * slope) /
58 (slope * slope + 1));
59 p.y = static_cast<int16_t>(slope * p.x + intercept);
60 }
61
62 if (is_on_line(p, line_pt_0->pos, line_pt_1->pos) && (!same_point(p, line_pt_0->pos)) &&
63 (!same_point(p, line_pt_1->pos))) {
64 /* Intersection on line */
65 *near_pt = make_edgept(p.x, p.y, line_pt_1, line_pt_0);
66 return true;
67 } else { /* Intersection not on line */
68 *near_pt = closest(point, line_pt_0, line_pt_1);
69 return false;
70 }
71 }
72
73 } // namespace tesseract
74