Skip to content

Instantly share code, notes, and snippets.

@roundrobin
Created July 24, 2012 17:51
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/3171481 to your computer and use it in GitHub Desktop.
Save roundrobin/3171481 to your computer and use it in GitHub Desktop.
Shape generator
{"description":"Shape generator","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"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,"fullscreen":false,"thumbnail":"http://i.imgur.com/4HLKxr4.png"}
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',"black")
.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 curve3 = '';
for(p in pathWay){
var e1 = pathWay[p];
var x = e1[0];
var y = e1[1];
if(curve3 == ''){
curve3 = 'M'+x+','+y+' ';
}else{
curve3 += 'L'+x+','+y+' '
}
path.attr('Elements',e1)
}
for(p in back){
var e1 = back[p];
var x = e1[0];
var y = e1[1];
if(p == 0){
curve3 += 'M'+x+','+y+' ';
}else{
curve3 += 'L'+x+','+y+' '
}
path.attr('Elements',e1)
}
path.attr('d',curve3)
joined = pathWay.slice().concat(back.slice());
console.log('Joined Lenght',joined.length)
var curve2 = createConvexHull('',joined);
console.log(curve2,'Mam')
//convexHull.attr('d',curve2)
}
function createConvexHull(curve,joined){
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, smi)
}
if(smi == undefined){
console.log('returned 1',curve)
return curve;
}
console.log(smi,joined.length,'DA',curve)
if(curve == '')
curve += 'M'+smi[0]+','+smi[1]+' '
else{
curve += 'L'+smi[0]+','+smi[1]+' '
}
if(smi != undefined){
return createConvexHull(curve,joined)
}else{
console.log('returned')
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