Reputation: 13597
I need to let the user draw a path on a grid but without a precision - the algorithm should adjust the path.
Example images where red line indicates the continuous path user will draw and blue dots are the final path
or
I am thinking something about intersections(red dots) so I would add intersection to the list from which to pathfind until the current input and maybe some weighted graph approach but have no final idea. I would appreciate any advice on this.!
Upvotes: 1
Views: 248
Reputation: 51835
Just to be sure I see it the right way:
curved line means be as close as possible to it ...
[notes]
Upvotes: 1
Reputation: 4100
At any point you can either change X or change Y, not both, so you can make changeX a boolean, with changeY its NOT. Suppose changeX is true. Then you just take the X coordinate of the user input. Vice versa for ~changeX and Y coordinate of user input.
The variable changeX can be altered only at the intersections. One way to decide whether to update would be to calculate which of the next points would be closest to the pointer. You just need to compare the squares of the distances so that saves you from an expensive square root calculation.
You could probably calculate every next point on that basis, but it's probably overkill for the situation when the grid is square.
Upvotes: 0