Skip to content

Instantly share code, notes, and snippets.

@timknip
timknip / DaeAnimation.js
Created August 16, 2011 02:59
animation proposal
var curve = new THREE.DaeAnimationCurve();
curve.addKey(new THREE.DaeAnimationKey(0, 0));
curve.addKey(new THREE.DaeAnimationKey(1, Math.PI*0.5));
curve.addKey(new THREE.DaeAnimationKey(2, Math.PI));
curve.addKey(new THREE.DaeAnimationKey(3, Math.PI*1.5));
curve.addKey(new THREE.DaeAnimationKey(4, Math.PI*2));
var channel = new THREE.DaeAnimationChannel(dae.rotation, "x");
channel.addCurve(curve);
@timknip
timknip / setFromRotationMatrix.js
Created August 18, 2011 16:54
Quaternion setFromRotationMatrix
setFromRotationMatrix: function ( m ) {
// Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
function copySign(a, b) {
return b < 0 ? -Math.abs(a) : Math.abs(a);
}
var absQ = Math.pow(m.determinant(), 1.0 / 3.0);
this.w = Math.sqrt( Math.max( 0, absQ + m.n11 + m.n22 + m.n33 ) ) / 2;
this.x = Math.sqrt( Math.max( 0, absQ + m.n11 - m.n22 - m.n33 ) ) / 2;
this.y = Math.sqrt( Math.max( 0, absQ - m.n11 + m.n22 - m.n33 ) ) / 2;
this.z = Math.sqrt( Math.max( 0, absQ - m.n11 - m.n22 + m.n33 ) ) / 2;
@timknip
timknip / MatrixDecompose.js
Created August 22, 2011 00:31
Matrix decomposition
/**
* Decompose a THREE.Matrix4 into translation, rotation and scale.
*/
var translation = new THREE.Vector3();
var rotation = new THREE.Quaternion();
var scale = new THREE.Vector3();
// decompose!
matrix.decompose(translation, rotation, scale);
@timknip
timknip / gist:1467615
Created December 12, 2011 14:49
point in polygon
pointInside: function(pt) {
var i, j, a, b, xpi, ypi, xpj, ypj;
var x = pt.x;
var y = pt.y;
var c = false;
for (i = 0; i < this.points.length; i++) {
j = (i+1) % this.points.length;
a = this.points[i];
b = this.points[j];
xpi = a.x;
@timknip
timknip / gist:2724963
Created May 18, 2012 12:10
clone graph
var G = new networkx.Graph();
var tmp = new Map();
forEach (graph.getNodes(), function(n) {
tmp.set(n, new Point2(n.x, n.y));
});
forEach (graph.getEdges(), function(e) {
G.addEdge(tmp.get(e[0]), tmp.get(e[1]));
});
listen(this.view.getElement(), ['mousedown', 'touchstart'], function(e) {
var v = this.listenVertex_.get(goog.getUid(e.target));
if (v) {
dispatchEvent(this, new SelectionEvent(this, e, v));
} else {
var edge = this.listenEdge_.get(goog.getUid(e.target));
if (edge) {
dispatchEvent(this, new SelectionEvent(this, e, edge));
}
}
console.log size.x, size.y, size.z
min = gl.vec3().set(vmax, vmax, vmax)
max = gl.vec3().set(vmin, vmin, vmin)
vs = [
gl.vec3().set(-size.x/2, -size.y/2, -size.z/2),
gl.vec3().set( size.x/2, -size.y/2, -size.z/2),
gl.vec3().set( size.x/2, size.y/2, -size.z/2),
gl.vec3().set(-size.x/2, size.y/2, -size.z/2),
gl.vec3().set(-size.x/2, -size.y/2, size.z/2),
(defn crop-old
[ctx w h]
(let [data (.-data (.getImageData ctx 0 0 w h))
s (* w h 4)
[a b] (loop [y 0 px [] py []]
(if (< y h)
(let [[x' y']
(loop [x 0 px' px py' py]
(if (< x w)
(let [a? (> (aget data (+ (* (+ (* y w) x) 4) 3)) 0)]
(defn trimmed-bounds
[ctx w h]
(let [data (.-data (.getImageData ctx 0 0 w h))]
(loop [y 0 minx Infinity miny Infinity maxx -Infinity maxy -Infinity]
(if (< y h)
(let [yw (* y w)
minx' (loop [x 0]
(when (< x w)
(if (= (aget data (+ (* (+ yw x) 4) 3)) 0)
(recur (inc x))
@timknip
timknip / gist:9f034c4bc8d048a7ae74e977004c8322
Created March 20, 2018 11:52
potrace - transparent png
pngtopnm -mix assign.png > a.pnm && potrace a.pnm -s -o a.svg