Skip to content

Instantly share code, notes, and snippets.

@saifuddin778
Last active May 23, 2016 02:50
Show Gist options
  • Save saifuddin778/ecc696ab0e3e3486c9867c764a32bf12 to your computer and use it in GitHub Desktop.
Save saifuddin778/ecc696ab0e3e3486c9867c764a32bf12 to your computer and use it in GitHub Desktop.
Random Fractals - IV

Theme: A circle, surrounded by four small circles.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
text-align: center;
font-family: monospace !important;
}
circle {
stroke: black;
stroke-width: 0.1;
fill: rgba(192, 57, 43, 0.2);
}
</style>
<body>
<div id="plot"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var width = 500;
var height = 500;
function get_mp(p1, p2) {
return [(p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2];
}
function get_polygon_points(points) {
var pts = "";
for (var i = 0; i < points.length; i++) {
for (var j = 0; j < points[i].length; j++) {
if (i < points.length - 1) {
pts += points[i][j] + ",";
} else {
pts += points[i][j] + " ";
}
}
pts += " "
}
return pts;
}
function get_children(box) {
var children = [
[
[box[0][0], box[0][1]],
[box[0][0], (box[0][1] + box[1][1]) / 2],
[(box[0][0] + box[3][0]) / 2, (box[0][1] + box[1][1]) / 2],
[(box[0][0] + box[3][0]) / 2, box[0][1]]
],
[
[box[0][0], (box[0][1] + box[1][1]) / 2],
[box[0][0], box[1][1]],
[(box[1][0] + box[2][0]) / 2, box[1][1]],
[(box[0][0] + box[3][0]) / 2, (box[0][1] + box[1][1]) / 2],
],
[
[(box[0][0] + box[3][0]) / 2, (box[0][1] + box[1][1]) / 2],
[(box[1][0] + box[2][0]) / 2, box[1][1]],
[box[2][0], box[2][1]],
[box[2][0], (box[2][1] + box[3][1]) / 2]
],
[
[(box[0][0] + box[3][0]) / 2, box[0][1]],
[(box[0][0] + box[3][0]) / 2, (box[0][1] + box[1][1]) / 2],
[box[2][0], (box[0][1] + box[1][1]) / 2],
[box[2][0], box[3][1]]
]
];
return children;
}
function add_circle(box) {
var w = box[3][0] - box[0][0];
space.append("circle")
.attr("cx", (box[0][0] + box[3][0]) / 2)
.attr("cy", (box[0][1] + box[1][1]) / 2)
.attr("r", w / 12);
return;
}
var space = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
var boxes = [
[
[0, height],
[0, 0],
[width, 0],
[height, width]
]
];
for (var i = 0; i < boxes.length; i++) {
add_circle(boxes[i]);
var children = get_children(boxes[i]);
for (var j = 0; j < children.length; j++) {
add_circle(children[j]);
boxes.push(children[j]);
}
//4, 20, 84, 420, 1364
if (i == 1364) {
break;
}
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment