Skip to content

Commit 7c16f72

Browse files
CopilotAzgaar
andauthored
fix: restore secure clipping to prevent landmass being cut at map edges (#1385)
* Initial plan * fix: restore secure clipping to prevent landmass edge artifacts The f2fc427 refactor replaced window.polygonclip (which had a secure=1 mode) with lineclip.clipPolygon, removing the behavior that added each boundary-crossing intersection point three times. Without this, the curveBasisClosed B-spline arcs away from the map edges instead of following them, leaving a visible ocean band at the map boundary. Restore the secure parameter on clipPoly: when secure=1, boundary points are duplicated twice after standard clipping, forcing the B-spline to stay on the boundary. Re-expose the parameter on window.clipPoly for legacy JS callers. Agent-Logs-Url: https://github.com/Azgaar/Fantasy-Map-Generator/sessions/0b83756d-4055-4265-9258-c23b0a3681cd Co-authored-by: Azgaar <26469650+Azgaar@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Azgaar <26469650+Azgaar@users.noreply.github.com>
1 parent 99ab003 commit 7c16f72

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/renderers/draw-features.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function featurePathRenderer(feature: PackedGraphFeature): string {
9595
}
9696

9797
const simplifiedPoints = simplify(points, 0.3);
98-
const clippedPoints = clipPoly(simplifiedPoints, graphWidth, graphHeight);
98+
const clippedPoints = clipPoly(simplifiedPoints, graphWidth, graphHeight, 1);
9999

100100
const lineGen = line().curve(curveBasisClosed);
101101
const path = `${round(lineGen(clippedPoints) || "")}Z`;

src/utils/commonUtils.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,41 @@ import { rand } from "./probabilityUtils";
99
* @param points - Array of points [[x1, y1], [x2, y2], ...]
1010
* @param graphWidth - Width of the graph
1111
* @param graphHeight - Height of the graph
12+
* @param secure - When truthy, duplicate boundary-crossing points to prevent B-spline
13+
* curves from arcing away from map edges (restores original "secure clipping" behavior)
1214
* @returns Clipped polygon points
1315
*/
1416
export const clipPoly = (
1517
points: [number, number][],
1618
graphWidth: number,
1719
graphHeight: number,
20+
secure?: number,
1821
) => {
1922
if (points.length < 2) return points;
2023
if (points.some((point) => point === undefined)) {
2124
window.ERROR && console.error("Undefined point in clipPoly", points);
2225
return points;
2326
}
2427

25-
return clipPolygon(points, [0, 0, graphWidth, graphHeight]);
28+
const clipped = clipPolygon(points, [0, 0, graphWidth, graphHeight]);
29+
30+
if (!secure || !clipped.length) return clipped;
31+
32+
// Duplicate each boundary point twice so the B-spline passes through it
33+
// rather than arcing away from the map edge (replicates polygonclip secure=1)
34+
const secured: [number, number][] = [];
35+
for (const point of clipped) {
36+
secured.push(point);
37+
if (
38+
point[0] === 0 ||
39+
point[0] === graphWidth ||
40+
point[1] === 0 ||
41+
point[1] === graphHeight
42+
) {
43+
secured.push(point, point);
44+
}
45+
}
46+
return secured;
2647
};
2748

2849
/**
@@ -375,7 +396,7 @@ declare global {
375396
interface Window {
376397
ERROR: boolean;
377398

378-
clipPoly: typeof clipPoly;
399+
clipPoly: (points: [number, number][], secure?: number) => [number, number][];
379400
getSegmentId: typeof getSegmentId;
380401
debounce: typeof debounce;
381402
throttle: typeof throttle;

src/utils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ import {
227227
wiki,
228228
} from "./commonUtils";
229229

230-
window.clipPoly = (points: [number, number][]) =>
231-
clipPoly(points, graphWidth, graphHeight);
230+
window.clipPoly = (points: [number, number][], secure?: number) =>
231+
clipPoly(points, graphWidth, graphHeight, secure);
232232
window.getSegmentId = getSegmentId;
233233
window.debounce = debounce;
234234
window.throttle = throttle;

0 commit comments

Comments
 (0)