A fractal of the form in which an equilateral triangle gets sliced into two right-angled halves, and each of these halves gets a child equilateral triangle which is processed in the similar fashion.
Last active
April 19, 2016 15:50
-
-
Save saifuddin778/c2918526aabd923772193f145784fd50 to your computer and use it in GitHub Desktop.
Random Fractals - I
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style></style> | |
<body> | |
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
<script> | |
function middle_point(p1, p2) { | |
return [(p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2]; | |
} | |
function get_rights(points) { | |
return [ | |
[ | |
points[0], points[1], middle_point(points[0], points[2]) | |
], | |
[ | |
points[0], middle_point(points[0], points[1]), middle_point(points[0], points[2]) | |
], | |
[ | |
middle_point(points[0], points[2]), middle_point(points[1], points[2]), points[2] | |
] | |
]; | |
} | |
function fractals_random_a() { | |
var margin = { | |
top: 30, | |
right: 60, | |
bottom: 60, | |
left: 30 | |
}; | |
var width = 1024 - margin.left - margin.right; | |
var height = (550 - 10) - margin.top - margin.bottom; | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("id", "gasket_svg") | |
.attr("width", width).attr("height", height); | |
var tpoints = [ | |
[ | |
[(width * 0.2), (height * 0.9)], | |
[(width * 0.5), (height * 0.1)], | |
[(width * 0.8), (height * 0.9)] | |
] | |
]; | |
for (var i = 0; i < tpoints.length; i++) { | |
append_(tpoints[i], i); | |
var rights = get_rights(tpoints[i]); | |
for (var j = 0; j < rights.length; j++) { | |
append_(rights[j], j); | |
//only push the triangles into the tpoints to be processed in the queue | |
if (j > 0) { | |
tpoints.push(rights[j]); | |
} | |
} | |
//follows this cycle: 0, 2, 6, 14, 30, 90, 126 | |
//terminating at i = 126 | |
if (i == 126) { | |
break; | |
} | |
} | |
function append_(rtrng, i_) { | |
var pts = rtrng[0][0] + "," + rtrng[0][1] + ", " + rtrng[1][0] + "," + rtrng[1][1] + ", " + rtrng[2][0] + "," + rtrng[2][1]; | |
svg.append("polygon") | |
.attr("class", "children") | |
.style("stroke", "#00B16A") | |
.style("stroke-width", "0.2") | |
.style("fill", "rgba(27, 188, 155, 0.06)") | |
.attr("points", pts); | |
return; | |
} | |
} | |
fractals_random_a(); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment