Skip to content

Instantly share code, notes, and snippets.

@saifuddin778
Last active April 19, 2016 16:16
Show Gist options
  • Save saifuddin778/dfacafb60c02e6949b87f7bdf2756eff to your computer and use it in GitHub Desktop.
Save saifuddin778/dfacafb60c02e6949b87f7bdf2756eff to your computer and use it in GitHub Desktop.
Random Fractals - II

Fractal of a form in which an equilateral triangle's base is divided by two child equilateral triangles, and the process is repeated recursively with those children.

<!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], 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_b() {
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);
tpoints.push(rights[j]);
}
//0, 2, 6, 14, 30, 90, 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("stroke", "#2C3E50")
.attr("stroke-width", "0.2")
.style("fill", "rgba(174, 168, 211, 0.15)")
.attr("points", pts);
return;
}
}
fractals_random_b();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment