Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript function that works like actionscript's normalize(1)

I need a formula that returns normalize numbers for xy point - similar to actionscript's normalize() function.

var normal = {x:pt1.x-pt2.x,y:pt1.y-pt2.y};

normal = Normalize(1) // this I do not know how to implement in Javascript
like image 426
user433872 Avatar asked Jan 21 '23 12:01

user433872


1 Answers

I think the as3 normalize function is just a way to scale a unit vector:

function normalize(point, scale) {
  var norm = Math.sqrt(point.x * point.x + point.y * point.y);
  if (norm != 0) { // as3 return 0,0 for a point of zero length
    point.x = scale * point.x / norm;
    point.y = scale * point.y / norm;
  }
}
like image 152
Patrick Avatar answered Feb 22 '23 01:02

Patrick