Skip to content

Instantly share code, notes, and snippets.

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

Fractal of the form in which an equilateral triangle's base is divided into 2 child equilateral triangles, and a third child is inserted exactly in the middle of two children. Moving forward, these three children are processed recursively until a particular cutoff limit is reached.

<!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], middle_point(points[0], points[2])),
middle_point(middle_point(points[0], points[1]), middle_point(points[1], points[2])),
middle_point(middle_point(points[0], points[2]), points[2])
],
[
middle_point(points[0], points[2]),
middle_point(points[1], points[2]),
points[2]
]
];
}
function fractals_random_c() {
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]);
var rights = get_rights(tpoints[i]);
for (var j = 0; j < rights.length; j++) {
append_(rights[j]);
tpoints.push(rights[j]);
}
if (i == 1000) { //terminating at some random limit
break;
}
}
d3.selectAll('polygon')
.on('mouseover', function() {
return $(this).attr("stroke", "#F9690E");
d3.event.stopPropagation();
})
.on('mouseout', function() {
return $(this).attr("stroke", "none");
d3.event.stopPropagation();
});
function append_(rtrng) {
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", "none")
.attr("stroke-width", "0.6")
.attr("shape-rendering", "geometricPrecision")
.style("fill", "rgba(30, 139, 195, 0.06)")
.attr("points", pts);
return;
}
}
fractals_random_c();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment