I follow Algorithm 1 in this article to check if a point is inside a triangle. This is my code:
//========================================================================================================================// // Methods //========================================================================================================================// private float getPerpDotProduct(final PointF p1, final PointF p2) { return p1.x * p2.y - p1.y * p2.x; } private boolean isInside(final PointF pPoint) { final float c1 = this.getPerpDotProduct(this.mA, pPoint); final float c2 = this.getPerpDotProduct(this.mB, pPoint); final float c3 = this.getPerpDotProduct(this.mC, pPoint); return ((c1 >= 0 && c2 >= 0 & c3 >= 0) || (c1 <= 0 && c2 <= 0 && c3 <= 0)); }
And this is my test: 
Blue zone: the real triangle that I give.
Pink Zone: Inside the Triangle
Blue Zone: Outside Triangle
EDIT:
This is my new vector calculation code:
private PointF getVector(final PointF pPoint1, final PointF pPoint2) { return new PointF(pPoint2.x - pPoint1.x, pPoint2.y - pPoint1.y); } private float getPerpDotProduct(final PointF p1, final PointF p2) { return p1.x * p2.y - p1.y * p2.x; } private boolean isInside(final PointF pPoint) { final float c1 = this.getPerpDotProduct(getVector(this.mA, this.mB), getVector(this.mA, pPoint)); final float c2 = this.getPerpDotProduct(getVector(this.mB, this.mC), getVector(this.mB, pPoint)); final float c3 = this.getPerpDotProduct(getVector(this.mC, this.mA), getVector(this.mC, pPoint)); return ((c1 > 0 && c2 > 0 & c3 > 0) || (c1 < 0 && c2 < 0 && c3 < 0)); }
Please clarify my code. Thanks.
source share