Given a start and end point, how can I constrain the end point so the resulting line segment is horizontal, vertical, or 45 degrees?

Posted by GloryFish on Game Development See other posts from Game Development or by GloryFish
Published on 2011-02-08T01:32:45Z Indexed on 2011/02/08 7:33 UTC
Read the original article Hit count: 272

Filed under:

I have a grid of letters. The player clicks on a letter and drags out a selection. Using Bresenham's Algorithm I can create a line of highlighted letters representing the player's selection.

However, what I really want is to have the line segment be constrained to 45 degree angles (as is common for crossword-style games).

So, given a start point and an end point, how can I find the line that passes through the start point and is closest to the end point?

Bonus: To make things super sweet I'd like to get a list of points in the grid that the line passes through, and for super MEGA bonus points, I'd like to get them in order of selection (i.e. from start point to end point).

© Game Development or respective owner

Related posts about algorithm