を私は私の問題のために合う良い解決策を見つけました。
まず、四角形の左上隅と右下隅を検出しようとします。そのためには、座標系のソースから2次元ベクトルの長さを計算します。 4つのベクトルの中で最短のものが左上隅、最も大きいものが右下隅です。他の2つの点は、2つの既知の点を使って簡単な座標チェックで検出しようとします。右上隅が左下と同じチェックに来る小さなバグを避けるために、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;
}
、このソリューションは、すべての可能なユーザー入力に正しかったです。
ポイント間の線を「y(x)= m * x + b」という形式の多項式関数と考えてください。 '' m''と '' b''は '' PointF''の座標で計算できます。それはあなたを始めるはずです。 – f1sh
私はあなたのことを正しく理解しているかどうか分かりませんが、私はあなたが「b」と言っていると思います。例えば、ポイントが上隅か下隅かを判断できますか?そして、「m」を使って、それが左か右であるかどうかを判断することができます。もしそうなら、ポイントが右角であればどのような価値がありますか? –