Skip to content

Commit 4c63766

Browse files
authored
Merge pull request #148 from pelekhay/master
Fix pistons scaling of circles
2 parents dee7727 + 5244a9f commit 4c63766

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/drawing/backend_impl/piston.rs

+19-1
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,7 +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(center.0 as f64, center.1 as f64, radius as f64);
161+
let rect = make_circle(center, radius, self.scale);
154162
if fill {
155163
ellipse(
156164
make_piston_rgba(&style.as_color()),
@@ -204,3 +212,13 @@ pub fn draw_piston_window<F: FnOnce(PistonBackend) -> Result<(), Box<dyn std::er
204212
}
205213
None
206214
}
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)