If they have the max number of corners (10), the code is going to go out bounds on the array on the last loop.
1 2
|
for (int i = 0; i < numberOfcorners; i++)
distanceCorners[i] = sqrt(powf((ArrayX[i + 1] - ArrayX[i]), 2) + powf((ArrayY[i + 1] - ArrayY[i]), 2));
|
//just cut out some of the code here just to simplify for illustration purposes
distanceCorners[0] = (ArrayX[1] - ArrayX[0])(ArrayY[1] - ArrayY[0])
distanceCorners[1] = (ArrayX[2] - ArrayX[1])(ArrayY[2] - ArrayY[1])
distanceCorners[2] = (ArrayX[3] - ArrayX[2])(ArrayY[3] - ArrayY[2])
distanceCorners[3] = (ArrayX[4] - ArrayX[3])(ArrayY[4] - ArrayY[3])
distanceCorners[4] = (ArrayX[5] - ArrayX[4])(ArrayY[5] - ArrayY[4])
distanceCorners[5] = (ArrayX[6] - ArrayX[5])(ArrayY[6] - ArrayY[5])
distanceCorners[6] = (ArrayX[7] - ArrayX[6])(ArrayY[7] - ArrayY[6])
distanceCorners[7] = (ArrayX[8] - ArrayX[7])(ArrayY[8] - ArrayY[7])
distanceCorners[8] = (ArrayX[9] - ArrayX[8])(ArrayY[9] - ArrayY[8])
distanceCorners[9] = (
ArrayX[10] - ArrayX[9])(
ArrayY[10] - ArrayY[9])
In any case, that last iteration should be the distance between the last point and the first point?
distanceCorners[9] = (
ArrayX[9] - ArrayX[0])(
ArrayY[9] - ArrayY[0]
or
distanceCorners[numberOfcorners-1] = (
ArrayX[numberOfcorners-1] - ArrayX[0])(
ArrayY[numberOfcorners-1] - ArrayY[0]
It's sort of a special case. You could check the value of the index in the for loop to check for this case, or pull it out of the loop and run the loop one fewer times. You just can't use the loop variable (e.g. i) outside of the loop.