我正在按照本文中的算法 1来检查一个点是否在三角形内。这是我的代码:
//========================================================================================================================//
// 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));
}
这是我的测试:
青色区域:我给出的真实三角形。
粉色区域:三角形“内部”
蓝色区域:三角形“外部”
编辑:
这是我用向量计算的新代码:
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));
}
请澄清我的代码。谢谢你。