public void adjustBeforeOptimize(Polygon2D_F64 polygon, GrowQueue_B touchesBorder, boolean clockwise) {
		int N = polygon.size();
		work.vertexes.resize(N);
		for (int i = 0; i < N; i++) {
			work.get(i).set(0, 0);
		}

		for (int i = N - 1, j = 0; j < N; i = j, j++) {
			int ii,jj,kk,mm;
			if( clockwise ) {
				mm = CircularIndex.addOffset(-1,i,N);
				ii=i;jj=j;
				kk = CircularIndex.addOffset(1,j,N);
			} else {
				mm = CircularIndex.addOffset(1,j,N);
				ii=j;jj=i;
				kk = CircularIndex.addOffset(-1,i,N);
			}

			Point2D_F64 a = polygon.get(ii), b = polygon.get(jj);

			double dx = b.x - a.x;
			double dy = b.y - a.y;

			double l = Math.sqrt(dx * dx + dy * dy);

			// The input polygon has two identical corners. This is bad. We will just skip over the first corner
			if( l == 0 ) {
				throw new RuntimeException("Input polygon has two identical corners. You need to fix that.");
			}

			dx *= 1.5 / l;
			dy *= 1.5 / l;

			Point2D_F64 _a = work.get(ii);
			Point2D_F64 _b = work.get(jj);

			// move the point away from the line
			if( touchesBorder.size>0 && touchesBorder.get(ii) ) {
				if(!touchesBorder.get(mm)) {
					_a.x -= dx;
					_a.y -= dy;
				}
			} else {
				_a.x += -dy;
				_a.y += dx;
			}
			if( touchesBorder.size>0 && touchesBorder.get(jj) ) {
				if( !touchesBorder.get(kk) ) {
					_b.x += dx;
					_b.y += dy;
				}
			} else {
				_b.x += -dy;
				_b.y += dx;
			}
		}

		for (int i = 0; i < N; i++) {
			Point2D_F64 a = polygon.get(i);
			Point2D_F64 b = work.get(i);
			a.x += b.x;
			a.y += b.y;
		}
	}
public void adjustBeforeOptimize ( Polygon2D_F64 polygon , GrowQueue_B touchesBorder , boolean clockwise ) { int N = polygon . size ( ) ; work . vertexes . resize ( N ) ; for ( int i = 0 ; i < N ; i ++ ) { work . get ( i ) . set ( 0 , 0 ) ; } for ( int i = N - 1 , j = 0 ; j < N ; i = j , j ++ ) { int ii , jj , kk , mm ; if ( clockwise ) { mm = CircularIndex . addOffset ( - 1 , i , N ) ; ii = i ; jj = j ; kk = CircularIndex . addOffset ( 1 , j , N ) ; } else { mm = CircularIndex . addOffset ( 1 , j , N ) ; ii = j ; jj = i ; kk = CircularIndex . addOffset ( - 1 , i , N ) ; } Point2D_F64 a = polygon . get ( ii ) , b = polygon . get ( jj ) ; double dx = b . x - a . x ; double dy = b . y - a . y ; double l = Math . sqrt ( dx * dx + dy * dy ) ; if ( l == 0 ) { throw new RuntimeException ( "Input polygon has two identical corners. You need to fix that." ) ; } dx *= 1.5 / l ; dy *= 1.5 / l ; Point2D_F64 _a = work . get ( ii ) ; Point2D_F64 _b = work . get ( jj ) ; if ( touchesBorder . size > 0 && touchesBorder . get ( ii ) ) { if ( ! touchesBorder . get ( mm ) ) { _a . x -= dx ; _a . y -= dy ; } } else { _a . x += - dy ; _a . y += dx ; } if ( touchesBorder . size > 0 && touchesBorder . get ( jj ) ) { if ( ! touchesBorder . get ( kk ) ) { _b . x += dx ; _b . y += dy ; } } else { _b . x += - dy ; _b . y += dx ; } } for ( int i = 0 ; i < N ; i ++ ) { Point2D_F64 a = polygon . get ( i ) ; Point2D_F64 b = work . get ( i ) ; a . x += b . x ; a . y += b . y ; } }
The polygon detected from the contour is too small because the binary image was eroded. This expand the size
of the polygon so that it fits the image edge better