Skip to content

Instantly share code, notes, and snippets.

@jfhbrook
Created August 26, 2011 20:00
Show Gist options
  • Select an option

  • Save jfhbrook/1174289 to your computer and use it in GitHub Desktop.

Select an option

Save jfhbrook/1174289 to your computer and use it in GitHub Desktop.
// Something on the internet reminded me of triangles.
var _ = require("underscore");
//Some basic vector maths
var v = {
add: function(u,v) {
return _.zip(u,v).map(function(t) { return t[0]+t[1]; });
},
sub: function(u,v) {
return _.zip(u,v).map(function(t) { return t[0]-t[1]; });
},
norm: function(u) {
return Math.pow(u.reduce(function(acc, x) {
return acc + Math.pow(x, 2);
}, 0), 0.5);
}
};
// Three points representing the vertices of a triangle, then side lengths.
// A combinations function would increase the DRYness.
var a = [0, 0],
b = a.map(function() { return Math.random(); }),
c = a.map(function() { return Math.random(); }),
ab = v.norm(v.sub(a,b)),
ac = v.norm(v.sub(a,c)),
bc = v.norm(v.sub(b,c));
// http://en.wikipedia.org/wiki/Area_of_triangle#Using_coordinates as it turns out
var area = (function (b,c) {
return Math.abs(b[0]*c[1] - c[0]*b[1])/2;
})(v.sub(b,a), b.sub(c,a));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment