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#pragma once
5
6namespace graphite2 {
7
8class Position
9{
10public:
11 Position() : x(0), y(0) { }
12 Position(const float inx, const float iny) : x(inx), y(iny) {}
13 Position operator + (const Position& a) const { return Position(x + a.x, y + a.y); }
14 Position operator - (const Position& a) const { return Position(x - a.x, y - a.y); }
15 Position operator * (const float m) const { return Position(x * m, y * m); }
16 Position &operator += (const Position &a) { x += a.x; y += a.y; return *this; }
17 Position &operator *= (const float m) { x *= m; y *= m; return *this; }
18
19 float x;
20 float y;
21};
22
23class Rect
24{
25public :
26 Rect() {}
27 Rect(const Position& botLeft, const Position& topRight): bl(botLeft), tr(topRight) {}
28 Rect widen(const Rect& other) { return Rect(Position(bl.x > other.bl.x ? other.bl.x : bl.x, bl.y > other.bl.y ? other.bl.y : bl.y), Position(tr.x > other.tr.x ? tr.x : other.tr.x, tr.y > other.tr.y ? tr.y : other.tr.y)); }
29 Rect operator + (const Position &a) const { return Rect(Position(bl.x + a.x, bl.y + a.y), Position(tr.x + a.x, tr.y + a.y)); }
30 Rect operator - (const Position &a) const { return Rect(Position(bl.x - a.x, bl.y - a.y), Position(tr.x - a.x, tr.y - a.y)); }
31 Rect operator * (float m) const { return Rect(Position(bl.x, bl.y) * m, Position(tr.x, tr.y) * m); }
32 float width() const { return tr.x - bl.x; }
33 float height() const { return tr.y - bl.y; }
34
35 bool hitTest(Rect &other);
36
37 // returns Position(overlapx, overlapy) where overlap<0 if overlapping else positive)
38 Position overlap(Position &offset, Rect &other, Position &otherOffset);
39 //Position constrainedAvoid(Position &offset, Rect &box, Rect &sdbox, Position &other, Rect &obox, Rect &osdbox);
40
41 Position bl;
42 Position tr;
43};
44
45} // namespace graphite2
46