views:

120

answers:

4

I ran into special case where I need to produce ultra symmetrical line or ray in 2D grid in order from (x0, y0) through (x1, y1) like this:

void drawSymmetricalLine(int x0, int y0, int x1, int y1)
{
    // loop and handle each (x, y)...
}

The actual problem lies in points where popular line drawing algorithms does NOT draw both coordinates (the other marked as x below) since it seems as thicken, which is desired in my case. Also performance is not important but simplicity.

Here is what I mean as ultra symmetrical lines:

ox   ooo
 oo     ooo


o    o
 o    o
  o   o
       o
+4  A: 

You can probably use Bresenham's line algorithm and modify it slightly so when the step change to move the draw position from one row to another you paint both the before and after pixels on the y-axis for the current x-axis.

Mike Q
+1  A: 

If simplicity is preferred over performance, then write a recursive algorithm. At each step compute DX=X1-X0 and DY=Y1-Y0.

Stop recursion when DX=0 or DY=0 (in which case your line is vertical or horizontal).

Otherwise, compute the two "middle" end-points, according to the parity of DX and DY, and draw the two half lines recursively.

Eric Bainville
A: 

Use Bresenham's line algorithm except when you plot a point at (x0+dx, y0+dy), also plot a point at (x1-dx, y1-dy). That way you ensure that it is symmetrical from both sides.

It's a little inefficient, but you said that doesn't matter.

Peter Alexander
+1  A: 

Render the line twice, once from p0 to p1 and again from p1 to p0.

Durandal