Skip to content

Commit 7da7b79

Browse files
committed
Fix vline hline sampling count
1 parent c9d4a8b commit 7da7b79

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

src/unicode_plot/interface/lineplot.cr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,21 @@ module UnicodePlot
170170
end
171171

172172
# vline! and hline! helpers
173+
private def linspace(a : Float64, b : Float64, n : Int32) : Array(Float64)
174+
return [] of Float64 if n <= 0
175+
return [a] if n == 1
176+
(0...n).map { |i| a + (b - a) * i.to_f64 / (n - 1).to_f64 }
177+
end
178+
173179
def vline!(plot : Plot, x : Float64, y : Array(Float64)? = nil, **kwargs) : Plot
174180
can = plot.canvas
175-
ys = y || begin
176-
o = can.origin_y
177-
(0..can.nrows).map { |i| o + (i.to_f / can.nrows) * can.height }
178-
end
181+
ys = y || linspace(can.origin_y, can.origin_y + can.height, can.nrows)
179182
lineplot!(plot, Array.new(ys.size, x), ys, **kwargs)
180183
end
181184

182185
def hline!(plot : Plot, y : Float64, x : Array(Float64)? = nil, **kwargs) : Plot
183186
can = plot.canvas
184-
xs = x || begin
185-
o = can.origin_x
186-
(0..can.ncols).map { |i| o + (i.to_f / can.ncols) * can.width }
187-
end
187+
xs = x || linspace(can.origin_x, can.origin_x + can.width, can.ncols)
188188
lineplot!(plot, xs, Array.new(xs.size, y), **kwargs)
189189
end
190190

0 commit comments

Comments
 (0)