From c1988783db2d97bda610254fa07dc95f5b1d59ea Mon Sep 17 00:00:00 2001 From: liabru Date: Mon, 26 Apr 2021 23:48:00 +0100 Subject: [PATCH] optimised Vertices.contains --- src/geometry/Vertices.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/geometry/Vertices.js b/src/geometry/Vertices.js index ae0b279..f28d7a9 100644 --- a/src/geometry/Vertices.js +++ b/src/geometry/Vertices.js @@ -224,12 +224,21 @@ var Common = require('../core/Common'); * @return {boolean} True if the vertices contains point, otherwise false */ Vertices.contains = function(vertices, point) { - for (var i = 0; i < vertices.length; i++) { - var vertice = vertices[i], - nextVertice = vertices[(i + 1) % vertices.length]; - if ((point.x - vertice.x) * (nextVertice.y - vertice.y) + (point.y - vertice.y) * (vertice.x - nextVertice.x) > 0) { + var pointX = point.x, + pointY = point.y, + verticesLength = vertices.length, + vertex = vertices[verticesLength - 1], + nextVertex; + + for (var i = 0; i < verticesLength; i++) { + nextVertex = vertices[i]; + + if ((pointX - vertex.x) * (nextVertex.y - vertex.y) + + (pointY - vertex.y) * (vertex.x - nextVertex.x) > 0) { return false; } + + vertex = nextVertex; } return true;