A large number of approaches exist for testing the containment of a point in a polygon.
A convex polygon is a simple polygon (with no self intersections) such that any line segment between two points inside of the polygon lies completely inside of it.
If it is convex, a trivial way to check it is that the point is laying on the same side of all the segments (if traversed in the same order).
You can check that easily with the dot product (as it is proportional to the cosine of the angle formed between the segment and the point, if we calculate it with the normal of the edge, those with positive sign would lay on the right side and those with negative sign on the left side).
/**
* @see box2d.b2Shape::TestPoint
* @export
* @return {boolean}
* @param {box2d.b2Transform} xf
* @param {box2d.b2Vec2} p
*/
box2d.b2PolygonShape.prototype.TestPoint = function(xf, p) {
var pLocal = box2d.b2MulT_X_V2(xf, p, box2d.b2PolygonShape.prototype.TestPoint.s_pLocal);
for (var i = 0, ict = this.m_count; i < ict; ++i) {
var dot = box2d.b2Dot_V2_V2(this.m_normals[i], box2d.b2Sub_V2_V2(pLocal, this.m_vertices[i], box2d.b2Vec2.s_t0));
if (dot > 0) {
return false;
}
}
return true;
}
box2d.b2PolygonShape.prototype.TestPoint.s_pLocal = new box2d.b2Vec2();
Comments