plotters-rs / plotters

A rust drawing library for high quality data plotting for both WASM and native, statically and realtimely 🦀 📈🚀
https://plotters-rs.github.io/home/
MIT License
3.89k stars 281 forks source link

Fix bug where close-to-collinear line segments with thickness >= 2 got size `f64::INFINITTY as i32` as coordinates. #574

Closed el-hult closed 6 months ago

el-hult commented 6 months ago

Bugfix for #562 .

The root cause of the bug was a comparison of floats, but to make sure I got it right, I changed the code away from early returns (that was part of why the bug surfaced) and added some more comments.

This script shows example thick lines with inside and outside corners, and the capping feature.

use plotters::prelude::*;
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
    let root = BitMapBackend::new("test.bmp", (1000, 1000)).into_drawing_area();
    let mut chart = ChartBuilder::on(&root)
        .build_cartesian_2d(0.0..1000.0, 0.0..1000.0)?;
    let pahts = vec![
        PathElement::new(
            vec![(336.0, 614.0), (339.0, 674.0),(341.0,714.0)],
            ShapeStyle::from(RED).stroke_width(2),
        ),
        PathElement::new(
            vec![(100.0, 100.0), (150.0, 150.0),(200.0,100.0)],
            ShapeStyle::from(BLUE).stroke_width(2),
        ),
        PathElement::new(
            vec![(400.0, 400.0), (400.0, 450.0),(400.0,500.0)],
            ShapeStyle::from(GREEN).stroke_width(5),
        ),
        PathElement::new(
            vec![(900.0, 410.0), (600.0, 400.0),(900.0,500.0), (1000.0, 1000.0)],
            ShapeStyle::from(YELLOW).stroke_width(10),
        ),
    ];
    chart.draw_series(pahts.into_iter())?;
    Ok(())
}
el-hult commented 6 months ago

Hi @AaronErhardt You asked in #345 that we should ping you if the issue goes without notice. Time to take a look at this PR?

I have done what I hope is a quite minimal fix. I have created a unit test and an example that shows the output. Is okay?

el-hult commented 6 months ago

@AaronErhardt I did revert a bit of the changes, to make the PR smaller and easier to review. Looks okay now?

el-hult commented 6 months ago

Now I can go back to use the main plotters-rs instead of my fork. Thanks a lot!