Skip to content

Commit 5123e95

Browse files
committed
implement switching off anti-aliasing
1 parent 0aeb925 commit 5123e95

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

plotters-backend/src/rasterizer/circle.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,27 @@ fn draw_sweep_line<B: DrawingBackend, S: BackendStyle>(
100100
let vs = s.ceil() - s;
101101
let ve = e - e.floor();
102102

103+
let (s1, s2) = if b.use_anti_aliasing() {
104+
(style.color().mix(vs), style.color().mix(ve))
105+
} else {
106+
(style.color(), style.color())
107+
};
103108
if dx == 0 {
104109
check_result!(b.draw_line(
105110
(p0 + x0, s.ceil() as i32 + y0),
106111
(p0 + x0, e.floor() as i32 + y0),
107112
&style.color()
108113
));
109-
check_result!(b.draw_pixel((p0 + x0, s.ceil() as i32 + y0 - 1), style.color().mix(vs)));
110-
check_result!(b.draw_pixel((p0 + x0, e.floor() as i32 + y0 + 1), style.color().mix(ve)));
114+
check_result!(b.draw_pixel((p0 + x0, s.ceil() as i32 + y0 - 1), s1));
115+
check_result!(b.draw_pixel((p0 + x0, e.floor() as i32 + y0 + 1), s2));
111116
} else {
112117
check_result!(b.draw_line(
113118
(s.ceil() as i32 + x0, p0 + y0),
114119
(e.floor() as i32 + x0, p0 + y0),
115120
&style.color()
116121
));
117-
check_result!(b.draw_pixel((s.ceil() as i32 + x0 - 1, p0 + y0), style.color().mix(vs)));
118-
check_result!(b.draw_pixel((e.floor() as i32 + x0 + 1, p0 + y0), style.color().mix(ve)));
122+
check_result!(b.draw_pixel((s.ceil() as i32 + x0 - 1, p0 + y0), s1));
123+
check_result!(b.draw_pixel((e.floor() as i32 + x0 + 1, p0 + y0), s2));
119124
}
120125

121126
Ok(())
@@ -319,22 +324,28 @@ pub fn draw_circle<B: DrawingBackend, S: BackendStyle>(
319324
let top = center.1 - lx.floor() as i32;
320325
let bottom = center.1 + lx.floor() as i32;
321326

327+
// Do not use flat color but interpolate when using anti-aliasing
328+
let (s1, s2) = if b.use_anti_aliasing() {
329+
(style.color().mix(1.0 - v), style.color().mix(v))
330+
} else {
331+
(style.color(), style.color())
332+
};
322333
if fill {
323334
check_result!(b.draw_line((left, y), (right, y), &style.color()));
324335
check_result!(b.draw_line((x, top), (x, up - 1), &style.color()));
325336
check_result!(b.draw_line((x, down + 1), (x, bottom), &style.color()));
326337
} else {
327-
check_result!(b.draw_pixel((left, y), style.color().mix(1.0 - v)));
328-
check_result!(b.draw_pixel((right, y), style.color().mix(1.0 - v)));
338+
check_result!(b.draw_pixel((left, y), s1));
339+
check_result!(b.draw_pixel((right, y), s1));
329340

330-
check_result!(b.draw_pixel((x, top), style.color().mix(1.0 - v)));
331-
check_result!(b.draw_pixel((x, bottom), style.color().mix(1.0 - v)));
341+
check_result!(b.draw_pixel((x, top), s1));
342+
check_result!(b.draw_pixel((x, bottom), s1));
332343
}
333344

334-
check_result!(b.draw_pixel((left - 1, y), style.color().mix(v)));
335-
check_result!(b.draw_pixel((right + 1, y), style.color().mix(v)));
336-
check_result!(b.draw_pixel((x, top - 1), style.color().mix(v)));
337-
check_result!(b.draw_pixel((x, bottom + 1), style.color().mix(v)));
345+
check_result!(b.draw_pixel((left - 1, y), s2));
346+
check_result!(b.draw_pixel((right + 1, y), s2));
347+
check_result!(b.draw_pixel((x, top - 1), s2));
348+
check_result!(b.draw_pixel((x, bottom + 1), s2));
338349
}
339350

340351
Ok(())

plotters-backend/src/rasterizer/line.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,16 @@ pub fn draw_line<DB: DrawingBackend, S: BackendStyle>(
8181
let grad = f64::from(to.1 - from.1) / f64::from(to.0 - from.0);
8282

8383
let mut put_pixel = |(x, y): BackendCoord, b: f64| {
84+
// Do not use flat color but interpolate when using anti-aliasing
85+
let st = if back.use_anti_aliasing() {
86+
style.color().mix(b)
87+
} else {
88+
style.color()
89+
};
8490
if steep {
85-
back.draw_pixel((y, x), style.color().mix(b))
91+
back.draw_pixel((y, x), st)
8692
} else {
87-
back.draw_pixel((x, y), style.color().mix(b))
93+
back.draw_pixel((x, y), st)
8894
}
8995
};
9096

plotters-backend/src/rasterizer/polygon.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,34 +200,33 @@ pub fn fill_polygon<DB: DrawingBackend, S: BackendStyle>(
200200
continue;
201201
}
202202

203+
let (mut s1, mut s2) = (style.color(), style.color());
203204
if horizontal_sweep {
205+
// Do not use flat color but interpolate when using anti-aliasing
206+
if back.use_anti_aliasing() {
207+
s1 = style.color().mix(from.ceil() - from);
208+
s2 = style.color().mix(to - to.floor());
209+
}
204210
check_result!(back.draw_line(
205211
(sweep_line, from.ceil() as i32),
206212
(sweep_line, to.floor() as i32),
207213
&style.color(),
208214
));
209-
check_result!(back.draw_pixel(
210-
(sweep_line, from.floor() as i32),
211-
style.color().mix(from.ceil() - from),
212-
));
213-
check_result!(back.draw_pixel(
214-
(sweep_line, to.ceil() as i32),
215-
style.color().mix(to - to.floor()),
216-
));
215+
check_result!(back.draw_pixel((sweep_line, from.floor() as i32), s1,));
216+
check_result!(back.draw_pixel((sweep_line, to.ceil() as i32), s2,));
217217
} else {
218+
// Do not use flat color but interpolate when using anti-aliasing
219+
if back.use_anti_aliasing() {
220+
s1 = style.color().mix(from.ceil() - from);
221+
s2 = style.color().mix(to.floor() - to);
222+
}
218223
check_result!(back.draw_line(
219224
(from.ceil() as i32, sweep_line),
220225
(to.floor() as i32, sweep_line),
221226
&style.color(),
222227
));
223-
check_result!(back.draw_pixel(
224-
(from.floor() as i32, sweep_line),
225-
style.color().mix(from.ceil() - from),
226-
));
227-
check_result!(back.draw_pixel(
228-
(to.ceil() as i32, sweep_line),
229-
style.color().mix(to.floor() - to),
230-
));
228+
check_result!(back.draw_pixel((from.floor() as i32, sweep_line), s1,));
229+
check_result!(back.draw_pixel((to.ceil() as i32, sweep_line), s2,));
231230
}
232231

233232
first = None;

0 commit comments

Comments
 (0)