I try to use this calculation a lot in the things I do, so I like to add it to the Math object:
Math.dist=function(x1,y1,x2,y2){ if(!x2) x2=0; if(!y2) y2=0; return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); } Math.dist(0,0, 3,4);
Update:
this approach is especially pleasing when you find yourself in situations similar to this (I often do this):
varName.dist=Math.sqrt( ( (varName.paramX-varX)/2-cx )*( (varName.paramX-varX)/2-cx ) + ( (varName.paramY-varY)/2-cy )*( (varName.paramY-varY)/2-cy ) );
this terrible thing becomes much more manageable:
varName.dist=Math.dist((varName.paramX-varX)/2, (varName.paramY-varY)/2, cx, cy);
jaya Oct 27 '15 at 3:54 on 2015-10-27 03:54
source share