简体   繁体   English

Java,在两点之间生成点的直接路径

[英]Java, Generate a direct path of points between two points

I currently have a bad method for doing this, it just translates the start point by 1/-1 depending on if the x/y coordinate is over or under the current coordinates and adds it to an ArrayList until the start point .equals(end), this is a bad solution because it creates a really bad path that looks like the black path below. 我目前有一个糟糕的方法,它只会根据x / y坐标是在当前坐标之上还是之下,将起点转换1 / -1,并将其添加到ArrayList直到起点.equals(end ),这是一个糟糕的解决方案,因为它会创建一条看起来像下面黑色路径的非常糟糕的路径。

https://dl.dropbox.com/u/64681860/paths.png

I'm trying to generate a direct path of points between two points (The same kind of line as Graphics.drawLine makes). 我试图在两点之间生成点的直接路径(与Graphics.drawLine产生的线相同)。

I'm guessing I need to use the Math class to get the angle, but I'm not very familiar with the Math API. 我想我需要使用Math类来获取角度,但是我对Math API不太熟悉。

One somewhat naive way, is to work out the ratio of the slope, rather than the angle. 一种比较幼稚的方法是计算坡度的比率,而不是角度。

Something like: 就像是:

float ratio = (y2 - y1) / (x2 - x1);

Then: 然后:

width = x2 - x1;
for(int i = 0; i < width; i++) {
    float x = x1 + i;
    float y = y1 + (ratio * i);
    points.add(new Point(x,y));
}

If float isn't what you need, you'll need to do some conversion. 如果不需要float,则需要进行一些转换。

You'll also need to handle the special case of x1 == x2, or you'll get divide-by-zero errors. 您还需要处理x1 == x2的特殊情况,否则您将得到除零错误。

Using this method, the steeper the line, the fewer points you will generate. 使用此方法,线越陡,将生成的点越少。 If you want the points evenly spaced no matter what the angle, you'll need to break out sin/cos/tan. 如果要使点均匀分布,无论角度如何,都需要分解sin / cos / tan。

Write unit tests for lines in at least the main 8 compass directions, to iron out any glitches. 编写至少在8个主要罗盘方向上的线的单元测试,以消除任何毛刺。

This is actually more of a math problems than a programming problem. 实际上,这实际上是数学问题,而不是编程问题。 You need to find the vector that goes from start to end and then scale it and add it to start for the number of points that you want. 你需要找到从那张矢量startend ,然后缩放,并把它添加到start为想要的点数。

In pseudo code: 在伪代码中:

f(start,end,nPoints) -> (path)
   delta = (end-start) / nPoints //Find the best difference vector.
   current = start //Set the current point to the start.
   i = 0
   while(|current-end| < epsilon)
      current += delta
      path[i] = current
      i = i + 1

I'm assuming that the points are floating point (due to the division). 我假设这些点是浮点数(由于除法)。 epslion should be chosen to be a small value (depends on your problem) to check equality (never use != for floating points!). epslion应该选择为一个较小的值(取决于您的问题)以检查是否相等(切勿使用!=来表示浮点!)。

This is a very simple concept, but you've not been too clear on what exactly you want. 这是一个非常简单的概念,但是您对确切想要什么还不太清楚。 By definition, there are an infinite number of points between any two points (and between two of those points, exist an infinite number of points. And so on.) 根据定义,有点中的任意两点之间的无限数量(和两个那些点之间,存在点的无限数量。等)。

The simplest solution would be to calculate the formula for the line between point A and point B, and then decide how far apart you want your points to be, and calculate those points using your line formula. 最简单的解决方案是计算A点与B点之间的直线的公式,然后确定您想要的点相距多远,然后使用直线公式计算这些点。

Elementary geometry tells us that the slope between two points in the change in y over the change in x. 基本几何告诉我们y的变化的两个点之间的斜率超过x的变化。 So m = (y1 - y2)/(x1 - x2) , where all of these values are doubles, and x1 and y1 belong to the same point. 因此m = (y1 - y2)/(x1 - x2) ,其中所有这些值都是双精度值,并且x1y1属于同一点。

Then the formula for the line between two points is y - y1 = m(x - x1) . 那么两点之间的直线的公式是y - y1 = m(x - x1) Then all you'd have to do is start plugging in values for, say, x , taking the resultant y , and drawing it. 然后,您要做的就是开始插入x值,并取值y并绘制出来。

This is, of course, the idea behind the whole thing. 当然,这就是整件事的想法 You'd be much better off using vectors. 使用向量会更好。

Check out this previously answered question to calculate the angle. 查看先前回答的问题以计算角度。 And then use the distance formula to find the distance. 然后使用距离公式来查找距离。

double dist = Math.sqr((x2 - x1)^2 + (y2 - y1)^2);  //obviously wont compile but you get the idea.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM