{"record":{"id":"e13309e240779b19","repo":"phaserjs/phaser","slug":"getcentroid-points-be-a-non-empty-array","errorCode":null,"errorMessage":"GetCentroid points be a non-empty array","messagePattern":"GetCentroid points be a non-empty array","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/math/GetCentroid.js","lineNumber":31,"sourceCode":" * @function Phaser.Math.GetCentroid\n * @since 4.0.0\n *\n * @generic {Phaser.Math.Vector2} O - [out,$return]\n *\n * @param {Phaser.Types.Math.Vector2Like[]} points - An array of Vector2Like objects to get the geometric center of.\n * @param {Phaser.Math.Vector2} [out] - A Vector2 object to store the output coordinates in. If not given, a new Vector2 instance is created.\n *\n * @return {Phaser.Math.Vector2} A Vector2 object representing the geometric center of the given points.\n */\nvar GetCentroid = function (points, out)\n{\n    if (out === undefined) { out = new Vector2(); }\n\n    var len = points.length;\n\n    if (!Array.isArray(points) || len === 0)\n    {\n        throw new Error('GetCentroid points be a non-empty array');\n    }\n\n    if (len === 1)\n    {\n        out.x = points[0].x;\n        out.y = points[0].y;\n    }\n    else\n    {\n        for (var i = 0; i < len; i++)\n        {\n            out.x += points[i].x;\n            out.y += points[i].y;\n        }\n\n        out.x /= len;\n        out.y /= len;\n    }","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/phaserjs/phaser/blob/41be1e462bc600064e498cba370bfa8c5c055a22/src/math/GetCentroid.js#L13-L49","documentation":"GetCentroid.js:31 throws when `points` is not an array or is an empty array. The function averages all point coordinates to find the geometric center, so it needs at least one valid `{x, y}` point. The guard checks both `Array.isArray(points)` and `len === 0`.","triggerScenarios":"Calling `Phaser.Geom.Point.GetCentroid(points)` (or `Phaser.Math.GetCentroid`) with `points = []`, `points = undefined`, or `points = null`. Also passing a non-array iterable (Set/Map values) which fails `Array.isArray`.","commonSituations":"Computing a centroid from a polygon/vertex list that was filtered to empty; passing the result of a `.filter()`/`.map()` that returned no items; deserialising geometry data that came back empty.","solutions":["Guard the call site: only invoke GetCentroid when `Array.isArray(points) && points.length > 0`.","Provide a default non-empty points array as the function's `out`/fallback.","Fix the upstream logic producing the empty array."],"exampleFix":"// before\nconst c = Phaser.Math.GetCentroid(filteredPoints)\n// after\nconst c = filteredPoints.length ? Phaser.Math.GetCentroid(filteredPoints) : new Phaser.Math.Vector2(0, 0)","handlingStrategy":"type-guard","validationCode":"if (!Array.isArray(points) || points.length === 0) {\n  return new Phaser.Math.Vector2(0, 0)\n}\nreturn Phaser.Math.GetCentroid(points)","typeGuard":"const isNonEmptyPointArray = (p) => Array.isArray(p) && p.length > 0 && p.every(pt => pt && typeof pt.x === 'number' && typeof pt.y === 'number')","tryCatchPattern":null,"preventionTips":["Guard centroid calls behind an array-length check.","Validate geometry data after deserialisation before passing to math helpers.","Document that GetCentroid requires >= 1 point at every call site."],"tags":["math","geom","validation","runtime"],"backgroundTag":null,"analyzedSha":"41be1e462bc600064e498cba370bfa8c5c055a22","analyzedAt":"2026-08-13T04:23:39.729Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}