小编Tru*_*246的帖子

两个线段之间的最短距离

我需要一个函数来找到两个线段之间的最短距离.线段由两个端点定义.因此,例如我的一个线段(AB)将由两个点A(x1,y1)和B(x2,y2)定义,而另一个(CD)将由两个点C(x1,y1)定义和D(x2,y2).

随意用您想要的任何语言编写解决方案,我可以将其翻译成javascript.请记住,我的几何技能非常生疏.我已经在这里看到,我不知道如何将其转换为函数.非常感谢你的帮助.

language-agnostic geometry

15
推荐指数
4
解决办法
3万
查看次数

服务器端NodeJS中requestAnimationFrame()的实现

关于广泛使用的requestAnimationFrame()函数,我有一些问题.最近我在多人游戏中遇到了一些在客户端而不是服务器端使用它的实现.

  1. 这样做有什么好处吗?
  2. 你能引用我在NodeJS中的任何"最佳实践"服务器端实现吗?

更新

我在动画和游戏循环之间有点混淆 - 我正在寻找的是NodeJS中的实现=>例如setInterval.

示例 - 客户端实现

(function () {
    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
        window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
        window.cancelAnimationFrame =
            window[vendors[x] + 'CancelAnimationFrame'] ||
            window[vendors[x] + 'CancelRequestAnimationFrame'];
    }

    if (!window.requestAnimationFrame)
        window.requestAnimationFrame = function (callback, element) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max(0, 16 - (currTime - lastTime));
            var id = …
Run Code Online (Sandbox Code Playgroud)

javascript client node.js server

10
推荐指数
1
解决办法
5569
查看次数

如何在javascript中获取嵌套数组中某个值的indexOf位置?

免责声明:这个问题并不像同此一个.

我有一个嵌套数组的例子:

var testArray = [
  true,
  "",
  [
    1,
    {a: 2, b: [3, false]},
    [2, []],
    null
  ],
  4,
  undefined,
  [5, "test"],
  function() {}
];
Run Code Online (Sandbox Code Playgroud)

如何在嵌套数组中获取valueOf,如:

testArray.multiIndexOf(null); //Expected result will be [2, 3]
Run Code Online (Sandbox Code Playgroud)

我将解释这里发生的事情.

首先,我们将testArray分解为:

var testArrayExplain = [0, 1, [0, 1, [0, 1], 3], 3, 4, [0, 1], 6];
Run Code Online (Sandbox Code Playgroud)

正如你在这里看到的,这个数组和上面的数组一样:第一层数组的长度为7,然后看一下位置#2的值,我们将看到另一个数组嵌套到第一层.正如您从上面记得的那样,该null值与第一个数组的位置#2处的数组中的位置#3具有相同的确切位置

因此,我们将具有第一层位置,然后是第二层位置null:,[2, 3]存储在从第一层到最后一层排序的数组中.

另一个例子:

var testArrayTwo = [1,[2,3,4],5,[6,[7,8],9,10],11,12];
testArrayTwo.multiIndexOf(6); //return [3, 0]
Run Code Online (Sandbox Code Playgroud)

可选:如果嵌套数组有两个相同的值,那么获取第一个值的位置,还是存储两个位置的数组?如果value不在嵌套数组中,return …

javascript arrays multidimensional-array

5
推荐指数
1
解决办法
1373
查看次数

如何在Javascript中获取嵌套数组长度?

我有一个嵌套数组的例子:

var testArray = [1,2,[3,4,[5,6],7],8,9,[10,11],12];
Run Code Online (Sandbox Code Playgroud)

这是我获取嵌套数组长度的函数:

Array.prototype.getLength = function() {
  var sum = 0;
  function getMultiLength(array) {
    for (count = 0; count < array.length; count ++) {
      sum ++;
      if (!array[count].length) {
        getMultiLength(array[count]);
      }
    }
  }
  getMultiLength(this.valueOf());
  return sum;
};
Run Code Online (Sandbox Code Playgroud)

我对结果的期望是12,但我得到的是无限循环:

testArray.getLength(); //infinite loop
Run Code Online (Sandbox Code Playgroud)

任何人都知道为什么以及如何获得嵌套数组长度?

javascript arrays function infinite-loop multidimensional-array

1
推荐指数
1
解决办法
5415
查看次数

如何在javascript中使用本地数组进行推送,取消移位,弹出和转换?

我有一个函数来描述我的问题:

function testingFunc(value) {
  var temp = value;
  console.log("temp before:    " + JSON.stringify(temp));
  console.log("arrayTest before:    " + JSON.stringify(arrayTest));
  temp.unshift(123);
  console.log("temp after unshift:    " + JSON.stringify(temp));
  console.log("arrayTest after unshift:    " + JSON.stringify(arrayTest));
  temp.push(456);
  console.log("temp after push:    " + JSON.stringify(temp));
  console.log("arrayTest after push:    " + JSON.stringify(arrayTest));
  temp.shift();
  console.log("temp after shift:    " + JSON.stringify(temp));
  console.log("arrayTest after shift:    " + JSON.stringify(arrayTest));
  temp.pop();
  console.log("temp after pop:    " + JSON.stringify(temp));
  console.log("arrayTest after pop:    " + JSON.stringify(arrayTest));
  return temp;
}

var arrayTest = [1,2,3,4,5];
var arrayTestTwo;

arrayTestTwo …
Run Code Online (Sandbox Code Playgroud)

javascript arrays function

1
推荐指数
1
解决办法
437
查看次数