✂️ clipline 📏
clipline
is a Rust crate for efficient scan conversion of a line segment with clipping to a rectangular window. It is an implementation of the 1995 paper by YP Kuzmin.
The key advantage of this algorithm over vanilla Bresenham is that it eliminates the need for bounds checking on every pixel, which speeds up line drawing. Furthermore, the clipping is done via integer arithmetic, producing accurate clipped endpoints. This sets it apart from floating-point clipping algorithms like Cohen-Sutherland, which may distort the line due to rounding errors.
Installation
To use clipline
, add it to your Cargo.toml
file:
[dependencies]
clipline = "0.1.0"
Usage
use clipline::clipline;
fn draw_pixel(x: isize, y: isize) {
// Your custom pixel drawing logic
// No bounds checks necessary here
}
let line = ((0, 0), (10, 10));
let clip_rect = ((2, 2), (8, 8));
let (start, end) = clipline(line, clip_rect, draw_pixel)
.expect("line intersects the clipping rectangle");
// Perform any additional logic with the clipped line segment.
// `start` and `end` represent the visible portion of the line.
In this example, clipline
takes a line defined by two endpoints, line
, and corners of a clipping rectangle, clip_rect
. The closure draw_pixel
handles each pixel within the clipped region, and the visible portion of the line is represented by the start
and end
variables.