English 中文(简体)
在二维环境中以百分比为增量将对象从矢量A移动到B
原标题:Moving object from vector A to B in 2d environment with in increments of percents

我知道向量A和向量B的坐标。如何计算这两个向量之间的第一点?第一个向量X是向量A和B之间距离的1%。所以首先我会把向量A中的物体移到离向量B近1%的地方。所以我需要计算向量X,它是物体的新向量,直到它到达向量B。

最佳回答

您想要lerping。作为参考,基本公式为:

x=A+t*(B-A)

其中t介于0和1之间。(任何超出该范围的东西都会使其成为额外的极化。)

t=0时检查x=A,当t=1时检查x=B

请注意,我的回答没有提到矢量或2D。

问题回答

车削aib的答案转换为代码:

function lerp(a, b, t) {
    var len = a.length;
    if(b.length != len) return;

    var x = [];
    for(var i = 0; i < len; i++)
        x.push(a[i] + t * (b[i] - a[i]));
    return x;
}

var A = [1,2,3];
var B = [2,5,6];

var X = lerp(A, B, 0.01);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签