Drawing a line code

rikki cattermole rikki at cattermole.co.nz
Sun Nov 6 17:15:03 UTC 2022


On 07/11/2022 5:48 AM, Joel wrote:
> The algorithm is too hard for me to work out and dg2d doesn't help 
> either. I want my code fixed up so that works from any two points.

Its not as complex as that page initially looks.

```
plotLine(x0, y0, x1, y1)
     dx = abs(x1 - x0)
     sx = x0 < x1 ? 1 : -1
     dy = -abs(y1 - y0)
     sy = y0 < y1 ? 1 : -1
     error = dx + dy

     while true
         plot(x0, y0)
         if x0 == x1 && y0 == y1 break
         e2 = 2 * error
         if e2 >= dy
             if x0 == x1 break
             error = error + dy
             x0 = x0 + sx
         end if
         if e2 <= dx
             if y0 == y1 break
             error = error + dx
             y0 = y0 + sy
         end if
     end while
```

That is the pseudo code you want.

Its just a matter of typing the variables to something like int.

I can recommend:

https://www.amazon.com/Computer-Graphics-Principles-Practice-3rd/dp/0321399528

and

https://www.amazon.com/Computer-Graphics-C-Version-2nd/dp/0135309247

(The updated version should be fine too)

If you wish to understand it.


More information about the Digitalmars-d-learn mailing list