Skip to content

Instantly share code, notes, and snippets.

View ssylvan's full-sized avatar

Sebastian Sylvan ssylvan

View GitHub Profile
So if your projection matrix P =
m11 m12 m13 m14
m21 m22 m23 m24
m31 m32 m33 m34
m41 m42 m43 m44
Then
p_clip = (x_clip,y_clip,z_clip,w_clip)^T = P * (x,y,z,1)^T
We don't actually care about z_clip here, so drop the third row:
// 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 January 17, 2019 19:13
Sudoku solver
#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);
/*
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 March 14, 2016 18:05
Ray-point optimization

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

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});
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,)))
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 December 17, 2015 18:39
Stupid C++ lib for trying to avoid null pointers
// 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 January 12, 2023 04:52
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…
#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