1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5struct RectWithSize {
6    vec2 p0;
7    vec2 size;
8};
9
10struct RectWithEndpoint {
11    vec2 p0;
12    vec2 p1;
13};
14
15float point_inside_rect(vec2 p, vec2 p0, vec2 p1) {
16    vec2 s = step(p0, p) - step(p1, p);
17    return s.x * s.y;
18}
19
20float signed_distance_rect(vec2 pos, vec2 p0, vec2 p1) {
21    vec2 d = max(p0 - pos, pos - p1);
22    // Instead of using a true signed distance to rect here, we just use the
23    // simpler approximation of the maximum distance on either axis from the
24    // outside of the rectangle. This avoids expensive use of length() and only
25    // causes mostly imperceptible differences at corner pixels.
26    return max(d.x, d.y);
27}
28
29vec2 rect_clamp(RectWithEndpoint rect, vec2 pt) {
30    return clamp(pt, rect.p0, rect.p1);
31}
32
33vec2 rect_size(RectWithEndpoint rect) {
34    return rect.p1 - rect.p0;
35}