Skip to content

Fix pistons scaling of circles #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/drawing/backend_impl/piston.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ fn make_point_pair(a: BackendCoord, b: BackendCoord, scale: f64) -> [f64; 4] {
]
}

fn make_circle(center: BackendCoord, radius: u32, scale: f64) -> [f64; 4] {
circle(
center.0 as f64 * scale,
center.1 as f64 * scale,
radius as f64 * scale,
)
}

impl<'a, 'b> PistonBackend<'a, 'b> {
pub fn new(size: (u32, u32), scale: f64, context: Context, graphics: &'b mut G2d<'a>) -> Self {
Self {
Expand Down Expand Up @@ -150,7 +158,7 @@ impl<'a, 'b> DrawingBackend for PistonBackend<'a, 'b> {
style: &S,
fill: bool,
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
let rect = circle(center.0 as f64, center.1 as f64, radius as f64);
let rect = make_circle(center, radius, self.scale);
if fill {
ellipse(
make_piston_rgba(&style.as_color()),
Expand Down Expand Up @@ -204,3 +212,13 @@ pub fn draw_piston_window<F: FnOnce(PistonBackend) -> Result<(), Box<dyn std::er
}
None
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_make_circle() {
assert_eq!(make_circle((1, 1), 0, 1.0), [1.0, 1.0, 0.0, 0.0]);
assert_eq!(make_circle((1, 2), 3, 4.0), [-8.0, -4.0, 24.0, 24.0]);
}
}