7internal readonly record
struct LayerPeriod(
13 internal static LayerPeriod Create(ISceneLayerCoordinates coordinates, SceneLayer layer)
16 throw new InvalidOperationException(
"Wrapping requires positive grid and tile dimensions.");
18 PointF
Period(
int columns,
int rows,
bool enabled)
20 var origin = coordinates.GetAnchorPixelAtSceneLayerCoordinates(layer, PointF.Empty);
21 var end = coordinates.GetAnchorPixelAtSceneLayerCoordinates(layer,
new(columns, rows));
22 var delta =
new PointF(end.X - origin.X, end.Y - origin.Y);
27 for (
int x = -2; x <= 2; x++)
28 for (
int y = -2; y <= 2; y++)
30 var a = coordinates.GetAnchorPixelAtSceneLayerCoordinates(layer,
new(x, y));
31 var b = coordinates.GetAnchorPixelAtSceneLayerCoordinates(layer,
new(x + columns, y + rows));
33 if (b.X - a.X != delta.X || b.Y - a.Y != delta.Y)
34 throw new InvalidOperationException(
35 $
"{layer.CoordinateSystemType} cannot repeat cleanly with these dimensions. Flat-top hex column wrapping requires even columns; pointed-top hex row wrapping requires even rows. Pixel-rounded projections also require an integral world-space period.");
42 var result =
new LayerPeriod(
48 if (Math.Abs(result.Columns.X * result.Rows.Y - result.Columns.Y * result.Rows.X) < 0.001)
49 throw new InvalidOperationException(
"Wrapping requires independent, nonzero world-space period vectors.");
54 internal PointF Coefficients(PointF delta)
56 double determinant = (double)Columns.X * Rows.Y - (
double)Columns.Y * Rows.X;
59 (float)((delta.X * (double)Rows.Y - delta.Y * (double)Rows.X) / determinant),
60 (float)((Columns.X * (double)delta.Y - Columns.Y * (double)delta.X) / determinant));
63 internal PointF Offset(
double column,
double row) =>
64 new((float)(column * Columns.X + row * Rows.X), (float)(column * Columns.Y + row * Rows.Y));
66 internal PointF Nearest(PointF point, PointF reference)
68 var delta =
new PointF(reference.X - point.X, reference.Y - point.Y);
69 var coefficients = Coefficients(delta);
72 double c = WrapColumns ? Math.Round(coefficients.X) : 0;
73 double r = WrapRows ? Math.Round(coefficients.Y) : 0;
75 if (WrapColumns && !WrapRows)
77 ((
double)delta.X * Columns.X + (
double)delta.Y * Columns.Y) /
78 ((
double)Columns.X * Columns.X + (
double)Columns.Y * Columns.Y));
80 if (WrapRows && !WrapColumns)
82 ((
double)delta.X * Rows.X + (
double)delta.Y * Rows.Y) /
83 ((
double)Rows.X * Rows.X + (
double)Rows.Y * Rows.Y));
85 var initial = Offset(c, r);
86 var best =
new PointF(point.X + initial.X, point.Y + initial.Y);
88 if (!WrapColumns || !WrapRows)
91 double Distance(PointF p) => Math.Pow(p.X - reference.X, 2) + Math.Pow(p.Y - reference.Y, 2);
92 double distance = Distance(best);
93 float radius = (float)Math.Sqrt(distance) + 1;
96 foreach (var offset
in Offsets(
103 reference.X - radius,
104 reference.Y - radius,
108 var candidate =
new PointF(point.X + offset.X, point.Y + offset.Y);
109 double next = Distance(candidate);
121 internal IEnumerable<PointF> Offsets(RectangleF content, RectangleF query)
126 Coefficients(
new(query.Left - content.Right, query.Top - content.Bottom)),
127 Coefficients(
new(query.Right - content.Left, query.Top - content.Bottom)),
128 Coefficients(
new(query.Left - content.Right, query.Bottom - content.Top)),
129 Coefficients(
new(query.Right - content.Left, query.Bottom - content.Top))
132 double minX = WrapColumns ? Math.Ceiling(corners.Min(p => p.X)) : 0;
133 double maxX = WrapColumns ? Math.Floor(corners.Max(p => p.X)) : 0;
134 double minY = WrapRows ? Math.Ceiling(corners.Min(p => p.Y)) : 0;
135 double maxY = WrapRows ? Math.Floor(corners.Max(p => p.Y)) : 0;
136 double count = Math.Max(0, maxX - minX + 1) * Math.Max(0, maxY - minY + 1);
138 if (!
double.IsFinite(count) || count > 1_000_000 || Math.Abs(minX) >
int.MaxValue ||
139 Math.Abs(maxX) >
int.MaxValue || Math.Abs(minY) >
int.MaxValue || Math.Abs(maxY) >
int.MaxValue)
140 throw new InvalidOperationException(
"The requested wrapped region exceeds the supported instance range.");
142 for (
double x = minX; x <= maxX; x++)
143 for (
double y = minY; y <= maxY; y++)
145 var offset = Offset(x, y);
146 var translated = content;
147 translated.Offset(offset);
149 if (translated.IntersectsWith(query))
bool WrapVertically
Gets or sets a value indicating whether this layer repeats along its row axis at the grid boundaries.
bool WrapHorizontally
Gets or sets a value indicating whether this layer repeats along its column axis at the grid boundari...
int GridRowCount
Gets the number of rows (tiles high) in this layer's grid.
int TileHeight
Gets or sets the height of each tile in this layer, measured in pixels.
int GridColumnCount
Gets the number of columns (tiles wide) in this layer's grid.
int TileWidth
Gets or sets the width of each tile in this layer, measured in pixels.