Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created December 31, 2012 21:47
Show Gist options
  • Save zeffii/4423022 to your computer and use it in GitHub Desktop.
Save zeffii/4423022 to your computer and use it in GitHub Desktop.
path_from_function_2
{"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}
var svg = d3.select("svg");
// paths visit http://www.w3.org/TR/SVG11/paths.html
// variables
var bend = 60, // angle to twist (degrees)
iterations = 2, // number of rewrites
F = 60 / iterations, // arm distance
angle_offset = to_rad(bend); // angle as radian
// 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(input_string){
var angle_rad = 0;
var token = "";
var path_string = "M " + 33 + "," + 150;
for (i in d3.range(input_string.length)){
token = input_string[i]
if (token === '-') angle_rad += angle_offset
if (token === '+') angle_rad -= angle_offset
if (token === 'F') path_string += command_from_rad(angle_rad)
}
return path_string
}
function draw_path(path){
svg.append("path")
.attr("d", path)
.style("fill", "none")
.style("stroke", "#0033FF")
.style("stroke-width", 1)
}
var path = make_path("F+F--F+F");
//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