From d1fe7a9230f5c4ddead902e63653d0ad55ca2860 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Sun, 28 Jul 2024 03:35:32 +0300 Subject: [PATCH] fast-quad-rotation --- articles/fast-quad-rotation/page.mmd | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 articles/fast-quad-rotation/page.mmd diff --git a/articles/fast-quad-rotation/page.mmd b/articles/fast-quad-rotation/page.mmd new file mode 100644 index 0000000..791b656 --- /dev/null +++ b/articles/fast-quad-rotation/page.mmd @@ -0,0 +1,32 @@ +Title: Fast Quad Rotation +Brief: A better way to rotate quads around their centers. +Date: 1722126213 +Tags: Programming, Optimization, C +CSS: /style.css + +A similar in essence trick to [by pi rotation](/articles/vector-pi-rotation.html), but with delta calculated +for some corner which is reused later with negation and coordinate swap. + +Scaling by `M_SQRT1_2` is there to retain the quad size (Pythagorean stuffs). + +### Code ### +```c +const t_fvec2 c = frect_center(sprite.rect); +const t_fvec2 d = { + .x = (cosf(sprite.rotation + (float)M_PI_4) * sprite.rect.w) * (float)M_SQRT1_2, + .y = (sinf(sprite.rotation + (float)M_PI_4) * sprite.rect.h) * (float)M_SQRT1_2, +}; + +/* upper-left */ +const t_fvec2 v0 = { c.x - d.x, c.y - d.y }; + +/* bottom-left */ +const t_fvec2 v1 = { c.x - d.y, c.y + d.x }; + +/* bottom-right */ +const t_fvec2 v2 = { c.x + d.x, c.y + d.y }; + +/* upper-right */ +const t_fvec2 v3 = { c.x + d.y, c.y - d.x }; + +```