Skip to content

Instantly share code, notes, and snippets.

@roundrobin
Created July 23, 2012 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roundrobin/3161464 to your computer and use it in GitHub Desktop.
Save roundrobin/3161464 to your computer and use it in GitHub Desktop.
just another inlet to tributary
var pathWay = [];
var back = [];
var joined;
var path = g.append('path')
.attr('stroke','red')
.attr('stroke-width','2')
.attr('fill',"none")
.attr('id','path')
.attr('fill-opacity','0.5')
.attr('class','drawit')
.attr('transform','scale(1)')
.attr('id','pathWay')
.attr('d','')
var convexHull = g.append('path')
.attr('stroke','red')
.attr('stroke-width','2')
.attr('fill',"none")
.attr('id','path')
.attr('fill-opacity','0.5')
.attr('class','drawit')
.attr('transform','scale(1)')
.attr('id','pathWay')
.attr('d','')
g.on('click',function(){
var point = d3.mouse(this);
pathWay.push(point)
back.push([point[0]+100,point[1]])
redraw()
}).on('mousedown',function(){
plab = ''
}).on('mouseup',function(){
console.log(plab)
})
function redraw(){
var curve = '';
console.log('Pathway',pathWay)
for(p in pathWay){
var e1 = pathWay[p];
var x = e1[0];
var y = e1[1];
if(curve == ''){
curve = 'M'+x+','+y+' ';
}else{
curve += 'L'+x+','+y+' '
}
path.attr('Elements',e1)
}
for(p in back){
var e1 = back[p];
var x = e1[0];
var y = e1[1];
console.log('Shapr Ele',p)
if(p == 0){
curve += 'M'+x+','+y+' ';
}else{
curve += 'L'+x+','+y+' '
}
path.attr('Elements',e1)
}
path.attr('d',curve)
console.log('Curve',curve,pathWay.length)
joined = pathWay.slice().concat(back.slice());
console.log('Joined',joined)
var curve = createConvexHull('');
//convexHull.attr('d',curve)
}
function createConvexHull(curve){
var smi;
var index;
var changed = false;
for(i in joined){
var current = joined[i];
var x = current[0];
var y = current[1];
if(!smi){
smi = current;
changed = true;
index = i;
}else{
if(x < smi[0]){
smi = current;
changed = true;
index = i;
}
if(x == smi[0] && y < smi[1]){
smi = current;
changed = true;
index = i;
}
}
}
if(changed){
removeByElement(joined, index)
console.log('Removed',joined)
}
if(curve == '')
curve += 'M'+smi[0]+','+smi[1]+' '
else
curve += 'L'+smi[0]+','+smi[1]+' '
console.log('Curve',curve,'SMI',smi)
return curve;
}
function removeByElement(arrayName,arrayElement)
{
for(var i=0; i<arrayName.length;i++ )
{
if(arrayName[i]==arrayElement)
arrayName.splice(i,1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment