Position.cpp 3.1 KB

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