Android比较点

时间:2015-07-26 12:49:56

标签: javascript android arraylist coordinates point

目前我正在为Android制作应用程序,我偶然发现了一个问题 - 我有12分(A, B, C, D, E, F, G, Q, N, L, M and K)。我需要做一些有6个最高分的东西。

为了进行计算,我写了这段代码:

List<Integer> YPoint = new java.util.ArrayList(Arrays.asList(A.y, B.y, C.y, D.y, E.y, F.y, G.y, K.y, Q.y, L.y, M.y, N.y));
List<Integer> YUpper = new java.util.ArrayList(Arrays.asList());

int Classified = 0;
int Highest = 0;


while(Classified != 6){
    Highest = Collections.min(YPoint);
    YPoint.remove(Integer.valueOf(Highest));
    YUpper.add(Integer.valueOf(Highest));
    Classified++;
}

android.graphics.Point Highest1 = new android.graphics.Point();
android.graphics.Point Highest2 = new android.graphics.Point();
android.graphics.Point Highest3 = new android.graphics.Point();
android.graphics.Point Highest4 = new android.graphics.Point();
android.graphics.Point Highest5 = new android.graphics.Point();
android.graphics.Point Highest6 = new android.graphics.Point();

boolean notdone = false;
if(YUpper.contains(A.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = A; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = A; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = A; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = A; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = A; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = A; notdone = true;}
    notdone = false;
}
if(YUpper.contains(B.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = B; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = B; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = B; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = B; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = B; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = B; notdone = true;}
    notdone = false;
}
if(YUpper.contains(C.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = C; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = C; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = C; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = C; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = C; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = C; notdone = true;}
    notdone = false;
}
if(YUpper.contains(E.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = E; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = E; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = E; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = E; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = E; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = E; notdone = true;}
    notdone = false;
}
if(YUpper.contains(D.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = D; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = D; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = D; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = D; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = D; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = D; notdone = true;}
    notdone = false;
}
if(YUpper.contains(F.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = F; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = F; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = F; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = F; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = F; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = F; notdone = true;}
    notdone = false;
}
if(YUpper.contains(G.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = G; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = G; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = G; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = G; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = G; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = G; notdone = true;}
    notdone = false;
}
if(YUpper.contains(Q.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = Q; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = Q; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = Q; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = Q; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = Q; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = Q; notdone = true;}
    notdone = false;
}
if(YUpper.contains(K.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = K; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = K; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = K; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = K; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = K; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = K; notdone = true;}
    notdone = false;
}
if(YUpper.contains(N.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = N; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = N; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = N; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = N; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = N; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = N; notdone = true;}
    notdone = false;
}
if(YUpper.contains(L.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = L; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = L; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = L; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = L; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = L; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = L; notdone = true;}
    notdone = false;
}
if(YUpper.contains(M.y)){
    if(Highest1.x == 0 && !notdone){Highest1 = M; notdone = true;}
    if(Highest2.x == 0 && !notdone){Highest2 = M; notdone = true;}
    if(Highest3.x == 0 && !notdone){Highest3 = M; notdone = true;}
    if(Highest4.x == 0 && !notdone){Highest4 = M; notdone = true;}
    if(Highest5.x == 0 && !notdone){Highest5 = M; notdone = true;}
    if(Highest6.x == 0 && !notdone){Highest6 = M; notdone = true;}
    notdone = false;
}

YPoint.remove(Integer.valueOf(Highest1.y));
YPoint.remove(Integer.valueOf(Highest2.y));
YPoint.remove(Integer.valueOf(Highest3.y));
YPoint.remove(Integer.valueOf(Highest4.y));
YPoint.remove(Integer.valueOf(Highest5.y));
YPoint.remove(Integer.valueOf(Highest6.y));

简单地说,我的代码遵循这个算法:

  1. 列出12个点Y坐标
  2. 从列表中获取6 intY坐标(对应最高点)
  3. 找到与2
  4. 中的6个整数对应的点
  5. 制作新的6个点并将其坐标设置为3个点 - 我重复点,以便我可以在进一步计算中使用它们。
  6. 上面的代码有效,但它很庞大而且很混乱。这有什么好的和干净的选择吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我现在无法提供代码,但您需要的算法类似于:

  1. 将您的12个点放入SortedSet(其他类也可以使用),使用Comparator将它们排在最低的y之前。
  2. 首先在SortedSet中弹出6并将它们存储到列表中。
  3. 查看API的文档,它们非常简单。