Skip to content

Commit c198878

Browse files
committed
optimised Vertices.contains
1 parent 6883d0d commit c198878

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

src/geometry/Vertices.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,21 @@ var Common = require('../core/Common');
224224
* @return {boolean} True if the vertices contains point, otherwise false
225225
*/
226226
Vertices.contains = function(vertices, point) {
227-
for (var i = 0; i < vertices.length; i++) {
228-
var vertice = vertices[i],
229-
nextVertice = vertices[(i + 1) % vertices.length];
230-
if ((point.x - vertice.x) * (nextVertice.y - vertice.y) + (point.y - vertice.y) * (vertice.x - nextVertice.x) > 0) {
227+
var pointX = point.x,
228+
pointY = point.y,
229+
verticesLength = vertices.length,
230+
vertex = vertices[verticesLength - 1],
231+
nextVertex;
232+
233+
for (var i = 0; i < verticesLength; i++) {
234+
nextVertex = vertices[i];
235+
236+
if ((pointX - vertex.x) * (nextVertex.y - vertex.y)
237+
+ (pointY - vertex.y) * (vertex.x - nextVertex.x) > 0) {
231238
return false;
232239
}
240+
241+
vertex = nextVertex;
233242
}
234243

235244
return true;

0 commit comments

Comments
 (0)