Skip to content

Instantly share code, notes, and snippets.

@Chlumsky
Chlumsky / fft.hpp
Last active April 28, 2022 11:41
Fast Fourier Transform
#include <artery-math.h>
using namespace artery;
// Fast Fourier Transform implementation by Viktor Chlumsky
template <typename T, typename U>
void fft(Complex<T> *dst, const U *src, int length, int stride = 1) {
if (!(length >>= 1)) {
*dst = *src;
return;
@Chlumsky
Chlumsky / levenshtein.hpp
Last active July 28, 2021 16:14
Modified Levenshtein distance
#pragma once
#include <algorithm>
#include <vector>
template <typename T> struct NeverEmptyComparator { bool operator==(const T &) const { return false; } };
/// Computes the Levenshtein distance between a and b but insertion/deletion of elements equal to EmptyComparator() is free
template <typename T, class EmptyComparator = NeverEmptyComparator<T> >
@Chlumsky
Chlumsky / aztec-diamond-animation.shadron
Created December 29, 2020 00:51
Aztec Diamond Smooth Animation
#if !defined(SHADRON_VERSION) || SHADRON_VERSION < 140
#error This script requires Shadron 1.4 or later
#endif
#include <rng>
#include <shapes>
#include <linearstep>
////////////////////////////////////////////////////////////////
#include <math_constants>
image Input = file("maze.png");
glsl vec4 initialState(vec2 pos) {
vec4 input = texture(Input, pos);
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
// Detect starting point
if (input.g > 0.5 && input.r + input.b < 1.0)
glsl {
// Signed distance from half-plane defined by oriented line segment (a, b)
float halfPlaneDistance(vec2 pos, vec2 a, vec2 b) {
return dot(normalize(vec2(b.y-a.y, a.x-b.x)), a-pos);
}
// Anti-aliased half-plane defined by oriented line segment (a, b)
float halfPlaneSmooth(vec2 pos, vec2 a, vec2 b, float border) {
#include <math_constants>
#include <shapes>
const vec3 WHITE = vec3(1.0, 1.0, 1.0);
const vec3 RED = vec3(0.698, 0.132, 0.203);
const vec3 BLUE = vec3(0.234, 0.233, 0.430);
#define A 1.0
#define B 1.9
#include <billboard>
#include <perlin>
#include <hsv>
param float OPACITY = 0.25;
glsl struct ParticleData {
vec2 position;
float phase;
#include <math_constants>
#include <affine_transform>
#include <lighting>
param int sides = 3 : logrange(3, 12);
param float zRotation : range(-PI, PI);
param float xRotation : range(-PI, PI);
glsl struct FragmentData {
#include "plot.shadron"
image Input = file("julia.png") : map(clamp);
#define PXSIZE shadron_PixelSize
param int STEPS = 8;
param float SIGMA = 1;
param float blurRange = 8 : range(256);
#define PLOT_SAMPLES 3
template <FN, XMIN, XMAX, YMIN, YMAX, XGRID, YGRID>
glsl vec4 plot(vec2 pos) {
vec2 tPos = vec2(
mix(float(XMIN), float(XMAX), pos.x),
mix(float(YMIN), float(YMAX), pos.y)
);
vec2 pxSize = vec2((XMAX)-(XMIN), (YMAX)-(YMIN))*shadron_PixelSize;