Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect calculation in get distance for diagonals #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mikeparker
Copy link

@mikeparker mikeparker commented Jan 7, 2020

I am implementing the ability to return incomplete paths and noticed this calculation was off.

For example if a node is 1x,1y away from the target this function incorrectly returns a distance of 2.4 instead of 1.4.

Another example, if a node is 4x,2y away, this should move diagonally for 2y which will also result in 2x movement, then the remaining 2x, so it should be 2*DIAGONAL_COST + 2*STRAIGHT_COST rather than 2*DIAGONAL_COST + 4*STRAIGHT_COST

@mikeparker mikeparker changed the title WIP: Fix incorrect calculation in get distance for diagonals Fix incorrect calculation in get distance for diagonals Nov 10, 2020
src/easystar.js Outdated
} else {
return DIAGONAL_COST * dy + dx;
return DIAGONAL_COST * dy + (dx=dy);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeparker sorry i know its old, i implemented this change myself also but wanted to ask... why was it dx=dy

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's pretty old, that looks like a typo to me, it's probably supposed to be (dx-dy)

If I was better with javascript this is exactly the sort of thing a unit test should check for.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that makes sense thinking about it. If dx < dy then you travel all the X distance via diagonals then the remainer dy via vertical moves.

If its the other way around then you travel all the Y distance via diagonals then the remainder dx via horizontal moves.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the typo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants