Skip to content

Instantly share code, notes, and snippets.

/_.md

Created January 2, 2013 12:52
Show Gist options
  • Save anonymous/4434389 to your computer and use it in GitHub Desktop.
Save anonymous/4434389 to your computer and use it in GitHub Desktop.
Pythagorean Triples
{"description":"Pythagorean Triples","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"tab":"edit","display_percent":0.630672268907563,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"hidepanel":false}
//draws all pythagorean triples with perimeters less than specified
d3.select('body').style('background', '#121212')
//max perimeter (CAUTION: high values take a long time to calculate)
var perimeter = 265;
//storage for the triples
var triples = [];
//calculate the triples (brute force)
for(var c=1; c<perimeter; c++)
for(var b=1; b<(perimeter-c); b++)
for(var a=1; a<(perimeter-(b+c)); a++)
if((a*a + b*b) === c*c)
triples.push({A:a, B:b, C:c});
//draw the triples
var svg = d3.select("svg");
var triangles = svg.append("svg:g");
//var colorScale = d3.scale.category20();
//var colorScale = d3.scale.ordinal()
// .range(["#0A8B58", "#1B8397", "#B63B3B"])
var colorScale = d3.scale.linear()
.domain([0, perimeter])
.range(["#FFFFFF", "#B4C1C7"])
.interpolate(d3.interpolateHcl)
//.interpolate(d3.interpolateHsl)
triangles.selectAll().data(triples).enter().append('line')
.attr({
"x1":function(d){return d.A},
"y2":function(d){return d.B},
"transform": "scale(" + 4.72 + ")"
})
.style({
"stroke": function(d,i) {
return colorScale(d.A+d.B+d.C);
},
"stroke-width":229.70825,
"stroke-opacity": 0.03368
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment