1 | // SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later |
2 | // Copyright 2010, SIL International, All rights reserved. |
3 | |
4 | #include "inc/Position.h" |
5 | #include <cmath> |
6 | |
7 | using namespace graphite2; |
8 | |
9 | bool Rect::hitTest(Rect &other) |
10 | { |
11 | if (bl.x > other.tr.x) return false; |
12 | if (tr.x < other.bl.x) return false; |
13 | if (bl.y > other.tr.y) return false; |
14 | if (tr.y < other.bl.y) return false; |
15 | return true; |
16 | } |
17 | |
18 | Position Rect::overlap(Position &offset, Rect &other, Position &othero) |
19 | { |
20 | float ax = (bl.x + offset.x) - (other.tr.x + othero.x); |
21 | float ay = (bl.y + offset.y) - (other.tr.y + othero.y); |
22 | float bx = (other.bl.x + othero.x) - (tr.x + offset.x); |
23 | float by = (other.bl.y + othero.y) - (tr.y + offset.y); |
24 | return Position((ax > bx ? ax : bx), (ay > by ? ay : by)); |
25 | } |
26 | |
27 | float boundmin(float move, float lim1, float lim2, float &error) |
28 | { |
29 | // error is always positive for easy comparison |
30 | if (move < lim1 && move < lim2) |
31 | { error = 0.; return move; } |
32 | else if (lim1 < lim2) |
33 | { error = std::fabs(move - lim1); return lim1; } |
34 | else |
35 | { error = std::fabs(move - lim2); return lim2; } |
36 | } |
37 | |
38 | #if 0 |
39 | Position Rect::constrainedAvoid(Position &offset, Rect &box, Rect &sdbox, Position &other, Rect &obox, Rect &osdbox) |
40 | { |
41 | // a = max, i = min, s = sum, d = diff |
42 | float eax, eay, eix, eiy, eas, eis, ead, eid; |
43 | float beste = INF; |
44 | Position res; |
45 | // calculate the movements in each direction and the error (amount of remaining overlap) |
46 | // first param is movement, second and third are movement over the constraining box |
47 | float ax = boundmin(obox.tr.x + other.x - box.bl.x - offset.x + 1, tr.x - offset.x, INF, &eax); |
48 | float ay = boundmin(obox.tr.y + other.y - box.bl.y - offset.y + 1, tr.y - offset.y, INF, &eay); |
49 | float ix = boundmin(obox.bl.x + other.x - box.tr.x - offset.x + 1, bl.x - offset.x, INF, &eix); |
50 | float iy = boundmin(obox.bl.y + other.y - box.tr.y - offset.y + 1, bl.y - offset.y, INF, &eiy); |
51 | 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); |
52 | 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); |
53 | 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); |
54 | 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); |
55 | |
56 | if (eax < beste) |
57 | { res = Position(ax, 0); beste = eax; } |
58 | if (eay < beste) |
59 | { res = Position(0, ay); beste = eay; } |
60 | if (eix < beste) |
61 | { res = Position(ix, 0); beste = eix; } |
62 | if (eiy < beste) |
63 | { res = Position(0, iy); beste = eiy; } |
64 | if (SQRT2 * (eas) < beste) |
65 | { res = Position(as, ad); beste = SQRT2 * (eas); } |
66 | if (SQRT2 * (eis) < beste) |
67 | { res = Position(is, is); beste = SQRT2 * (eis); } |
68 | if (SQRT2 * (ead) < beste) |
69 | { res = Position(ad, ad); beste = SQRT2 * (ead); } |
70 | if (SQRT2 * (eid) < beste) |
71 | { res = Position(id, id); beste = SQRT2 * (eid); } |
72 | return res; |
73 | } |
74 | #endif |
75 | |