Skip to content

Instantly share code, notes, and snippets.

@RandyGaul
RandyGaul / spatial_hash.c
Last active October 16, 2024 04:18
2d spatial hashing
/**
* 2D spatial hash implementation.
*
* The spatial hash is really good at representing grids in games. It only stores elements
* of the grid where items are inserted into the spacial hash, making it a memory-efficient
* option for grid games with lots of empty space between tiles/entities.
*
* Spatial hash queries are extremely fast for small queries covering low surface areas.
*
* Spatial hashes breakdown when queries cover large surface areas. This is because each
@RandyGaul
RandyGaul / prio_q.lua
Created August 3, 2024 16:37
priority queue implementation in lua
-- Piorirty queue, used for implementing other algorithms such as
-- prioritized message queues, or the A* algorithm.
--
-- Create a new priority q like so:
--
-- q = prio_q()
--
-- Add elements to the queue, each has a cost associated. THe cost is
-- used to sort the elements, where the lowest cost is considered the
-- highest priority (first out when pop() is called).
@RandyGaul
RandyGaul / curriculum.txt
Last active August 16, 2023 20:13
Learn Software Engineering - Self Paced
WHAT IS THIS?
I get asked a lot about recommendations for self-taught or self-learning to become professional developer, so I
am slowly working on a curriculum someone could follow. If finished in earnest, you'd be at roughly bachelor degree
level of competence without any holes in your education. If anyone is interested, please do take a look and provide
feedback! The format is to collate really high quality links to references organized by major topic and sub-categories.
By design this is C/C++ centric. This encourages the learning of low level details and fundamental knowledge that
transcends technological fads, ensuring the knowledge won't ever go out of date, easily translatable to other higher
level languages, frameworks, granting sustainable flexibility in career potential.
@RandyGaul
RandyGaul / lines_2d_intersect.cpp
Last active January 6, 2023 01:20
2D halfspace intersection via cramer's rule
#ifndef _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_NONSTDC_NO_DEPRECATE
# define _CRT_NONSTDC_NO_DEPRECATE
#endif
#include <vector>
// -------------------------------------------------------------------------------------------------
@RandyGaul
RandyGaul / wrap_triangle_in_minimal_bound_box.cpp
Last active January 7, 2023 23:37
Wrap a triangle in a minimal bounding box, optionally inflated by a radius factor
#ifndef _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_NONSTDC_NO_DEPRECATE
# define _CRT_NONSTDC_NO_DEPRECATE
#endif
#include <vector>
// -------------------------------------------------------------------------------------------------
@RandyGaul
RandyGaul / htable.h
Last active May 24, 2024 13:03
Polymorphic C hash table macros
// Polymorphic hashtable API in C
//
// We get value semantics here for hset and hget (see comments below).
// An extra layer of indirection is internally used so we can sort {k,v} pairs, e.g. for priority queue.
// The main trick is the use of comma in C. Any statement in C takes on the value of the expression
// in the final comma, not any of the prior. We can use that to "return" a value in `hget` while running
// a few expressions beforehand. Since the user type is a pointer to a value we get polymorphism by
// the indirection or array operator `*h` or `h[0]`.
//
// Original inspired from these resources by Per Vognsen and Micha Mettke
@RandyGaul
RandyGaul / embed.c
Created September 25, 2022 20:17
Create an array of binary data to embed files into your program directly
// Embedded some small file here.
int g_some_file_sz = 64;
unsigned char g_some_file_data[64] = {
0x10,0xaa,0x98,0xe0,0x10,0x5a,0x3e,0x63,0xe5,0xdf,0xa4,0xb5,0x5d,0xf3,0x3c,0x0a,
0x31,0x5d,0x6e,0x58,0x1e,0xb8,0x5b,0xa4,0x4e,0xa3,0xf8,0xe7,0x55,0x53,0xaf,0x7a,
0x4a,0xc5,0x56,0x47,0x30,0xbf,0xdc,0x22,0xc7,0x67,0x3b,0x23,0xc5,0x00,0x21,0x7e,
0x19,0x3e,0xa4,0xed,0xbc,0x0f,0x87,0x98,0x80,0xac,0x89,0x82,0x30,0xe9,0x95,0x6c
};
// Creates an array like the one above.
@RandyGaul
RandyGaul / routine.h
Last active November 6, 2022 15:33
Portable "coroutine"-like thing for implementing FSM and behavior-cycles
#ifndef ROUTINE_H
#define ROUTINE_H
#include <stdint.h>
// A portable "coroutine"-like thing for implementing FSM and behavior-cycles.
//
// Original implementation by Noel Berry.
// See: https://gist.github.com/NoelFB/7a5fa66fc29dd7ed1c11042c30f1b00e
//
@RandyGaul
RandyGaul / edge_interesections.cpp
Last active November 15, 2022 15:18
Calculate edge-edge intersections of convex polygons via Sutherland-Hodgman in 2D
#ifndef _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_NONSTDC_NO_DEPRECATE
# define _CRT_NONSTDC_NO_DEPRECATE
#endif
#include <vector>
// -------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <assert.h>
struct item_t
{
int key;
int val;
};
int get_byte(int val, int byte_index)