Luk*_* Vo 6 java algorithm collision-detection
我下面的算法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));
}
Run Code Online (Sandbox Code Playgroud)
这是我的考验:

青色区域:我给出的真正三角形.
粉红色区域:"内部"三角形
蓝色区域:"外部"三角形
编辑:
这是我用向量计算的新代码:
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));
}
Run Code Online (Sandbox Code Playgroud)
请澄清我的代码.谢谢.
文章中关于需要做什么的描述中有一个“错误”:
计算所有三个点 V1、V2、V3 与测试点 P 的 perpDotProduct/叉积
应该是“计算所有三个向量与测试点 P 的向量的 perpDotProduct/叉积”。
正如本文中所解释的,true如果所有三个点在从原点(图片的左上角)相同的“角度方向”上可见,则算法将返回。你的图片也准确地显示了这一点:(0, p)如果所有粉红色点的向量位于蓝色区域上方,则它们需要顺时针旋转才能到达三角形;如果它们位于蓝色区域下方,则矢量需要逆时针移动。
要修正该算法,您需要计算向量{(V1-V2), (V1-P)}、{(V2-V3), (V2-P)}和的叉积{(V3-V1), (V3-P)}。看一下这篇文章的伪代码。
| 归档时间: |
|
| 查看次数: |
426 次 |
| 最近记录: |