1 /*  GRAPHITE2 LICENSING
2 
3     Copyright 2010, SIL International
4     All rights reserved.
5 
6     This library is free software; you can redistribute it and/or modify
7     it under the terms of the GNU Lesser General Public License as published
8     by the Free Software Foundation; either version 2.1 of License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Lesser General Public License for more details.
15 
16     You should also have received a copy of the GNU Lesser General Public
17     License along with this library in the file named "LICENSE".
18     If not, write to the Free Software Foundation, 51 Franklin Street,
19     Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
20     internet at http://www.fsf.org/licenses/lgpl.html.
21 
22 Alternatively, the contents of this file may be used under the terms of the
23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
24 License, as published by the Free Software Foundation, either version 2
25 of the License or (at your option) any later version.
26 */
27 #include "inc/Position.h"
28 #include <cmath>
29 
30 using namespace graphite2;
31 
hitTest(Rect & other)32 bool Rect::hitTest(Rect &other)
33 {
34     if (bl.x > other.tr.x) return false;
35     if (tr.x < other.bl.x) return false;
36     if (bl.y > other.tr.y) return false;
37     if (tr.y < other.bl.y) return false;
38     return true;
39 }
40 
overlap(Position & offset,Rect & other,Position & othero)41 Position Rect::overlap(Position &offset, Rect &other, Position &othero)
42 {
43     float ax = (bl.x + offset.x) - (other.tr.x + othero.x);
44     float ay = (bl.y + offset.y) - (other.tr.y + othero.y);
45     float bx = (other.bl.x + othero.x) - (tr.x + offset.x);
46     float by = (other.bl.y + othero.y) - (tr.y + offset.y);
47     return Position((ax > bx ? ax : bx), (ay > by ? ay : by));
48 }
49 
boundmin(float move,float lim1,float lim2,float & error)50 float boundmin(float move, float lim1, float lim2, float &error)
51 {
52     // error is always positive for easy comparison
53     if (move < lim1 && move < lim2)
54     { error = 0.; return move; }
55     else if (lim1 < lim2)
56     { error = std::fabs(move - lim1); return lim1; }
57     else
58     { error = std::fabs(move - lim2); return lim2; }
59 }
60 
61 #if 0
62 Position Rect::constrainedAvoid(Position &offset, Rect &box, Rect &sdbox, Position &other, Rect &obox, Rect &osdbox)
63 {
64     // a = max, i = min, s = sum, d = diff
65     float eax, eay, eix, eiy, eas, eis, ead, eid;
66     float beste = INF;
67     Position res;
68     // calculate the movements in each direction and the error (amount of remaining overlap)
69     // first param is movement, second and third are movement over the constraining box
70     float ax = boundmin(obox.tr.x + other.x - box.bl.x - offset.x + 1, tr.x - offset.x, INF, &eax);
71     float ay = boundmin(obox.tr.y + other.y - box.bl.y - offset.y + 1, tr.y - offset.y, INF, &eay);
72     float ix = boundmin(obox.bl.x + other.x - box.tr.x - offset.x + 1, bl.x - offset.x, INF, &eix);
73     float iy = boundmin(obox.bl.y + other.y - box.tr.y - offset.y + 1, bl.y - offset.y, INF, &eiy);
74     float as = boundmin(ISQRT2 * (osdbox.tr.x + other.x + other.y - sdbox.bl.x - offset.x - offset.y) + 1, tr.x - offset.x, tr.y - offset.y, &eas);
75     float is = boundmin(ISQRT2 * (osdbox.bl.x + other.x + other.y - sdbox.tr.x - offset.x - offset.y) + 1, bl.x - offset.x, bl.y - offset.y, &eis);
76     float ad = boundmin(ISQRT2 * (osdbox.tr.y + other.x - other.y - sdbox.bl.y - offset.x + offset.y) + 1, tr.y - offset.y, tr.x - offset.x, &ead);
77     float id = boundmin(ISQRT2 * (osdbox.bl.y + other.x - other.y - sdbox.tr.y - offset.x + offset.y) + 1, bl.y - offset.y, bl.x - offset.x, &eid);
78 
79     if (eax < beste)
80     { res = Position(ax, 0); beste = eax; }
81     if (eay < beste)
82     { res = Position(0, ay); beste = eay; }
83     if (eix < beste)
84     { res = Position(ix, 0); beste = eix; }
85     if (eiy < beste)
86     { res = Position(0, iy); beste = eiy; }
87     if (SQRT2 * (eas) < beste)
88     { res = Position(as, ad); beste = SQRT2 * (eas); }
89     if (SQRT2 * (eis) < beste)
90     { res = Position(is, is); beste = SQRT2 * (eis); }
91     if (SQRT2 * (ead) < beste)
92     { res = Position(ad, ad); beste = SQRT2 * (ead); }
93     if (SQRT2 * (eid) < beste)
94     { res = Position(id, id); beste = SQRT2 * (eid); }
95     return res;
96 }
97 #endif
98