Skip to content

Instantly share code, notes, and snippets.

View r-lyeh-archived's full-sized avatar

r-lyeh-archived

View GitHub Profile
@r-lyeh-archived
r-lyeh-archived / neville.cc
Created May 20, 2015 13:14
neville interpolation
// neville interpolation based on code by Behzad Torkian
// - rlyeh, public domain
/*
There is a distinction between interpolation and curve fitting.In interpolation we construct
a curve through the data points.In doing so, we make the implicit assumption that the data
points are accurate and distinct. Curve fitting is applied to data that contain scatter(noise),
usually due to measurement errors. Here we want to find a smooth curve that approximates the data
in some sense. Thus the curve does not have to hit the data points. This difference between
interpolation and curve fitting is illustrated in Fig. 3.1.
@r-lyeh-archived
r-lyeh-archived / split-zip.hpp
Last active August 29, 2015 14:21
split/zip containers
// split/zip containers quickly
// rlyeh, public domain {
#pragma once
#include <map>
#include <vector>
template<typename K, typename T>
static inline void split( std::vector<K> &k, std::vector<T> &t, const std::map<K,T> &map ) {
t.clear(), t.reserve( map.size() );
k.clear(), k.reserve( map.size() );
for( auto &pair : map ) {
@r-lyeh-archived
r-lyeh-archived / atf.hpp
Last active March 30, 2016 20:54
map<K,V> interpolator
// map<K,V> interpolator (requires keys and values with arithmetical operators)
// - rlyeh, public domain
#pragma once
#include <algorithm>
enum {
// supported easings
at_linear = 0,
at_quadinout = 1,
// default configuration (feel free to customize)
at_interpolator = at_quadinout,
@r-lyeh-archived
r-lyeh-archived / kdtree.h
Last active July 14, 2017 18:23
kdtree
/*=======================================================*\
* file: gb/graphics/containers/kdTree.h *
* author: Sharov "Zefick" Sergey zefick@mail.ru *
* date: 27.03.2011 *
* version: 1.0 *
\*=======================================================*/
#ifndef KDTREE_H
#define KDTREE_H
@r-lyeh-archived
r-lyeh-archived / quant.cc
Last active March 27, 2023 03:39
suite of de/quantizers (s/unorm direct3d, s/unorm opengl and quaternions)
#include <stdint.h>
// r-lyeh, public domain - quantize quaternions to 32-bits & numbers to 8/10-bits {
//
// [ref] http://zeuxcg.org/2010/12/14/quantizing-floats/
// D3D10, GL2: for *_UNORM formats of n-bit length, the decode function is decode(x) = x / (2^n - 1)
// Unsigned quantization: input: [0..1] float; output: [0..255] integer
// D3D10: for *_SNORM formats of n-bit length the decode function is decode(x) = clamp(x / (2^(n-1) - 1), -1, 1)
// Signed quantization for D3D10 rules: input: [-1..1] float; output: [-127..127] integer
static inline uint8_t encode8_unorm(float x) { return uint8_t( int (x * 255.f + 0.5f) ); }
static inline float decode8_unorm(uint8_t x) { return x / 255.f; }
// lerp code / r-lyeh, public domain
template<typename T>
static inline T lerp(const T &a, const T &b, float t01) {
return a < b ? a * (1-t01) + b * t01 : b * (1-t01) + a * t01;
}
// todo: slerp, nlerp
// [ref] https://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/
@r-lyeh-archived
r-lyeh-archived / dll.cpp
Created May 11, 2015 08:20
std::function && dll
// [ref] http://stackoverflow.com/questions/15249465/c-dynamically-load-arbitrary-function-from-dll-into-stdfunction
template <typename T>
struct TypeParser {};
template <typename Ret, typename... Args>
struct TypeParser<Ret(Args...)> {
static std::function<Ret(Args...)> createFunction(const FARPROC lpfnGetProcessID) {
return std::function<Ret(Args...)>(reinterpret_cast<Ret (__stdcall *)(Args...)>(lpfnGetProcessID));
}
/*
* Random-Number Utilities (randutil)
* Addresses common issues with C++11 random number generation.
* Makes good seeding easier, and makes using RNGs easy while retaining
* all the power.
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Melissa E. O'Neill
*
// [ref] http://www.reddit.com/r/gamedev/comments/34xmv9/scoring_mechanism/
/*
scoring function on the form a×e-bt , where t is the time spent by the player, and a and b are constants you find appropriate for the level. As this is a continuous score function, a user will always be awarded with an improvement of their time by an increase in score - no matter how good or bad the score was to begin with.
With a little bit of math it is easy to find good values of a and b for a certain level.
Let us say you want a very good performance (5 stars) to be worth 10,000 points and a fairly low-end performance (1 star) to be worth 1,000 points. For a certain level you consider 10 seconds to be a very good time and 25 seconds to be a fairly low-end performance.
You then have
a × e-10b = 10,000
a × e-25b = 1,000
(e-10b ) / (e-25b ) = 10
e15b = 10