Skip to content

Commit

Permalink
optimised Vertices.contains
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Apr 29, 2021
1 parent 6883d0d commit c198878
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/geometry/Vertices.js
Expand Up @@ -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;
Expand Down

0 comments on commit c198878

Please sign in to comment.