I'm developing an app which lets the user draw in a specific area and then checks if the shapes he drawn are similar. For example, these are two shapes that should be considered similar (I cannot check the exactly coordinates because I need to be flexible since the users draw by hand):
The coordinates of the first shape are here: link1 and the coordinates of the second one are here link2
So, to check with precision if the shapes are similar, I thought to divide each on in 10 micro-segments (each of them composed by 1/10 of its points) and see the variation. This is the code:
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 is the ArrayList corresponding to the first shape, and copia2 the second one. The first thing I do, is calculating the 10% of the shape, so I know how big the micro-groups are, then I sum the difference between the X and the Y coordinates (to see how the segments moves through the draw) of that group and save it in an array which I will use to calculate the differences between the shapes. Then, once I calculated that, I check if the differences between the two shapes is less than nGroupsFirstShape*3.5 (a constant to keep a bit of flexibility). With this method it works, but I don't understand if it can be applied to all the geometric forms. This is the result of a print I made in the first for:
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
And this in the second for:
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
Then, these are the differences:
DIFF: 4
DIFF: 4
DIFF: 2
DIFF: 2
DIFF: 3
DIFF: 4
DIFF: 19
DIFF: 14
DIFF: 6
DIFF: 7
Since the first shape has been divide into micro-groups of 4 elements, the number of differences with a value less then 3.5*4 is 8 and it can be considered similar. Do you think that this algorithm can be applied to each geometric form? PS: sorry for my English, I'm Italian