Skip to content

Instantly share code, notes, and snippets.

--a line from x1, y1 to x2, y2 as t varies from 0 to 1
function line(x1, y1, x2, y2)
return function(t)
return x1 + (x2 - x1) * t, y1 + (y2 - y1) * t
end
end
--the Bezier curve formed by interpolating between multiple given curves or lines
function curve(c1, c2, c3, ...)
@scythe
scythe / bezier.lua
Created June 27, 2011 16:02
de Casteljau's algorithm in lua metatables
--[[a parametric function describing the Bezier curve determined by given control points,
which takes t from 0 to 1 and returns the x, y of the corresponding point on the Bezier curve]]
function bezier(xv, yv)
local reductor = {__index = function(self, ind)
return setmetatable({tree = self, level = ind}, {__index = function(curves, ind)
return function(t)
local x1, y1 = curves.tree[curves.level-1][ind](t)
local x2, y2 = curves.tree[curves.level-1][ind+1](t)
return x1 + (x2 - x1) * t, y1 + (y2 - y1) * t
end
@scythe
scythe / p357.lua
Created February 12, 2012 02:53
357
--Solves Project Euler problem 357.
function filltablesieve(lim, qf)
local sievetable = {}
for i = 1, lim do sievetable[i] = false end --use optimized tables
for i = 1, lim do
for j = i, lim / i, 1 do
sievetable[j * i] = sievetable[j*i] or qf(i, j)
end
end
//big num library in Rust
static MD: int = 1000000000;
enum BigNumber {
End(int),
Segment(int, ~BigNumber, int)
}
fn make(val: int) -> ~BigNumber {
if(val > MD) { ~Segment(val/MD, ~End(val % MD), 1) }
--an implementation of smoothsort.
--the code is in a "monadic" style for fun.
--you should [probably] not actually code like this.
function leonardo(n)
local function lc(i, j, n)
return n > 1 and lc(j, i+j+1, n-1) or j
end
return lc(1, 1, n)
end
@scythe
scythe / gist:24a697b3c9059ed69d52
Created August 13, 2014 00:47
my terminal colors
<gconf>
<entry name="bold_color_same_as_fg" mtime="1406920645" type="bool" value="false"/>
<entry name="use_theme_colors" mtime="1406919946" type="bool" value="false"/>
<entry name="palette" mtime="1406928675" type="string">
<stringvalue>#1036069804A5:#858514144B4A:#2A2ABBBB3938:#7D7D99993030:#000044437777:#61600D0DC9C9:#3939CCCCAAA9:#BA06B314974C:#6C5F5FE3542B:#FFFF41413636:#0100FFFF7070:#FFFFDCDC0000:#00007473D9D9:#F0F01211BEBE:#6F6FDBDBFFFF:#F241F4E7D0FB</stringvalue>
</entry>
<entry name="alternate_screen_scroll" mtime="1406145628" type="bool" value="true"/>
<entry name="background_color" mtime="1406920886" type="string">
<stringvalue>#1036069804A5</stringvalue>
</entry>
@scythe
scythe / raj.lua
Last active August 29, 2015 14:05
A JSON parser in LPeg.re
re = require "re"
local function condense(keyvals)
local obj = {}
for i = 1, #keyvals do
obj[keyvals[i][1]] = keyvals[i][2]
end
return obj
end
@scythe
scythe / gist:d28c3f4933ff2f1e5c47
Last active August 29, 2015 14:06
transducers in lua
--Rich Hickey's infamous transducers, demonstrating reversed function composition.
--Look closely.
function transduce(tf, rf, init, iter)
for i in iter do
init = tf(rf)(init, i)
end
return init
end
@scythe
scythe / va_stringify.h
Created January 21, 2015 19:27
Convert variadic macro arguments to strings
/* STR_VA_ARG(...) -- convert variadic arglist to strings
* Usage: STR_VA_ARG(phnglui, mglwnafh, cthulhu, rlyeh, wgahnagl, fhtagn)
* >> "phnglui", "mglwnafh", "cthulhu", "rlyeh", "wgahnagl", "fhtagn"
*/
#ifndef __VA_STRINGIFY_H
#define __VA_STRINGIFY_H
#define PP_NARG(...) \
PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
@scythe
scythe / gist:844a4977fa22b67e5c9e
Last active August 29, 2015 14:17
hofstadter_female_differences.lua
f = {[0] = 1}
m = {[0] = 0}
len = 100000000
for i = 1, len do
m[i] = i - f[m[i-1]]
f[i] = i - m[f[i-1]]
end