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

[BUG] draw PathElement result looks incorrect #501

Open Lishen1 opened 1 year ago

Lishen1 commented 1 year ago

Describe the bug i'm trying to draw rays (green circles is rays origin) such as:

let p0 = ...;
let p1 = p0 + r * 1e2;
root.draw(&PathElement::new(
    vec![(p0[0], p0[1]), (p1[0], p1[1])],
    ...,
))
.unwrap();

where r is normalized ray direction. if ray len ( r 1e2 ) is big enough rays goes incorrect. all rays must approx cross point 64 image with r 1e1 result is: image with r*2e0 result is: image

with big (r*1e2 and more) ray len rays treng to go into image corners: image

check math:

let d = 1e2;
let p1 = p0 + r * d;
println!("p0: {} p1: {} r: {} d: {}", p0, p1, r, d);
p0:
  ┌                    ┐
  │ 3.3882831114523206 │
  │  2.706594237999449 │
  └                    ┘

 p1:
  ┌                    ┐
  │ 10.279252998404655 │
  │  102.4688843781978 │
  └                    ┘

 r:
  ┌                     ┐
  │ 0.06890969886952335 │
  │  0.9976229014019834 │
  └                     ┘

 d: 100

init plotters as:

    let img_w = 1024_i32;
    let img_h = 768_i32;
    let root_screen = BitMapBackend::gif(OUTPUT_GIF, (img_w as u32, img_h as u32), 100)
        .unwrap()
        .into_drawing_area();
    let marg = 0;
    let root = {
        let scale = img_h as f64 / scene_h;
        let sc_w = ((max_x - min_x) * scale) as i32;
        root_screen
            .margin(marg, marg, marg, marg)
            .apply_coord_spec(Cartesian2d::<RangedCoordf64, RangedCoordf64>::new(
                min_x..max_y,
                max_y..min_y,
                (img_w / 2 - sc_w / 2..sc_w * 2, 0..img_h),
            ))
   }

straightforward mapping don't works too.

Version Information plotters = {version="0.3.5", features = ["ttf", "all_series"] }