double computeSideError ( List < Point2D_I32 > contour , int indexA , int indexB ) { assignLine ( contour , indexA , indexB , line ) ; // don't sample the end points because the error will be zero by definition int numSamples ; double sumOfDistances = 0 ; int length ; if ( indexB >= indexA ) { length = indexB - indexA - 1 ; numSamples = Math . min ( length , maxNumberOfSideSamples ) ; for ( int i = 0 ; i < numSamples ; i ++ ) { int index = indexA + 1 + length * i / numSamples ; Point2D_I32 p = contour . get ( index ) ; sumOfDistances += Distance2D_F64 . distanceSq ( line , p . x , p . y ) ; } sumOfDistances /= numSamples ; } else { length = contour . size ( ) - indexA - 1 + indexB ; numSamples = Math . min ( length , maxNumberOfSideSamples ) ; for ( int i = 0 ; i < numSamples ; i ++ ) { int where = length * i / numSamples ; int index = ( indexA + 1 + where ) % contour . size ( ) ; Point2D_I32 p = contour . get ( index ) ; sumOfDistances += Distance2D_F64 . distanceSq ( line , p . x , p . y ) ; } sumOfDistances /= numSamples ; } // handle divide by zero error if ( numSamples > 0 ) return sumOfDistances ; else return 0 ; }
Scores a side based on the sum of Euclidean distance squared of each point along the line . Euclidean squared is used because its fast to compute