olavolav / uniplot

Lightweight plotting to the terminal. 4x resolution via Unicode.
MIT License
343 stars 16 forks source link

Occasional gaps in lines #4

Closed olavolav closed 3 years ago

olavolav commented 3 years ago

Sometimes there are gaps at the end of lines.

To reproduce

>>> from uniplot import plot
>>> plot(xs=[0.433,0.6666], ys=[0.8,0.1133], x_min=0, x_max=1, y_min=0, y_max=1, lines=True)
DEBUG indices_slope -0.8571428571428571 slope -2.939640410958905
DEBUG x_index_start 51
DEBUG x_index_stop 79
DEBUG x_indices_of_line [52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
 76 77 78]
┌────────────────────────────────────────────────────────────┐
││                                                           │ 1.0
││                                                           │ 
││                                                           │ 
││                        ▝▖                                 │ 
││                         ▝▚                                │ 0.7
││                           ▚                               │ 
││                            ▚▖                             │ 
││                             ▝▖                            │ 
││                              ▝▖                           │ 0.5
││                               ▝▚                          │ 
││                                 ▚                         │ 
││                                  ▚▖                       │ 
││                                   ▝▖                      │ 0.3
││                                    ▝▖                     │ 
││                                     ▝▘                    │ 
││                                      ▝                    │ 
││▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁│ 0.0
└────────────────────────────────────────────────────────────┘
 0                                                          1

This is not a bug, but an artifact of the line drawing attempting to be accurate. If the end pixel is drawn because of an end pint that is significantly away from the center of the pixel then this is possible.

Possible solutions

olavolav commented 3 years ago

Let's go with the first solution for the moment. The result looks like this:

>>> from uniplot import plot
>>> plot(xs=[0.433,0.6666], ys=[0.8,0.1133], x_min=0, x_max=1, y_min=0, y_max=1, lines=True)
┌────────────────────────────────────────────────────────────┐
││                                                           │ 1.0
││                                                           │ 
││                                                           │ 
││                        ▝▖                                 │ 
││                         ▝▚                                │ 0.7
││                           ▚                               │ 
││                            ▚▖                             │ 
││                             ▝▖                            │ 
││                              ▝▖                           │ 0.5
││                               ▝▚                          │ 
││                                 ▚                         │ 
││                                  ▚▖                       │ 
││                                   ▝▖                      │ 0.3
││                                    ▝▖                     │ 
││                                     ▝▚                    │ 
││                                      ▝                    │ 
││▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁│ 0.0
└────────────────────────────────────────────────────────────┘
 0                                                          1
olavolav commented 3 years ago

Actually, the fix that is implemented now does the above but will also reject "overshoot". So it's even a mix between the above solutions.