function sArt(s){ /* s should have the structure { name:, nails:[{s:{x:,y:},e:{x:,y:},n:},...], strings:[{n1:,n2:,se:,c:,th:},...], nFlag: } where name = name to identify the design, nails = array of nail groups, nails[i].s = coordinates of starting nail of i-th nail group, nails[i].e = coordinates of ending nail of i-th nail group, nails[i].n = number of nails in i-th nail group, strings = array of strings, strings[i].se = 0 if start nail is mapped to start nail of the groups, strings[i].n1 = index of the first nail group in nails array for i-th string group, strings[i].n2 = index of the second nail group in nails array for i-th string group, strings[i].c = colour of strings for i-th string group, strings[i].th = thickness for strings in i-th group, nFlag = 0/1 flag to enable or disable the display of nails. */ function drawNails(sa){ sa.nails.forEach(function(d){ var nr = 2;// nail radius var p = d3.range(0,d.n+1).map(function(i){ return {x:d.s.x+(i/d.n)*(d.e.x-d.s.x),y:d.s.y+(i/d.n)*(d.e.y-d.s.y)}; }); d3.select("#nails").append("g").selectAll("circle").data(p).enter().append("circle") .attr("cx", function(d){ return d.x;}).attr("cy",function(d){ return d.y;}) .attr("r",nr); }); } function drawStrings(sa){ sa.strings.forEach(function(d){ var s1=sa.nails[d.n1].s; var e1=sa.nails[d.n1].e; var s2= d.se==0? sa.nails[d.n2].s : sa.nails[d.n2].e; var e2= d.se==0? sa.nails[d.n2].e : sa.nails[d.n2].s; //assume sa.nails[d.n1].n == sa.nails[d.n2].n; var p = d3.range(0,sa.nails[d.n1].n+1).map(function(l){ return {x1: s1.x + (l/sa.nails[d.n1].n)*(e1.x-s1.x), y1: s1.y + (l/sa.nails[d.n1].n)*(e1.y-s1.y), x2: s2.x + (l/sa.nails[d.n1].n)*(e2.x-s2.x), y2: s2.y + (l/sa.nails[d.n1].n)*(e2.y-s2.y),}; }); d3.select("#strings").append("g").selectAll("line").data(p).enter().append("line") .attr("x1",function(t){return t.x1;}).attr("y1",function(t){return t.y1;}) .attr("x2",function(t){return t.x2;}).attr("y2",function(t){return t.y2;}) .style("stroke", function(){ return d.c}).style("stroke-width", function(){ return d.th;}); }); } if(s.nFlag==undefined || s.nFlag==1) drawNails(s); drawStrings(s); }