Gist #4423022 [ Launch Inlet ]
Gist #4421006 [ Launch Inlet ]
Gist #4420916 [ Launch Inlet ]
Gist #4420154 [ Launch Inlet ]
Gist #4420141 [ Launch Inlet ]
Gist #3200444
-
-
Save anonymous/4425074 to your computer and use it in GitHub Desktop.
path_from_function_2
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
{"description":"path_from_function_2","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.36369165867866515,"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} |
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
var svg = d3.select("svg"); | |
// paths visit http://www.w3.org/TR/SVG11/paths.html | |
// variables | |
var bend = 60, // angle to twist (degrees) | |
iterations = Math.floor(4.15263), // number of rewrites | |
F = 260/(Math.pow(3,iterations)), // arm distance | |
angle_offset = to_rad(bend), // angle as radian | |
angle_rad = 0, // the current angle for drawing | |
x_start = 33, // drawing start x | |
y_start = 150, // drawing start y | |
rules = {}; // a map of the rules | |
// converting from resulting string to svg path commands --------------- | |
function to_rad(degrees){ | |
return degrees * (2*Math.PI) / 360 | |
} | |
function get_xy(angle_rad){ | |
return {x: Math.cos(angle_rad) * F, | |
y: Math.sin(angle_rad) * F} | |
} | |
function command_from_rad(angle_rad){ | |
var c = get_xy(angle_rad); | |
return " l " + c.x + " " + c.y | |
} | |
function make_path(rules, start_string){ | |
return "M " + x_start + "," + y_start + recursive_path(rules,start_string, 0); | |
} | |
function recursive_path(rules, input_string, iteration) { | |
var token = ""; | |
var path_string = ""; | |
for (i in d3.range(input_string.length)) { | |
token = input_string[i]; | |
if (token === '-') angle_rad += angle_offset; | |
else if (token === '+') angle_rad -= angle_offset; | |
else if (token in rules) { | |
// if we're at the bottom level, move forward | |
if (iteration >= iterations) path_string += command_from_rad(angle_rad); | |
// else go deeper down the rabbit hole... | |
else path_string += recursive_path(rules, rules[token], iteration+1); | |
} | |
} | |
return path_string; | |
} | |
function draw_path(path){ | |
svg.append("path") | |
.attr("d", path) | |
.style("fill", "none") | |
.style("stroke", "#0033FF") | |
.style("stroke-width", 1) | |
} | |
rules["F"] = "F+G--F+F"; | |
rules["G"] = "F---F+F"; | |
start = "F"; | |
var path = make_path(rules, start); | |
//var path = make_path("F+F--F+F+F+F--F+F--F+F--F+F+F+F--F+F+F+F--F+F+F+F--F+F--F+F--F+F+F+F--F+F--F+F--F+F+F+F--F+F--F+F--F+F+F+F--F+F+F+F--F+F+F+F--F+F--F+F--F+F+F+F--F+F") | |
draw_path(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment