Skip to content

Instantly share code, notes, and snippets.

Sebastian Sylvan ssylvan

Block or report user

Report or block ssylvan

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:80debcac11605f357e5a45d7aa55058d
// Example:
ctx.ClearScreen();
let ctx = ctx.BeginTriangleStrip(); // Old context is *consumed*, new context has different methods
// ctx.ClearScreen(); // ERRROR: not supported on this context
ctx.AddVertex(...);
ctx.AddVertex(...);
ctx.AddVertex(...);
let ctx = ctx.End();
@ssylvan
ssylvan / main.cpp
Last active Jan 17, 2019
Sudoku solver
View main.cpp
#include <intrin.h>
#include <assert.h>
#include <vector>
#include <chrono>
#include <memory>
int get_set_bit(uint16_t x) {
assert(__popcnt16(x) == 1);
unsigned long index;
_BitScanForward(&index, x);
View gist:d193d2bbb838e9e4a452658af23b71d4
/*
Need cost function to divide by z, so that the errors we minimize are _projected_ errors.
Without it, 1px error might be 1mm if the ball is close up, or 100mm if the ball is further away.
Thus to minimize error the optimizer would move the ball closer. By optimizing projected error there's no such bias.
*/
float CostFunction(vec3 dir, float r, vec3 pos)
{
// Compute distance to the sphere surface
vec3 posOnSphere = dir*dot(dir, pos);
vec3 dirToRay = posOnSphere - pos;
@ssylvan
ssylvan / gist:0309be58e5448e3c7989
Last active Mar 14, 2016
Ray-point optimization
View gist:0309be58e5448e3c7989

I would've derived it differnetly. Rather than explicitly setting up the equation for squared distances then finding the solution that minimizes it, I would've just used the normal equations to do the least squares part for me. IMO this makes it a bit more "mechanical" in figuring out the soluion (I just need to figure out what equation I'm trying to solve, the normal equation can be memorized and will figure out how to compute the least squares solution to it).

So we already have the equation from the stackexchange thing for computing the "offset vector" from a point to the line. You can express this in different ways, but they way they did it is fine. Basically the 3D vector telling us how far a point x is from the line described by an origin o and a direction u.

ray_to_point_offset = (I - u*ut)*x - (I-u*ut)*o

That first term is "x projected onto a plane with normal u", and the second term is "ray origin projected to the same plane". So the difference between the two is just finding the perpendicu

View gist:8b19fb016983a1ae38b7
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const jsdom = require("jsdom").jsdom;
const cwd = path.resolve(process.argv[2]);
console.log(cwd);
var files = glob.sync("*.html", {cwd: path.resolve(process.argv[2]), matchBase:true, follow: true, realpath:true});
View gist:ab93a39a8c2aa1ce35ad
1. AND
2. OR
3. Parity of three bits
inv(x) = nand(x,x)
and(x,y) = inv(nand(x,y))
or(x,y) = nand(inv(x), inv(y))
xor(x,y) = or(x & inv(y), y & inv(x)) = inv(nand(x&inv(y), y&inv(x))) = inv(nand(nand(x, inv(y)), nand(y, inv(x))))
xnor(x,y) = nand(nand(x, inv(y)), nand(y, inv(x)))
par(x,y,z) = or(and(inv(x), xnor(y,z)), and(x, xor(y,z,)))
View Trouble setting sparse matrix elements
Eigen::SparseMatrix<float> A(numRows, numCols);
Eigen::SparseVector<float> r(numCols);
A.row(0) = r;
// Comment the last line out, and no compiler errors.
// Leave it in and get errors about missing resize:
// eigen\src\sparsecore\sparsematrixbase.h(209): error C2039: 'resize': is not a member of 'Eigen::Block<Derived,1,-1,false>'
@ssylvan
ssylvan / gist:5654932
Last active Dec 17, 2015
Stupid C++ lib for trying to avoid null pointers
View gist:5654932
// Class for non-nullable pointers
template<class T>
class ptr
{
private:
T* m_ptr;
ptr(nullptr_t); // avoids init with nullptr
ptr(int); // avoids init with 0 or NULL
public:
explicit ptr(T* ptr) throw() : m_ptr(ptr) { assert(ptr != nullptr); };
@ssylvan
ssylvan / rh_hash_table.hpp
Last active Sep 16, 2019
Quick'n'dirty Robin Hood hash table implementation. Note, I have implemented this algorithm before, with tons of tests etc. But *this* code was written specifically for the blog post at http://sebastiansylvan.com/post/robin-hood-hashing-should-be-your-default-hash-table-implementation/, it has not been extensively tested so there may be bugs (an…
View rh_hash_table.hpp
#define USE_ROBIN_HOOD_HASH 1
#define USE_SEPARATE_HASH_ARRAY 1
template<class Key, class Value>
class hash_table
{
static const int INITIAL_SIZE = 256;
static const int LOAD_FACTOR_PERCENT = 90;
struct elem
You can’t perform that action at this time.