# Generating Straight line path

## Recommended Posts

not too good but w/ee

public static Position[] generateStraightPath(Position start, Position end) {
ArrayList<Position> path = new ArrayList<Position>();
int currentX = start.getX();
int currentY = start.getY();
int endX = end.getX();
int endY = end.getY();
int dX = (currentX < endX) ? 1 : -1;
int dY = (currentY < endY) ? 1 : -1;
while (currentX != endX || currentY != endY) {
if (currentX != endX) {
currentX += dX;
}
if (currentY != endY) {
currentY += dY;
}
}
return path.toArray(new Position[0]);
}

what does it do?

for example using

generateStraightPath(new Position(5, 10, 0), new Position(10, 20, 0))

it will spit this path out  (E the element in the array)

Visual demo of how it would look (IM SO GOOD @ PAINT!!!):

as you see it goes diagonal until it reaches the same x then goes straight up

##### Share on other sites

1. Depending on the distance between the points, you could have a mass cache of position instances that just aren't necessary. You should set dX and dY to an user-inputted "jump" offset. This would require additional coding to ensure the leaps don't overstep the end position, etc.
2. The or operator in the while loop should be changed to an and operator, else the loop could prematurely stop before the entire path has been generated just because one axis has reached its max size.
3. I think it would be more effective to work with a 2d array object instead of the ArrayList object. You can already compute the distance between those two points, so theoretically you can compute a fixed size amount--even with user-inputted offset parameter. This OCD implementation would prove more efficient (even if it's minuscule) than using an ArrayList. Then again, you can provide a capping value for the ArrayList instance.
4. Why are you declaring int variables to represent the start and end X and Y positions? They're already get-able from the Position class. Just wasteful payload that doesn't need to exist.
5. ..Just because: You don't need opening and closing brackets on selectors if they only execute one conditional operation. You could shave 2 lines off your code.

I may update this post to ensure I haven't fooked up anywhere.

Edited by liverare
##### Share on other sites

1. Depending on the distance between the points, you could have a mass cache of position instances that just aren't necessary. You should set dX and dY to an user-inputted "jump" offset. This would require additional coding to ensure the leaps don't overstep the end position, etc.
2. The or operator in the while loop should be changed to an and operator, else the loop could prematurely stop before the entire path has been generated just because one axis has reached its max size.
3. I think it would be more effective to work with a 2d array object instead of the ArrayList object. You can already compute the distance between those two points, so theoretically you can compute a fixed size amount--even with user-inputted offset parameter. This OCD implementation would prove more efficient (even if it's minuscule) than using an ArrayList. Then again, you can provide a capping value for the ArrayList instance.
4. Why are you declaring int variables to represent the start and end X and Y positions? They're already get-able from the Position class. Just wasteful payload that doesn't need to exist.
5. ..Just because: You don't need opening and closing brackets on selectors if they only execute one conditional operation. You could shave 2 lines off your code.

I may update this post to ensure I haven't fooked up anywhere.

5. that's just my OCD

3. a 2d. array object would be a nice idea thanks!

2. no it wouldn't as the other axis would not be at it's max size yet

thanks for the feedback really informative and sounds like you know what you're doing