mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-18 14:35:59 +01:00
refactor(tapes): the line of a tape drawn by a class of its own, so Sonar sees a figure ended after a gap
Sonar (S2583, the reliability rating of #54) took the end of a figure after a gap in Trace for code never run: begun was changed by the local function Flush, and its analysis doesn't follow a local changed that way. Nothing was wrong in what was drawn. The columns of the line are kept by ColumnLine, in its fields, and drawn through the three calls it is given, the figure begun, a line to the next point and its end; Trace hands it those of the StreamGeometry. What is drawn is the same. Tests: a column entered where it started and left where it ended through its lowest and highest, a break ending the figure and the next point beginning another, a break with nothing drawn ending nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
55
StarterNG.Tests/ColumnLineTests.cs
Normal file
55
StarterNG.Tests/ColumnLineTests.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using Avalonia;
|
||||
using StarterNG.Views.Tapes;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
/// <summary>The line of a tape, drawn a column of pixels at a time and broken off at a gap.</summary>
|
||||
public class ColumnLineTests
|
||||
{
|
||||
private readonly List<string> _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);
|
||||
}
|
||||
}
|
||||
65
StarterNG/Views/Tapes/ColumnLine.cs
Normal file
65
StarterNG/Views/Tapes/ColumnLine.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using Avalonia;
|
||||
|
||||
namespace StarterNG.Views.Tapes;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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).
|
||||
/// </remarks>
|
||||
internal sealed class ColumnLine(Action<Point> begin, Action<Point> lineTo, Action end)
|
||||
{
|
||||
private bool _begun;
|
||||
private int _column = int.MinValue;
|
||||
private double _top, _bottom, _first, _last;
|
||||
|
||||
/// <summary>The point of a sample, at its column of pixels.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The line ends here, at a gap or at the end of the tape; a point added after begins a figure of its own.</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user