diff --git a/StarterNG.Tests/ColumnLineTests.cs b/StarterNG.Tests/ColumnLineTests.cs new file mode 100644 index 0000000..d501ad4 --- /dev/null +++ b/StarterNG.Tests/ColumnLineTests.cs @@ -0,0 +1,55 @@ +using Avalonia; +using StarterNG.Views.Tapes; + +namespace StarterNG.Tests; + +/// The line of a tape, drawn a column of pixels at a time and broken off at a gap. +public class ColumnLineTests +{ + private readonly List _drawn = []; + + private ColumnLine Line() => + new(p => _drawn.Add($"begin {p.X} {p.Y}"), p => _drawn.Add($"line {p.X} {p.Y}"), () => _drawn.Add("end")); + + [Fact] + public void A_column_is_entered_where_it_started_and_left_where_it_ended_through_its_lowest_and_highest() + { + var line = Line(); + + // Falling within the column: from its first through its highest and its lowest to its last. + foreach (double v in new[] { 3.0, 1.0, 4.0, 2.0 }) + line.Add(5, v); + // Rising: through its lowest first. + foreach (double v in new[] { 2.0, 4.0, 1.0, 3.0 }) + line.Add(6, v); + line.Break(); + + Assert.Equal(["begin 5 3", "line 5 4", "line 5 1", "line 5 2", + "line 6 2", "line 6 1", "line 6 4", "line 6 3", "end"], _drawn); + } + + [Fact] + public void A_break_ends_the_figure_and_the_next_point_begins_another() + { + var line = Line(); + + line.Add(0, 10); + line.Break(); + line.Add(9, 20); + line.Break(); + + Assert.Equal(["begin 0 10", "line 0 10", "line 0 10", "line 0 10", "end", + "begin 9 20", "line 9 20", "line 9 20", "line 9 20", "end"], _drawn); + } + + [Fact] + public void A_break_with_nothing_drawn_ends_nothing() + { + var line = Line(); + + line.Break(); + line.Break(); + + Assert.Empty(_drawn); + } +} diff --git a/StarterNG/Views/Tapes/ColumnLine.cs b/StarterNG/Views/Tapes/ColumnLine.cs new file mode 100644 index 0000000..7273681 --- /dev/null +++ b/StarterNG/Views/Tapes/ColumnLine.cs @@ -0,0 +1,65 @@ +using System; +using Avalonia; + +namespace StarterNG.Views.Tapes; + +/// +/// The line of a tape, one column of pixels at a time: of each column, the first, the lowest, the highest and the +/// last point in it, entered where the column started and left where it ended, so a long trip draws as fast as a +/// short one. The figures go out through the three calls given: one begun at its first point, a line to each next, +/// and its end. +/// +/// +/// Its state is kept in fields, not in the locals of a local function, which Sonar's analysis doesn't see changed +/// by it (S2583 took the end of a figure after a gap for code never run). +/// +internal sealed class ColumnLine(Action begin, Action lineTo, Action end) +{ + private bool _begun; + private int _column = int.MinValue; + private double _top, _bottom, _first, _last; + + /// The point of a sample, at its column of pixels. + public void Add(int column, double value) + { + if (column != _column) + { + Flush(); + _column = column; + _top = _bottom = _first = value; + } + _top = Math.Min(_top, value); + _bottom = Math.Max(_bottom, value); + _last = value; + } + + /// The line ends here, at a gap or at the end of the tape; a point added after begins a figure of its own. + public void Break() + { + Flush(); + _column = int.MinValue; + if (!_begun) + return; + end(); + _begun = false; + } + + private void Flush() + { + if (_column == int.MinValue) + return; + // Enter where the column started, leave where it ended. + var (a, b) = _first <= _last ? (_top, _bottom) : (_bottom, _top); + foreach (double v in new[] { _first, a, b, _last }) + { + var p = new Point(_column, v); + if (_begun) + lineTo(p); + else + { + begin(p); + _begun = true; + } + } + } +} diff --git a/StarterNG/Views/Tapes/TapeView.cs b/StarterNG/Views/Tapes/TapeView.cs index 5cf5e47..d02f9ec 100644 --- a/StarterNG/Views/Tapes/TapeView.cs +++ b/StarterNG/Views/Tapes/TapeView.cs @@ -341,60 +341,18 @@ public abstract class TapeView : Control { var geometry = new StreamGeometry(); using var g = geometry.Open(); - bool begun = false; - int column = int.MinValue; - double top = 0, bottom = 0, first = 0, last = 0; - - void Flush() - { - if (column == int.MinValue) - return; - // Enter where the column started, leave where it ended. - var (a, b) = first <= last ? (top, bottom) : (bottom, top); - foreach (double v in new[] { first, a, b, last }) - { - var p = new Point(column, v); - if (!begun) - { - g.BeginFigure(p, false); - begun = true; - } - else - g.LineTo(p); - } - } - + var line = new ColumnLine(p => g.BeginFigure(p, false), p => g.LineTo(p), () => g.EndFigure(false)); double? before = null; foreach (var s in tape.Samples) { // A gap, the player in another vehicle, is not drawn as if the train drove or stood through it: the // line breaks off there. if (before is { } previous && s.Time - previous > tape.GapSeconds) - { - Flush(); - column = int.MinValue; - if (begun) - { - g.EndFigure(false); - begun = false; - } - } + line.Break(); before = s.Time; - int c = (int)Math.Round(XOf(s)); - double value = y(s); - if (c != column) - { - Flush(); - column = c; - top = bottom = first = value; - } - top = Math.Min(top, value); - bottom = Math.Max(bottom, value); - last = value; + line.Add((int)Math.Round(XOf(s)), y(s)); } - Flush(); - if (begun) - g.EndFigure(false); + line.Break(); return geometry; } }