Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created January 1, 2013 20:06
Show Gist options
  • Save enjalot/4429648 to your computer and use it in GitHub Desktop.
Save enjalot/4429648 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
//max perimeter (CAUTION: high values take a long time to calculate)
var perimeter = 578;
//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(["#0A8B58", "#971B52"])
.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(" + 10.08 + ")"
})
.style({
"stroke": function(d,i) {
return colorScale(d.A+d.B+d.C);
},
"stroke-width":0.35693,
"stroke-opacity": 0.31329
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment