Skip to content

Commit 5244a9f

Browse files
committed
refactored call to piston_window::circle into make_circle and added unit-test for it
1 parent 5f3d801 commit 5244a9f

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/drawing/backend_impl/piston.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ fn make_point_pair(a: BackendCoord, b: BackendCoord, scale: f64) -> [f64; 4] {
3434
]
3535
}
3636

37+
fn make_circle(center: BackendCoord, radius: u32, scale: f64) -> [f64; 4] {
38+
circle(
39+
center.0 as f64 * scale,
40+
center.1 as f64 * scale,
41+
radius as f64 * scale,
42+
)
43+
}
44+
3745
impl<'a, 'b> PistonBackend<'a, 'b> {
3846
pub fn new(size: (u32, u32), scale: f64, context: Context, graphics: &'b mut G2d<'a>) -> Self {
3947
Self {
@@ -150,11 +158,7 @@ impl<'a, 'b> DrawingBackend for PistonBackend<'a, 'b> {
150158
style: &S,
151159
fill: bool,
152160
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
153-
let rect = circle(
154-
center.0 as f64 * self.scale,
155-
center.1 as f64 * self.scale,
156-
radius as f64 * self.scale,
157-
);
161+
let rect = make_circle(center, radius, self.scale);
158162
if fill {
159163
ellipse(
160164
make_piston_rgba(&style.as_color()),
@@ -208,3 +212,13 @@ pub fn draw_piston_window<F: FnOnce(PistonBackend) -> Result<(), Box<dyn std::er
208212
}
209213
None
210214
}
215+
216+
#[cfg(test)]
217+
mod test {
218+
use super::*;
219+
#[test]
220+
fn test_make_circle() {
221+
assert_eq!(make_circle((1, 1), 0, 1.0), [1.0, 1.0, 0.0, 0.0]);
222+
assert_eq!(make_circle((1, 2), 3, 4.0), [-8.0, -4.0, 24.0, 24.0]);
223+
}
224+
}

0 commit comments

Comments
 (0)