Skip to content

Instantly share code, notes, and snippets.

@SpexGuy
SpexGuy / DelaunayAdjacency.java
Last active December 22, 2016 22:22
A routine to calculate whether two points are adjacent in the Delaunay Triangulation of a space.
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
/**
* This class computes whether two points are adjacent in the Delaunay Triangulation of a set of points.
* Throughout the implementation, it makes the assumption that points can be compared with reference equality for uniqueness.
*/
public abstract class DelaunayAdjacency {
private static final float EPSILON = 0.000001f;
@SpexGuy
SpexGuy / main.cpp
Created February 28, 2017 02:14
GlfwTemplate simple triangle on mac
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <glm/glm.hpp>
#include <stb/stb_image.h>
#include "gl_includes.h"
@SpexGuy
SpexGuy / tagged_unions.zig
Last active March 15, 2024 22:41
Zig: Using tagged unions to make a simple json formatter
const JsonString = struct {
value: []const u8,
};
const JsonNumber = struct {
value: f64,
};
const JsonObject = struct {
const Property = struct {
name: []const u8,
value: JsonValue,
@SpexGuy
SpexGuy / vla.zig
Last active June 18, 2021 15:40
VLA types implemented in user space in Zig
const std = @import("std");
pub fn VLA(comptime ArrayType: type, comptime ParentStruct: type, comptime fieldName: []u8) type {
return struct {
const headerSize: usize = comptime std.mem.alignForward(@sizeOf(ParentStruct), @alignOf(ArrayType));
const headerAlign: u29 = comptime std.math.max(@alignOf(ArrayType), @alignOf(ParentStruct));
len: usize,
pub fn get(self: *@This()) []ArrayType {
@SpexGuy
SpexGuy / vk_nv_raytracing.zig
Created April 21, 2020 03:55
The NV_ray_tracing extension subset of vulkan_core.zig
pub const NV_ray_tracing = 1;
pub const AccelerationStructureNV = *@OpaqueType();
pub const NV_RAY_TRACING_SPEC_VERSION = 3;
pub const NV_RAY_TRACING_EXTENSION_NAME = "VK_NV_ray_tracing";
pub const SHADER_UNUSED_NV = (~@as(u32, 0));
pub const AccelerationStructureTypeNV = extern enum(i32) {
TOP_LEVEL = 0,
BOTTOM_LEVEL = 1,
const std = @import("std");
const Channel = std.event.Channel;
const Timer = std.time.Timer;
const warn = std.debug.warn;
pub const io_mode = .evented;
// Send the sequence 2, 3, 4, ... to channel 'ch'.
fn generate(ch: *Channel(u32)) void {
var i: u32 = 2;

The current implementation of async is modeled on state machines. Every async function has a Frame struct, which looks something like this:

const Frame = struct {
    ptrToFunction: usize,
    state: usize,
    parent_frame: usize,
    ptr_to_return_value: *ReturnType,
    locals: struct { ... },
};
@SpexGuy
SpexGuy / erase.zig
Last active June 3, 2020 18:47
Type Erasure in Zig, untested.
pub const erased_runtime_safety = false;
pub const erased_debug_names = false;
/// Returns a struct containing a field named "unique_global".
/// There is exactly one such unique_global for every passed type.
/// The address of `unique_global` is guaranteed to be unique for each type.
/// If `erased_debug_names` is true, unique_global is of type [N:0]u8
/// and its value is the name of the passed type T. Otherwise unique_global
/// is of type `u8` and its value is meaningless.
@SpexGuy
SpexGuy / select_frames.zig
Created June 15, 2020 18:42
A weird method for frame selection
const SelectFrames = struct {
frames: []@Frame(runInternal),
next_get_result: usize,
next_put_result: usize,
result_frames: []@Frame(awaitResultInternal);
result_values: []Result;
pub fn init(self: *@This(), in_frames: []anyframe->Result) !void {
const wrapped_frames = try allocator.alloc(@Frame(runInternal), in_frames.len);
errdefer allocator.free(wrapped_frames);
@SpexGuy
SpexGuy / stack_alloc.zig
Created October 30, 2020 18:33
A Buffer Stack Allocator for Zig
const std = @import("std");
const testing = std.testing;
const Allocator = std.mem.Allocator;
const Error = Allocator.Error;
const assert = std.debug.assert;
const mem = std.mem;
const math = std.math;
pub fn StackAllocator(comptime buffer_size: comptime_int, comptime max_alignment: comptime_int) type {