 p1.y - p2.y;
            return Math.sqrt(dx * dx + dy * dy);
        }

        // --- CubicBezierPatch Class (Global) ---
        class CubicBezierPatch {
            constructor(initialPoints = null) {
                this.points = Array(4).fill(null).map(() => Array(4).fill(null).map(() => createPoint()));
                if (initialPoints) {
                    for (let i = 0; i < 4; i++) {
                        for (let j = 0; j < 4; j++) {
                            if (initialPoints[i] && initia