This commit is contained in:
veclav talica 2024-07-30 23:28:33 +03:00
parent 7f0d22e5dc
commit 16c045ba97

View File

@ -7,15 +7,45 @@ CSS: /style.css
A similar in essence trick to [by pi rotation](/articles/vector-pi-rotation.html), but with delta calculated 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. 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). Additionally `cos(a) = sqrt(1 - sin(a) ^ 2)` is used to reuse the result of sin(a),
with `fast_sqrt()` for good measure.
### Code ### ### Code ###
```c ```c
const t_fvec2 c = frect_center(sprite.rect); /* http://www.azillionmonkeys.com/qed/sqroot.html */
const t_fvec2 d = { static inline float fast_sqrt(float x)
.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, union {
float f;
uint32_t u;
} pun = {.f = x};
pun.u += 127 << 23;
pun.u >>= 1;
return pun.f;
}
/* instead of calculating cosf again, - use sinf result */
static inline t_fvec2 fast_cossine(float a) {
const float s = sinf(a);
return (t_fvec2){
.x = fast_sqrt(1.0f - s * s) *
(a >= (float)M_PI_2 && a < (float)(M_PI + M_PI_2) ? -1 : 1),
.y = s
}; };
}
/* final vertex calculation */
const t_fvec2 t = fast_cossine(sprite.rotation + (float)M_PI_4);
/* scaling by `M_SQRT1_2` is there to retain the quad size (Pythagorean stuffs). */
const t_fvec2 d = {
.x = t.x * sprite.rect.w * (float)M_SQRT1_2,
.y = t.y * sprite.rect.h * (float)M_SQRT1_2,
};
const t_fvec2 c = frect_center(sprite.rect);
/* upper-left */ /* upper-left */
const t_fvec2 v0 = { c.x - d.x, c.y - d.y }; const t_fvec2 v0 = { c.x - d.x, c.y - d.y };