Skip to content

Instantly share code, notes, and snippets.

// --- Library --------------------------------------------------------------------------
#include <string.h>
#include <assert.h>
struct ui_box {int x,y,w,h;};
typedef unsigned long long ui_id;
struct ui_node {
int parent;
int lst, end, nxt;
int siz[2];
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#define streq(a, b) (!strcmp((a), (b)))
#ifndef __USE_GNU
#define __USE_GNU
@vurtun
vurtun / kth.c
Last active May 23, 2024 18:34
print K largest(or smallest) elements in an array
/* Problem: Directory file view ui. Files can be sorted by different properties (name, size, ...). Ui itself only needs elements
that are currently in the visible section of the list view from x and count k. So I want to use a fixed size buffer with up to k elements
and walk through all files in the directory and only have the final elements in the end in the fixed size buffer.
Temp buffers are fine for me as long as they have a fixed at compile time size. For those familiar with SQL basically this is
a SORT BY LIMIT begin, count.
Idea: Use Floyd–Rivest algorithm with average O(n) to find the element at index x. Walk over list again and skip all elements smaller than x
then use heap to sort for the k smallest elements afterwards. So we have for Floyd–Rivest and heap combined O(n*log(k))
Problem: Find x is not directly possible since not all elements are in memory because we have only fixed size buffer.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#define cast(t,p) ((t)(p))
#define szof(a) ((int)sizeof(a))
@vurtun
vurtun / math.c
Last active May 16, 2024 04:17
Linear Algebra
#include <math.h>
#define op(r,e,a,p,b,i,s) ((r) e (a) p ((b) i (s)))
#define lerp(r,a,b,t) ((r)=(a)+(t)*((b)-(a)))
#define rad(x) ((x)*3.141592653f/180.0f)
#define op3(r,e,a,p,b,i,s)\
do{op((r)[0],e,(a)[0],p,(b)[0],i,s), op((r)[1],e,(a)[1],p,(b)[1],i,s),\
op((r)[2],e,(a)[2],p,(b)[2],i,s);} while(0)
#define op3s(r,e,a,p,s)\
do{op((r)[0],e,(0),+,(a)[0],p,s), op((r)[1],e,(0),+,(a)[1],p,s),\
@vurtun
vurtun / rdp.c
Last active May 15, 2024 08:05
Ramer-Douglas-Peucker line simplification
#include <assert.h>
#include <math.h>
#include <stdio.h>
/* Alternative design: Iterate over path and at each point i check distance to line with points
i + 1 and i + 2 and skip i + 1 when distance is small than epsilon. */
static float
line_dist(const float *p, const float *p1, const float *p2) {
float dx = p2[0] - p1[0];
float dy = p2[1] - p1[1];
@vurtun
vurtun / _GJK.md
Last active May 1, 2024 22:49
3D Gilbert–Johnson–Keerthi (GJK) distance algorithm

Gilbert–Johnson–Keerthi (GJK) 3D distance algorithm

The Gilbert–Johnson–Keerthi (GJK) distance algorithm is a method of determining the minimum distance between two convex sets. The algorithm's stability, speed which operates in near-constant time, and small storage footprint make it popular for realtime collision detection.

Unlike many other distance algorithms, it has no requirments on geometry data to be stored in any specific format, but instead relies solely on a support function to iteratively generate closer simplices to the correct answer using the Minkowski sum (CSO) of two convex shapes.

@vurtun
vurtun / fold.c
Last active April 15, 2024 16:41
unicode case folding
// Selection algorithm
// Partial sorting
// http://www.unicode.org/Public/UCD/latest/ucd/CaseFolding.txt
switch(rune) {
case 0x0041: return 0x0061; // LATIN CAPITAL LETTER A
case 0x0042: return 0x0062; // LATIN CAPITAL LETTER B
case 0x0043: return 0x0063; // LATIN CAPITAL LETTER C
case 0x0044: return 0x0064; // LATIN CAPITAL LETTER D
case 0x0045: return 0x0065; // LATIN CAPITAL LETTER E
@vurtun
vurtun / burg.c
Last active March 13, 2024 08:22
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define szof(a) ((int)sizeof(a))
#define cntof(a) ((int)(sizeof(a) / sizeof((a)[0])))
static void