我找到了一个很好的解决方案,适合我的问题。
首先,我尝试检测Rectangle的左上角和右下角。 为此,我从坐标系的源计算二维Vector的长度。 4个向量中最短的是左上角,最大的是右下角。 我尝试通过对两个已知点的简单坐标检查来检测其他两个点。 为避免出现小错误,右上角与左下角通过相同的检查,我通过x坐标对点进行排序。
这是代码:
private PointF[] checkPointLocation(PointF[] points){
double minLength = 100000000;
double maxLength = 0;
int minPos = 0;
int maxPos = 0;
for(int i = 0; i < points.length; i++){
double vLength = Math.abs(Math.sqrt((points[i].x * points[i].x) + (points[i].y * points[i].y)));
if(vLength < minLength) {
minLength = vLength;
minPos = i;
}
if(vLength > maxLength) {
maxLength = vLength;
maxPos = i;
}
}
PointF topLeft = points[minPos];
PointF bottomRight = points[maxPos];
Log.d(TAG, "TopLeft: " + topLeft);
Log.d(TAG, "BottomRight: " + bottomRight);
PointF topRight = null;
PointF bottomLeft = null;
Arrays.sort(points, (o1, o2) -> Float.compare(o1.x, o2.x));
for(int i = 0; i < points.length; i++){
PointF p = points[i];
Log.d(TAG, "Point: " + p);
if( p.equals(topLeft) || p.equals(bottomRight))
continue;
if(bottomLeft == null && p.x < bottomRight.x && p.y > topLeft.y)
bottomLeft = p;
if(topRight == null && p.x > topLeft.x && p.y < bottomRight.y)
topRight = p;
}
if(topRight == null){
throw new NullPointerException("topRight is null");
}
if(bottomLeft == null){
throw new NullPointerException("BottomLeft is null");
}
Log.d(TAG, "BottomLeft = " + bottomLeft);
Log.d(TAG, "TopRight = " + topRight);
PointF[] ccwPoints = {topLeft, bottomLeft, bottomRight, topRight};
return ccwPoints;
}
就我而言,此解决方案适用于所有可能的用户输入。