> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/x/xiaolin_wus_line_algorithm.md).

# Xiaolin Wu's line algorithm

```ruby
func plot(x, y, c) {
    c && printf("plot %d %d %.1f\n", x, y, c)
}
 
func fpart(x) {
    x - int(x)
}
 
func rfpart(x) {
    1 - fpart(x)
}
 
func drawLine(x0, y0, x1, y1) {
 
    var p = plot
    if (abs(y1 - y0) > abs(x1 - x0)) {
        p = {|arg| plot(arg[1, 0, 2]) }
        (x0, y0, x1, y1) = (y0, x0, y1, x1)
    }
 
    if (x0 > x1) {
        (x0, x1, y0, y1) = (x1, x0, y1, y0)
    }
 
    var dx = (x1 - x0)
    var dy = (y1 - y0)
    var gradient = (dy / dx)
 
    var xends = []
    var intery
 
    # handle the endpoints
    for x,y in [[x0, y0], [x1, y1]] {
        var xend = int(x + 0.5)
        var yend = (y + gradient*(xend-x))
        var xgap = rfpart(x + 0.5)
 
        var x_pixel = xend
        var y_pixel = yend.int
        xends << x_pixel
 
        p.call(x_pixel, y_pixel  , rfpart(yend) * xgap)
        p.call(x_pixel, y_pixel+1,  fpart(yend) * xgap)
        defined(intery) && next
 
        # first y-intersection for the main loop
        intery = (yend + gradient)
    }
 
    # main loop
    for x in (xends[0]+1 .. xends[1]-1) {
        p.call(x, intery.int,  rfpart(intery))
        p.call(x, intery.int+1, fpart(intery))
        intery += gradient
    }
}
 
drawLine(0, 1, 10, 2)
```

#### Output:

```
plot 0 1 0.5
plot 10 2 0.5
plot 1 1 0.9
plot 1 2 0.1
plot 2 1 0.8
plot 2 2 0.2
plot 3 1 0.7
plot 3 2 0.3
plot 4 1 0.6
plot 4 2 0.4
plot 5 1 0.5
plot 5 2 0.5
plot 6 1 0.4
plot 6 2 0.6
plot 7 1 0.3
plot 7 2 0.7
plot 8 1 0.2
plot 8 2 0.8
plot 9 1 0.1
plot 9 2 0.9
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://trizen.gitbook.io/sidef-lang/programming_tasks/x/xiaolin_wus_line_algorithm.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
