获取两个位置之间的路径

huangapple go评论112阅读模式
英文:

Getting path between two positions

问题

给定一个位置:

class Position {
    private int x, y;

    public Position(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

我想计算两个这样位置之间的差异,并且希望它返回一个位置列表,以便到达最终位置。

例如:

Position oldPosition = new Position(10, 10);
Position newPosition = new Position(12, 10);

应该返回一个列表:

[[10,10], [11,10], [12,10]]

我的当前代码:

Position oldPosition = new Position(10, 10);
Position newPosition = new Position(12, 12);

List<Position> fromOldToNewPositions = new ArrayList<>();
int differenceX = newPosition.getX() - oldPosition.getX();
int differenceY = newPosition.getY() - oldPosition.getY();
boolean xNegative = differenceX < 0;
boolean yNegative = differenceY < 0;

for (int x = oldPosition.getX(); xNegative && x >= newPosition.getX() || !xNegative && x <= newPosition.getX(); x = xNegative ? x - 1 : x + 1) {
    for (int y = oldPosition.getY(); yNegative && y >= newPosition.getY() || !yNegative && y <= newPosition.getY(); y = yNegative ? y - 1 : y + 1) {
        fromOldToNewPositions.add(new Position(x, y));
    }
}

在终点位置为12,12的情况下,它返回一个列表:

[[10,10], [10,11], [10,12], [11,10], [11,11], [11,12], [12,10], [12,11], [12,12]]

我希望结果是:

[[10,10], [11,11], [12,12]]

如何实现这样的解决方案?

英文:

Given a Position:

class Position {
    private int x, y;

    public Position(int x, int y) {
        this.x = x;
        this.y = y;
}

I would like to calculate the difference between two such positions and then have it return a list of positions that would get it to the end.

For example:

Position oldPosition = new Position(10, 10);
Position newPosition = new Position(12, 10);

Should return a list with:

[[10,10], [11,10], [12,10]]

My current code:

Position oldPosition = new Position(10, 10);
Position newPosition = new Position(12, 12);

List&lt;Position&gt; fromOldToNewPositions = new ArrayList&lt;&gt;();
int differenceX = newPosition.getX() - oldPosition.getX();
int differenceY = newPosition.getY() - oldPosition.getY();
boolean xNegative = differenceX &lt; 0;
boolean yNegative = differenceY &lt; 0;


for (int x = oldPosition.getX(); xNegative &amp;&amp; x &gt;= newPosition.getX() || !xNegative &amp;&amp; x &lt;= newPosition.getX(); x = xNegative ? x - 1 : x + 1) {
    for (int y = oldPosition.getY(); yNegative &amp;&amp; y &gt;= newPosition.getY() || !yNegative &amp;&amp; y &lt;= newPosition.getY(); y = yNegative ? y - 1 : y + 1) {
        fromOldToNewPositions.add(new Position(x, y));
    }
}

Does this well, but in scenarios where the end position is 12,12 it returns a list:

[[10,10], [10,11], [10,12], [11,10], [11,11], [11,12], [12,10], [12,11], [12,12]]

where I would like the result to be:

[[10,10], [11,11], [12,12]]

How would I go about achieving such a solution?

答案1

得分: 1

如果只允许左、右、上和下的移动,那么您需要首先在X方向上进行所有步骤,然后在Y方向上进行所有步骤。这意味着两个连续的for循环,而不是嵌套的。

如果允许对角线步骤(似乎是这样),则需要计算一般需要采取多少步骤:max(diffX, diffY)。然后采取同样数量的步骤,其中一部分是对角线步骤:

Position oldPosition = new Position(10, 10);
Position newPosition = new Position(14, 12);

List<Position> fromOldToNewPositions = new ArrayList<>();
int differenceX = newPosition.getX() - oldPosition.getX();
int differenceY = newPosition.getY() - oldPosition.getY();

int steps = Math.max(differenceX, differenceY);
for (int step = 0; step <= steps; step++) {
    double part = step / (double) steps;
    fromOldToNewPositions.add(new Position(
            (int) (oldPosition.getX() + differenceX * part),
            (int) (oldPosition.getY() + differenceY * part)));
}
[[10 | 10], [11 | 10], [12 | 11], [13 | 11], [14 | 12]]
英文:

If you are only allowed left, right, up and down then you need to take all steps in X direction first and then all steps in Y direction. That means two for loops after each other, not nested.

If you are allowed diagonal steps (as it seems) you need to calculate how many steps you need to take in general: max(diffX, diffY). And then take as many steps, some of them being diagonal steps:

Position oldPosition = new Position(10, 10);
Position newPosition = new Position(14, 12);

List&lt;Position&gt; fromOldToNewPositions = new ArrayList&lt;&gt;();
int differenceX = newPosition.getX() - oldPosition.getX();
int differenceY = newPosition.getY() - oldPosition.getY();

int steps = Math.max(differenceX, differenceY);
for (int step = 0; step &lt;= steps; step++) {
    double part = step / (double) steps;
    fromOldToNewPositions.add(new Position(
            (int) (oldPosition.getX() + differenceX * part),
            (int) (oldPosition.getY() + differenceY * part)));
}
[[10 | 10], [11 | 10], [12 | 11], [13 | 11], [14 | 12]]

答案2

得分: 0

为了获取路径,请尝试以下方式:

Position oldPosition = new Position(15, 10);
Position newPosition = new Position(12, 11);

List<Position> fromOldToNewPositions = new ArrayList<>();
if (oldPosition.x > newPosition.x) {
    Position c = oldPosition;
    oldPosition = newPosition;
    newPosition = c;
}
int x = oldPosition.x;
int y = oldPosition.y;
fromOldToNewPositions.add(oldPosition);
for (; x <= newPosition.x; x++) {
    // 这意味着你到达目标行,只需移动列
    if (x == newPosition.x) {
        while (y != newPosition.y) {
            if (y < newPosition.y) {
                fromOldToNewPositions.add(new Position(newPosition.x, y++));
            } else {
                fromOldToNewPositions.add(new Position(newPosition.x, y--));
            }
        }
    } else {
        if (y < newPosition.y) {
            fromOldToNewPositions.add(new Position(x + 1, ++y));
        } else if (y > newPosition.y) {
            fromOldToNewPositions.add(new Position(x + 1, --y));
        } else {
            fromOldToNewPositions.add(new Position(x + 1, y));
        }
    }
}
fromOldToNewPositions.forEach(e -> System.out.println(e.x + " " + e.y));
英文:

To get path try like this:

Position oldPosition = new Position(15, 10);
Position newPosition = new Position(12, 11);

List&lt;Position&gt; fromOldToNewPositions = new ArrayList&lt;&gt;();
if (oldPosition.x &gt; newPosition.x) {
    Position c = oldPosition;
    oldPosition = newPosition;
    newPosition = c;
}
int x = oldPosition.x;
int y = oldPosition.y;
fromOldToNewPositions.add(oldPosition);
for (; x &lt;= newPosition.x; x++) {
    // that means you reach destination row. just go column only
    if (x == newPosition.x) {
        while (y != newPosition.y) {
            if (y &lt; newPosition.y) {
                fromOldToNewPositions.add(new Position(newPosition.x, y++));
            } else {
                fromOldToNewPositions.add(new Position(newPosition.x, y--));
            }
        }
    } else {
        if (y &lt; newPosition.y) {
            fromOldToNewPositions.add(new Position(x + 1, ++y));
        } else if (y &gt; newPosition.y) {
            fromOldToNewPositions.add(new Position(x + 1, --y));
        } else {
            fromOldToNewPositions.add(new Position(x + 1, y));
        }
    }
}
fromOldToNewPositions.forEach(e -&gt; System.out.println(e.x + &quot; &quot; + e.y));

答案3

得分: 0

int xChange = newPosition.getX() - oldPosition.getX();
int yChange = newPosition.getY() - oldPosition.getY();
int xPositive = 1; // 每次 x 步长
int yPositive = 1; // 每次 y 步长
if (xChange < 0) {
    xPositive = -1;
}
if (yChange < 0) {
    yPositive = -1;
}
int x = oldPosition.getX();
int y = oldPosition.getY();

fromOldToNewPositions.add(oldPosition);
while (xChange != 0 || yChange != 0) {
    // 只有在当前位置不等于目标位置时才改变 x
    if (xChange != 0) {
        x += xPositive;
        xChange -= xPositive;
    }
    if (yChange != 0) {
        y += yPositive;
        yChange -= yPositive;
    }
    fromOldToNewPositions.add(new Position(x, y));
}
英文:
int xChange = newPosition.getX() - oldPosition.getX();
int yChange = newPosition.getY() - oldPosition.getY();
int xPositive = 1; // each step of x
int yPositive = 1; // each step of y
if (xChange &lt; 0) {
    xPositive = -1;
}
if (yChange &lt; 0) {
    yPositive = -1;
}
int x = oldPosition.getX();
int y = oldPosition.getY();

fromOldToNewPositions.add(oldPosition);
while (xChange != 0 || yChange != 0) {
    // x only change if current position is not the same as destination
    if (xChange != 0) {
        x += xPositive;
        xChange -= xPositive;
    }
    if (yChange != 0) {
        y += yPositive;
        yChange -= yPositive;
    }
    fromOldToNewPositions.add(new Position(x, y));
}

huangapple
  • 本文由 发表于 2020年4月6日 21:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61060749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定