Skip to content

Commit e483a50

Browse files
committed
Fix the anti-aliasing pixel might be out of range bug in default rasterizer
1 parent 76879c7 commit e483a50

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/drawing/rasterizer/line.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,29 @@ pub fn draw_line<DB: DrawingBackend, S: BackendStyle>(
9191
}
9292
};
9393

94-
let mut y = f64::from(from.1);
94+
let y_step_limit =
95+
(f64::from(to.1.min(size_limit.1 as i32 - 1).max(0) - from.1) / grad).floor() as i32;
9596

96-
let y_step_limit = (f64::from(to.1.min(size_limit.1 as i32 - 1) - from.1) / grad).ceil() as i32;
97+
let batch_start = (f64::from(from.1.min(size_limit.1 as i32 - 2).max(0) - from.1) / grad)
98+
.abs()
99+
.ceil() as i32
100+
+ from.0;
97101

98-
for x in from.0..=to.0.min(size_limit.0 as i32 - 2).min(from.0 + y_step_limit) {
102+
let batch_limit =
103+
to.0.min(size_limit.0 as i32 - 2)
104+
.min(from.0 + y_step_limit - 1);
105+
106+
let mut y = f64::from(from.1) + f64::from(batch_start - from.0) * grad;
107+
108+
for x in batch_start..=batch_limit {
99109
check_result!(put_pixel((x, y as i32), 1.0 + y.floor() - y));
100110
check_result!(put_pixel((x, y as i32 + 1), y - y.floor()));
101111

102112
y += grad;
103113
}
104114

105-
if to.0 >= (size_limit.0 as i32) - 1 && y < f64::from(to.1) {
106-
let x = size_limit.0 as i32 - 1;
115+
if to.0 >= batch_limit + 1 && y < f64::from(to.1) {
116+
let x = batch_limit as i32 + 1;
107117
if 1.0 + y.floor() - y > 1e-5 {
108118
check_result!(put_pixel((x, y as i32), 1.0 + y.floor() - y));
109119
}

0 commit comments

Comments
 (0)