Skip to content

[BUG] Circle size is fixed over changing coordinate systems. #699

Open
@jdbosser

Description

@jdbosser

Describe the bug
Size of circles are not "respecting" the cartesian grid. Drawing the same circle in two different grids yield a circle that is equally large in the two images, and is not scaled to match the coordinate system difference. Is this intentional?

To Reproduce
I took the code from the snowflake example, and added a circle. Change the line
.build_cartesian_2d(-2.0..2.0, -1.5..1.5)?;
to
.build_cartesian_2d(-4.0..4.0, -3.0..3.0)?;
and then re-export and compare the figures.

Including the snowflake code with a circle in it for completeness.

use plotters::prelude::*;

fn snowflake_iter(points: &[(f64, f64)]) -> Vec<(f64, f64)> {
    let mut ret = vec![];
    for i in 0..points.len() {
        let (start, end) = (points[i], points[(i + 1) % points.len()]);
        let t = ((end.0 - start.0) / 3.0, (end.1 - start.1) / 3.0);
        let s = (
            t.0 * 0.5 - t.1 * (0.75f64).sqrt(),
            t.1 * 0.5 + (0.75f64).sqrt() * t.0,
        );
        ret.push(start);
        ret.push((start.0 + t.0, start.1 + t.1));
        ret.push((start.0 + t.0 + s.0, start.1 + t.1 + s.1));
        ret.push((start.0 + t.0 * 2.0, start.1 + t.1 * 2.0));
    }
    ret
}

const OUT_FILE_NAME: &str = "snowflake.png";
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area();

    root.fill(&WHITE)?;

    let mut chart = ChartBuilder::on(&root)
        .caption("Koch's Snowflake", ("sans-serif", 50))
        .build_cartesian_2d(-2.0..2.0, -1.5..1.5)?;

    let mut snowflake_vertices = {
        let mut current: Vec<(f64, f64)> = vec![
            (0.0, 1.0),
            ((3.0f64).sqrt() / 2.0, -0.5),
            (-(3.0f64).sqrt() / 2.0, -0.5),
        ];
        for _ in 0..6 {
            current = snowflake_iter(&current[..]);
        }
        current
    };

    chart.draw_series(std::iter::once(Polygon::new(
        snowflake_vertices.clone(),
        RED.mix(0.2),
    )))?;
    snowflake_vertices.push(snowflake_vertices[0]);
    chart.draw_series(std::iter::once(PathElement::new(snowflake_vertices, RED)))?;

    chart.draw_series(std::iter::once(Circle::new((0.0, 0.0), 20.0, ShapeStyle {
        color: GREEN.into(),
        filled: true, 
        stroke_width: 2
    })))?;

    // To avoid the IO failure being ignored silently, we manually call the present function
    root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir");
    println!("Result has been saved to {}", OUT_FILE_NAME);
    Ok(())
}
#[test]
fn entry_point() {
    main().unwrap()
}

This produces these two images. One can see that PathElement respects the coordinate system as the star changes size in the image, but the Circle does not.

Using .build_cartesian_2d(-2.0..2.0, -1.5..1.5)?;:
Image

Using .build_cartesian_2d(-4.0..4.0, -3.0..3.0)?;:
Image

Version Information
Installed using cargo add plotters. I have version 0.3.7.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions