Skip to content

Instantly share code, notes, and snippets.

@roundrobin
Created July 22, 2012 23:53
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/3161415 to your computer and use it in GitHub Desktop.
Save roundrobin/3161415 to your computer and use it in GitHub Desktop.
just another inlet to tributary
var pathWay = [];
var back = [];
var joined;
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
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(){
console.log('dfgsd')
pathWay.push(d3.mouse(this))
back.push([d3.mouse(this)[0]+100,d3.mouse(this)[1]])
redraw()
}).on('mousedown',function(){
plab = 'sdfds'
}).on('mouseup',function(){
console.log(plab)
})
function redraw(){
var curve = '';
for(i in pathWay){
var e = pathWay[i];
var x = e[0];
var y = e[1];
if(curve == ''){
curve = 'M'+x+','+y+' ';
}else{
curve += 'L'+x+','+y+' '
}
}
for(i in back){
var e = back[i];
var x = e[0];
var y = e[1];
if(i == 0){
curve += 'M'+x+','+y+' ';
} else{
curve += 'L'+x+','+y+' '
}
}
console.log('sdfsdfsdfsdfsdfsd',curve)
path.attr('d',curve)
joined = pathWay.concat(back);
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){
}
}
}
if(curve == '')
curve += 'M'+smi[0]+','+smi[1]+' '
else
curve += 'L'+smi[0]+','+smi[1]+' '
console.log('Curve',curve,'SMI',smi)
return curve;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment