类似于动作脚本的normalize(1)的Javascript函数 [英] Javascript function that works like actionscript's normalize(1)

查看:110
本文介绍了类似于动作脚本的normalize(1)的Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个返回xy点归一化数字的公式-类似于actionscript的normalize()函数.

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

推荐答案

我认为as3

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;
  }
}

这篇关于类似于动作脚本的normalize(1)的Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆