fixes and stuff

This commit is contained in:
tyuXX 2026-10-29 11:45:21 +03:00
parent 99fd7ab61a
commit 5db8423cdb
3 changed files with 153 additions and 26 deletions

View file

@ -126,4 +126,64 @@ public sealed class Viewport
return col >= 0 && col < viewCols && row >= 0 && row < viewRows;
}
public bool MathXToSubpixel(double x, int subWidth, out int subX)
{
if (subWidth <= 1) subWidth = 2;
if (double.IsNaN(x) || double.IsInfinity(x))
{
subX = 0;
return false;
}
double normX = (x - XMin) / Width;
normX = System.Math.Clamp(normX, -10000.0, 10000.0);
subX = (int)System.Math.Round(normX * (subWidth - 1));
return subX >= 0 && subX < subWidth;
}
public bool MathYToSubpixel(double y, int subHeight, out int subY)
{
if (subHeight <= 1) subHeight = 2;
if (double.IsNaN(y) || double.IsInfinity(y))
{
subY = 0;
return false;
}
double normY = (YMax - y) / Height;
normY = System.Math.Clamp(normY, -10000.0, 10000.0);
subY = (int)System.Math.Round(normY * (subHeight - 1));
return subY >= 0 && subY < subHeight;
}
public bool MathXToScreen(double x, int viewCols, out int col)
{
if (viewCols <= 1) viewCols = 2;
if (double.IsNaN(x) || double.IsInfinity(x))
{
col = 0;
return false;
}
double normX = (x - XMin) / Width;
normX = System.Math.Clamp(normX, -10000.0, 10000.0);
col = (int)System.Math.Round(normX * (viewCols - 1));
return col >= 0 && col < viewCols;
}
public bool MathYToScreen(double y, int viewRows, out int row)
{
if (viewRows <= 1) viewRows = 2;
if (double.IsNaN(y) || double.IsInfinity(y))
{
row = 0;
return false;
}
double normY = (YMax - y) / Height;
normY = System.Math.Clamp(normY, -10000.0, 10000.0);
row = (int)System.Math.Round(normY * (viewRows - 1));
return row >= 0 && row < viewRows;
}
}

View file

@ -63,21 +63,25 @@ public sealed class PlotRenderer
int subH = canvas.SubpixelHeight;
// X axis: y = 0
if (viewport.MathToSubpixel(0, 0, subW, subH, out _, out int axisY))
// If y = 0 is on-screen, draw at axisY.
// If y = 0 is off-screen, draw a reference border axis line along the edge (bottom if YMin > 0, top if YMax < 0).
bool hasXAxisOnScreen = viewport.MathYToSubpixel(0, subH, out int axisY);
int drawAxisY = hasXAxisOnScreen ? axisY : (viewport.YMin > 0 ? subH - 1 : 0);
for (int x = 0; x < subW; x++)
{
for (int x = 0; x < subW; x++)
{
canvas.SetPixel(x, axisY, ColorRgb.AxisLine, CanvasLayer.Axis);
}
canvas.SetPixel(x, drawAxisY, ColorRgb.AxisLine, CanvasLayer.Axis);
}
// Y axis: x = 0
if (viewport.MathToSubpixel(0, 0, subW, subH, out int axisX, out _))
// If x = 0 is on-screen, draw at axisX.
// If x = 0 is off-screen, draw a reference border axis line along the edge (left if XMin > 0, right if XMax < 0).
bool hasYAxisOnScreen = viewport.MathXToSubpixel(0, subW, out int axisX);
int drawAxisX = hasYAxisOnScreen ? axisX : (viewport.XMin > 0 ? 0 : subW - 1);
for (int y = 0; y < subH; y++)
{
for (int y = 0; y < subH; y++)
{
canvas.SetPixel(axisX, y, ColorRgb.AxisLine, CanvasLayer.Axis);
}
canvas.SetPixel(drawAxisX, y, ColorRgb.AxisLine, CanvasLayer.Axis);
}
}
@ -86,12 +90,15 @@ public sealed class PlotRenderer
int subW = canvas.SubpixelWidth;
int subH = canvas.SubpixelHeight;
bool hasXAxisOnScreen = viewport.MathYToSubpixel(0, subH, out _);
bool hasYAxisOnScreen = viewport.MathXToSubpixel(0, subW, out _);
// Vertical grid lines
double firstX = System.Math.Floor(viewport.XMin / xStep) * xStep;
for (double x = firstX; x <= viewport.XMax; x += xStep)
for (double x = firstX; x <= viewport.XMax + xStep * 0.5; x += xStep)
{
if (System.Math.Abs(x) < 1e-9) continue; // Axis handles x=0
if (viewport.MathToSubpixel(x, 0, subW, subH, out int subX, out _))
if (System.Math.Abs(x) < 1e-9 && hasYAxisOnScreen) continue; // Y axis handles x=0 if on screen
if (viewport.MathXToSubpixel(x, subW, out int subX))
{
for (int y = 0; y < subH; y += 4)
{
@ -102,10 +109,10 @@ public sealed class PlotRenderer
// Horizontal grid lines
double firstY = System.Math.Floor(viewport.YMin / yStep) * yStep;
for (double y = firstY; y <= viewport.YMax; y += yStep)
for (double y = firstY; y <= viewport.YMax + yStep * 0.5; y += yStep)
{
if (System.Math.Abs(y) < 1e-9) continue; // Axis handles y=0
if (viewport.MathToSubpixel(0, y, subW, subH, out _, out int subY))
if (System.Math.Abs(y) < 1e-9 && hasXAxisOnScreen) continue; // X axis handles y=0 if on screen
if (viewport.MathYToSubpixel(y, subH, out int subY))
{
for (int x = 0; x < subW; x += 4)
{
@ -126,14 +133,24 @@ public sealed class PlotRenderer
int cellH = canvas.CellHeight;
// Find cell row for X axis
bool hasXAxis = viewport.MathToScreen(0, 0, cellW, cellH, out _, out int axisCellY);
int labelCellY = hasXAxis ? System.Math.Clamp(axisCellY + 1, 0, cellH - 1) : cellH - 1;
bool hasXAxis = viewport.MathYToScreen(0, cellH, out int axisCellY);
int labelCellY;
if (hasXAxis)
{
labelCellY = System.Math.Clamp(axisCellY + 1, 0, cellH - 1);
}
else
{
labelCellY = viewport.YMin > 0 ? cellH - 1 : 0;
}
bool hasYAxis = viewport.MathXToScreen(0, cellW, out int axisCellX);
double firstX = System.Math.Floor(viewport.XMin / xStep) * xStep;
for (double x = firstX; x <= viewport.XMax; x += xStep)
for (double x = firstX; x <= viewport.XMax + xStep * 0.5; x += xStep)
{
if (System.Math.Abs(x) < 1e-9) continue;
if (viewport.MathToScreen(x, 0, cellW, cellH, out int cellX, out _))
if (System.Math.Abs(x) < 1e-9 && hasXAxis && hasYAxis) continue;
if (viewport.MathXToScreen(x, cellW, out int cellX))
{
string text = FormatNumber(x);
int startCol = cellX - text.Length / 2;
@ -145,14 +162,21 @@ public sealed class PlotRenderer
}
// Find cell col for Y axis
bool hasYAxis = viewport.MathToScreen(0, 0, cellW, cellH, out int axisCellX, out _);
int labelCellX = hasYAxis ? System.Math.Clamp(axisCellX + 1, 0, cellW - 6) : 1;
int labelCellX;
if (hasYAxis)
{
labelCellX = System.Math.Clamp(axisCellX + 1, 0, cellW - 6);
}
else
{
labelCellX = viewport.XMin > 0 ? 1 : cellW - 7;
}
double firstY = System.Math.Floor(viewport.YMin / yStep) * yStep;
for (double y = firstY; y <= viewport.YMax; y += yStep)
for (double y = firstY; y <= viewport.YMax + yStep * 0.5; y += yStep)
{
if (System.Math.Abs(y) < 1e-9) continue;
if (viewport.MathToScreen(0, y, cellW, cellH, out _, out int cellY))
if (System.Math.Abs(y) < 1e-9 && hasYAxis && hasXAxis) continue;
if (viewport.MathYToScreen(y, cellH, out int cellY))
{
string text = FormatNumber(y);
if (cellY >= 0 && cellY < cellH && labelCellX + text.Length < cellW)
@ -679,8 +703,10 @@ public sealed class PlotRenderer
private static double CalculateNiceStep(double range, int maxTicks)
{
if (double.IsNaN(range) || double.IsInfinity(range) || range <= 1e-12) range = 10.0;
if (maxTicks <= 0) maxTicks = 5;
double rawStep = range / maxTicks;
if (rawStep <= 1e-12) return 1.0;
double power = System.Math.Floor(System.Math.Log10(rawStep));
double fraction = rawStep / System.Math.Pow(10, power);

View file

@ -158,5 +158,46 @@ public static class BrailleCanvasTests
}
Assert.True(cyanCells > 0, "Expected Cyan cells for line y=x");
});
runner.Add("PlotRenderer: Grid, axes, and ticks render far from origin", () =>
{
var canvas = new BrailleCanvas(30, 20);
var vp = new Viewport
{
XMin = 500,
XMax = 550,
YMin = 200,
YMax = 250
};
// Origin (0, 0) is far off screen in both dimensions
var ticks = PlotRenderer.Render(canvas, vp, [], _ => 0, showGrid: true);
// 1. Ticks should be computed for both X and Y axes
Assert.True(ticks.Count > 0, "Expected axis tick numbers when far from origin");
bool hasXTick = ticks.Any(t => t.IsXAxis);
bool hasYTick = ticks.Any(t => !t.IsXAxis);
Assert.True(hasXTick, "Expected X axis tick numbers when far from origin");
Assert.True(hasYTick, "Expected Y axis tick numbers when far from origin");
// 2. Grid dots and Axis lines should be rasterized on canvas
int gridCells = 0;
int axisCells = 0;
for (int r = 0; r < canvas.CellHeight; r++)
{
for (int c = 0; c < canvas.CellWidth; c++)
{
var (ch, fg, _) = canvas.GetCell(c, r);
if (ch != ' ')
{
if (fg == ColorRgb.GridLine) gridCells++;
if (fg == ColorRgb.AxisLine) axisCells++;
}
}
}
Assert.True(gridCells > 0, $"Expected background grid line cells far from origin, got {gridCells}");
Assert.True(axisCells > 0, $"Expected reference axis line cells far from origin, got {axisCells}");
});
}
}