Skip to content

Instantly share code, notes, and snippets.

@petrsm
petrsm / gist:d47be1fbd0f240b5051bdc81cb62f79e
Created November 22, 2017 08:28
2D point projection onto cubic (Catmull-Rom) curve
//
// CatmullRomCurveProjectPt()
//
//
//*******************************************************************************************
template <typename T>
T CatmullRomCurveProjectPt( const CVector2<T> &p0,
const CVector2<T> &p1,
const CVector2<T> &p2,
const CVector2<T> &p3,
//
// CatmullRomLength()
//
//
//*******************************************************************************************
template <typename T>
T CatmullRomLength( const CVector2<T> &p0,
const CVector2<T> &p1,
const CVector2<T> &p2,
const CVector2<T> &p3,
@petrsm
petrsm / gist:04f0f94f79ddc1a91051303585c8f413
Created November 29, 2017 08:51
Catmull-Rom bounding box 2D
//
// CatmullRomBoundinBox2D()
//
//
//*********************************************************************************************
template <typename T_Vec2>
void CatmullRomBoundingBox2D(const T_Vec2 ctrlPts[4], T_Vec2 &min, T_Vec2 &max)
{
// See this for description of concept:
// https://stackoverflow.com/questions/24809978/calculating-the-bounding-box-of-cubic-bezier-curve
@petrsm
petrsm / gist:079de9396d63e00d5994a7cc936ae9c7
Last active November 21, 2022 17:55
Fast pow-2 approximation
static NX_FORCEINLINE float FastPow2_P2(float p)
{
// 2^x = 2^xi * 2^xf
// pow2f - polynomial approximation of e(x) on <0, 1) range
const float pi = floorf(p);
const float pf = p - pi;
const float pow2i = UIntAsFloat((1 << 23) * ((int)pi + 127));
float pow2f = 3.4400110689651967e-1f;
@petrsm
petrsm / gist:ee54e688bc6de367ae3b84f0db69bca3
Created December 21, 2017 13:46
Fast log2 approximation
static NX_FORCEINLINE float FastLog2_P2(float a)
{
// Based on code by Norbert Juffa
// r = log2(1 + m) for m in [sqrt(0.5) - 1, sqrt(2.0) - 1] range
const int e = (FloatAsUInt(a) - 0x3f3504f3) & 0xff800000;
const float m = UIntAsFloat(FloatAsUInt(a) - e) - 1.0f;
const float i = (float)e * 1.19209290e-7f;
float r = -6.8969686599880982e-1f;