我下面的算法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));
}
请澄清我的代码。谢谢。
本文的描述中有一个“ bug”,说明需要做什么:
用测试点P计算所有三个点V1,V2,V3的perpDotProduct /叉积
应该为“ 使用到测试点P的向量来计算所有三个向量的perpDotProduct /叉积 ”。
如本文所述,true
如果从原点(图片的左上角)在相同的“角度方向”上所有三个点都可见,则该算法将返回。您的图片也精确地显示出来:(0, p)
如果所有粉红色点的矢量都在蓝色区域上方,则它们需要顺时针旋转才能到达三角形。如果它们在蓝色区域以下,则矢量将需要逆时针移动。
要解决这个算法,你需要计算向量的交叉产品{(V1-V2), (V1-P)}
,{(V2-V3), (V2-P)}
和{(V3-V1), (V3-P)}
。看一下本文中的伪代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句