移动一个点除了在JavaScript画布线路画布、线路、JavaScript

由网友(心口朱砂是你゛)分享简介:我有我的九年级的科学公正的项目的问题,我需要一些帮助。对于我的项目,我想打一个交通仿真,但有一点是给我麻烦...... I have a question for my ninth grade science fair project and I need some help. For my project I wa...

我有我的九年级的科学公正的项目的问题,我需要一些帮助。对于我的项目,我想打一个交通仿真,但有一点是给我麻烦......

I have a question for my ninth grade science fair project and I need some help. For my project I want to make a traffic simulator, but one thing is giving me trouble...

让我们说,我有一个行的坐标(25,35 45,65,30,85 - 这将是两部分的线)。我需要移动一个点(车)沿着这条线以恒定的距离每帧。我怎样才能做到这一点?

Let's say that I have the coordinates of a line (25,35 45,65, 30,85 - It would be a two part line). I need to move a point (car) along that line at a constant distance every frame. How can I do this?

谢谢,C.Ruhl

推荐答案

考虑行(25,35 45,65)。从开始到结束时的矢量为(20,30)。要移动一个点(x,y)的那个方向,我们可以只添加矢量:

Consider the line (25,35 45,65). The vector from the beginning to the end is (20, 30). To move a point (x,y) in that direction, we could just add that vector:

V =(20,30) (X,Y)=>(X + 20,Y + 30)。

V = (20, 30) (x,y) => (x+20, y+30).

如果我们开始在该行的开始,我们将到达终点。 但是,这是太大的一步。我们想要的东西,但更小的在同一方向的,所以我们乘以向量,比方说,0.1

If we start at the beginning of the line, we'll arrive at the end. But that's too big a step. We want something smaller but in the same direction, so we multiply the vector by, say, 0.1:

V =(2,3) (X,Y)=>(X + 2,Y + 3)=>(x + 4开始,Y + 6)=> ...

V = (2, 3) (x,y) => (x+2, y+3) => (x+4, y+6) => ...

这是方便的标准化,那就是让其长度为1,这是我们做的由它的长度划分:

It's convenient to normalize, that is to make its length 1, which we do by dividing by its length:

V => V / | V | =(2,3)/的sqrt(2 2 + 3 2 )=(7.21,10.82)

V => V/|V| = (2,3)/sqrt(22 + 32) = (7.21, 10.82)

然后,你可以乘上任何一步你想要的大小。

Then you can just multiply that by whatever step size you want.

阅读全文

相关推荐

最新文章