rect.c (1660B)
1 /* 2 * Copyright (c) 2021, 2022 lumidify <nobody@lumidify.org> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include "rect.h" 18 19 /* FIXME */ 20 #undef MAX 21 #undef MIN 22 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 23 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 24 25 ltk_rect 26 ltk_rect_intersect(ltk_rect r1, ltk_rect r2) { 27 ltk_rect i; 28 i.x = MAX(r1.x, r2.x); 29 i.y = MAX(r1.y, r2.y); 30 i.w = MIN(r1.x + r1.w, r2.x + r2.w) - i.x; 31 i.h = MIN(r1.y + r1.h, r2.y + r2.h) - i.y; 32 return i; 33 } 34 35 ltk_rect 36 ltk_rect_relative(ltk_rect parent, ltk_rect child) { 37 return (ltk_rect){child.x - parent.x, child.y - parent.y, child.w, child.h}; 38 } 39 40 ltk_rect 41 ltk_rect_union(ltk_rect r1, ltk_rect r2) { 42 ltk_rect u; 43 u.x = MIN(r1.x, r2.x); 44 u.y = MIN(r1.y, r2.y); 45 u.w = MAX(r1.x + r1.w, r2.x + r2.w) - u.x; 46 u.h = MAX(r1.y + r1.h, r2.y + r2.h) - u.y; 47 return u; 48 } 49 50 int 51 ltk_collide_rect(ltk_rect rect, int x, int y) { 52 return (rect.x <= x && (rect.x + rect.w) >= x && rect.y <= y 53 && (rect.y + rect.h) >= y); 54 }