From cb6f190c58ad2651a72c10f4a496b53e3f3c79e1 Mon Sep 17 00:00:00 2001 From: Bill Kendrick Date: Sat, 28 Dec 2024 12:31:19 -0800 Subject: [PATCH] in_circle_rad() - Avoid mult if obviously outside circle Most places that invoke this already avoid outside testing inherently (e.g., "for (y = -r; y <= r; y++) ..."), but a few places don't, so I'm _hoping_ this might be beneficial to avoid a handful of multiplication operations. (Worst case, we can add the test optionally, and have it dump some debug output, so we can track down places that waste clockcycles testing points obviously outside a circle.) --- src/tuxpaint.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/tuxpaint.c b/src/tuxpaint.c index 59d4de3d7..babdab468 100644 --- a/src/tuxpaint.c +++ b/src/tuxpaint.c @@ -21201,7 +21201,12 @@ static void flip_starter(void) /** - * FIXME + * Returns whether a button click is valid, based on the + * "no button distinction" setting. + * + * @param Uint8 button -- the button clicked (we check for 1, 2, or 3) + * @return int -- 1/true if the button is recognized, else 0/false + * (2 & 3 are only valid if "no button distiction" is enabled) */ static int valid_click(Uint8 button) { @@ -21213,10 +21218,17 @@ static int valid_click(Uint8 button) /** - * FIXME + * Returns whether a point is within a circle. + * + * @param int x -- x position relative to center of circle + * @param int y -- y position [ditto] + * @param int rad -- radius of circle + * @return int -- 1/true if (x,y) is within the circle, else 0/false */ static int in_circle_rad(int x, int y, int rad) { + if (abs(x) > rad || abs(y) > rad) // short circuit to avoid unnecessary math + return (0); if ((x * x) + (y * y) - (rad * rad) < 0) return (1); else