Jazz
Jazz

Reputation: 101

Ultra symmetrical line algorithm?

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

Upvotes: 10

Views: 1215

Answers (4)

Durandal
Durandal

Reputation: 20059

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

Upvotes: 3

Eric Bainville
Eric Bainville

Reputation: 9886

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.

Upvotes: 1

Peter Alexander
Peter Alexander

Reputation: 54270

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.

Upvotes: 0

Mike Q
Mike Q

Reputation: 23229

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.

Upvotes: 4

Related Questions