我正在开发一个应用程序,该应用程序允许用户在特定区域中绘制,然后检查他绘制的形状是否相似。例如,以下两个形状应被视为相似(我无法检查确切的坐标,因为用户需要手工绘制,因此我需要灵活一些):
第一个形状的坐标在这里:link1,第二个形状的坐标在link2
因此,要精确检查形状是否相似,我想将它们分成10个微段(每个微段由其点的1/10组成)并查看变化。这是代码:
private boolean checkShape (int z, ArrayList<Pair<Float,Float>> points) {
ArrayList<Pair<Float,Float>> copia = segments.get(z);
int nGroupsFirstShape = (segments.get(z).size()*10)/100;
int nValuesFirstShape[] = new int[10];
for (int j=0, j2=0; j<10; j++, j2+=nGroupsFirstShape) {
int sumValues=0;
sumValues+=copia.get(j2).first-copia.get(j2+nGroupsFirstShape-1).first;
sumValues+=copia.get(j2).second-copia.get(j2+nGroupsFirstShape-1).second;
nValuesFirstShape[j] = sumValues;
}
ArrayList<Pair<Float,Float>> copia2 = points;
int nGroupSecondShape = (copia2.size()*10)/100;
int nValuesSecondShape[] = new int[10];
for (int j=0, j2=0; j<10; j++, j2+=nGroupSecondShape) {
int sumValues=0;
sumValues+=copia2.get(j2).first-copia2.get(j2+nGroupSecondShape-1).first;
sumValues+=copia2.get(j2).second-copia2.get(j2+nGroupSecondShape-1).second;
nValuesSecondShape[j] = sumValues;
}
int differences[] = new int[10];
int numberOf = 0;
for (int index=0; index<10; index++) {
differences[index] = nValuesFirstShape[index] - nValuesSecondShape[index];
if (differences[index]<0) differences[index] = -differences[index];
if (differences[index]<nGroupsFirstShape*3.5) numberOf++;
}
if (numberOf>=6) return true; else return false;
}
copia 是对应于第一个形状的ArrayList,而 copia2 是第二个形状。我要做的第一件事是计算形状的10%,所以我知道微组的大小,然后将X和Y坐标之间的差求和(以查看线段如何在绘制中移动)。该组并将其保存在一个数组中,我将用它来计算形状之间的差异。然后,一旦计算出该值,便检查两个形状之间的差异是否小于nGroupsFirstShape * 3.5(为保持灵活性而保持不变的常数)。通过这种方法,它可以工作,但是我不知道它是否可以应用于所有几何形式。这是我第一次打印的结果:
First Shape: 5
First Shape: 11
First Shape: 6
First Shape: 4
First Shape: 3
First Shape: 4
First Shape: -23
First Shape: -15
First Shape: -13
First Shape: -8
第二个是:
Second Shape: 1
Second Shape: 7
Second Shape: 8
Second Shape: 6
Second Shape: 6
Second Shape: 0
Second Shape: -4
Second Shape: -29
Second Shape: -19
Second Shape: -15
然后,这些是区别:
DIFF: 4
DIFF: 4
DIFF: 2
DIFF: 2
DIFF: 3
DIFF: 4
DIFF: 19
DIFF: 14
DIFF: 6
DIFF: 7
由于第一个形状已被划分为4个元素的微型组,因此值小于3.5 * 4的差异数为8,可以将其视为相似。您认为该算法可以应用于每种几何形式吗? PS:对不起,我的英语,我是意大利语