
Line drawing on a grid - mbostock
http://www.redblobgames.com/grids/line-drawing.html
======
toolslive
I remember enjoying Abrash's write up in the mid 90s.
[http://www.phatcode.net/res/224/files/html/ch35/35-01.html](http://www.phatcode.net/res/224/files/html/ch35/35-01.html)
He starts with the algorithm, then a C implementation, then optimization in
ASM, only to throw it all away in the subsequent chapter because of a better
algorithm. He goes through this cycle several times. Splendid educational
effort on multiple levels.

------
forrestthewoods
Not sure why they author immediately dismisses bresenham. It's fast, quality,
and well known. I feel like if the post continued for another few logical
steps of optimization that's exactly where he'd end up!

~~~
chipsy
Bresenham lines are not "quality." For one, they're obviously jaggy. As well,
most implementations of the algorithm will give you different results if you
invert the start and end points. This matters a great deal for the common use-
case in games of casting rays to determine sight lines. It is quite annoying
to have a tactical combat game where a foe can shoot from behind a wall and
you can't shoot back from the exact same position. Amit notes lack of symmetry
in the "2.1 Orthogonal steps" implementation, which leads him to the
"Supercover" implementation.

~~~
abcd_f
Pray tell which tactical combat game determines the line of sight using a
_discrete grid_.

~~~
mattdesl
An awful lot of top down 2D tile based games.

------
lovelearning
What a stroke of luck for me! Just today I was studying line drawing
techniques on a grid for a prototype related to offline mapping using
polygons. It's yet another reminder how topics and links that don't seem
relevant to my interests at first (games in this case) often turn out to be
relevant to other areas of my interest.

------
jokoon
don't forget to read his explanation of A*, it's really great, and pretty
easy. explained in python so it was even better.

~~~
dirkk0
You are right. Thanks for bringing this to my attention.
[http://www.redblobgames.com/pathfinding/a-star/introduction....](http://www.redblobgames.com/pathfinding/a-star/introduction.html)

Every now and then I visit Amit's pages and find something interesting that he
explains in an awesome way.

------
poseid
great to see the examples. I am exploring a small JavaScript module approach
to SVG drawing to: [https://github.com/mulderp/pinboard-
grid](https://github.com/mulderp/pinboard-grid)

~~~
amitp
I used to draw the lines that way, but I've switched to drawing the squares
instead. It tends to look cleaner, the code is simpler, and it also lets me
color the squares individually. If I really need to draw the lines, I can set
a background color. It's a cheap trick :-)

~~~
poseid
indeed good advise! I changed the implementation
[https://github.com/mulderp/pinboard-
grid/commit/2a9d2c256ce0...](https://github.com/mulderp/pinboard-
grid/commit/2a9d2c256ce0ba18cf90d7eae28acb8e43c7bb48) \- just saw you use
transform instead of adjusting x/y

------
guelo
Too bad there was nothing about using color for anti aliasing.

------
andrewchoi
Great demo. I really appreciate the interactivity.

Is there something similar for antialiasing?

------
cafebeen
Anyone seen a similar run-down for 3D grids?

~~~
amitp
One nice thing about linear interpolation is that the same algorithm works for
2D grids, 3D grids, and hex grids. For 3D, the diagonal_distance, point_lerp,
and point_round functions can look at at z in addition to x and y. For hex
grids, you need hex grid distances and hex grid rounding
([http://www.redblobgames.com/grids/hexagons/#line-
drawing](http://www.redblobgames.com/grids/hexagons/#line-drawing)).

