// is rotated by.
double angleCD = indicatedAngle - expectedAngle ; // this is the rotation angle to be done
logger.debug("angle C-D " + angleCD);
Point center = new Point(centerX/2,centerY/2); // This is the center point of the four board used for rotation
double dxAB = 0, dxCD = 0, dxMP = 0;
double dyAB = 0, dyCD = 0, dyMP = 0;
// Now we do binary search n-times between AngleAB and AngleCD to find lowest error
// This is up to as good as we want, I prefer for-loop than while-loop.
for(int i = 0; i< 50;++i)
{
// use angleAB to calculate the displacement necessary to get placementLocation to boardLocation.
// Each point will have slightly different value.
// Then we can tell the error resulted from using this angleAB.
Point A = new Point(placementLocationA.getX(),placementLocationA.getY());
A = Utils2D.rotateTranslateCenterPoint(A, angleAB,0,0,center);
Point B = new Point(placementLocationB.getX(),placementLocationB.getY());
B = Utils2D.rotateTranslateCenterPoint(B, angleAB,0,0,center);
Point C = new Point(placementLocationC.getX(),placementLocationC.getY());
C = Utils2D.rotateTranslateCenterPoint(C, angleAB,0,0,center);
Point D = new Point(placementLocationD.getX(),placementLocationD.getY());
D = Utils2D.rotateTranslateCenterPoint(D, angleAB,0,0,center);
double dA = (boardLocationA.getX() - A.getX());
double dB = (boardLocationB.getX() - B.getX());
double dC = (boardLocationC.getX() - C.getX());
double dD = (boardLocationD.getX() - D.getX());
// Take the average of the four
dxAB = (dA + dB + dC + dD)/4;
double errorAB = Math.abs(dxAB- dA) + Math.abs(dxAB- dB) + Math.abs(dxAB- dC) + Math.abs(dxAB- dD);
dA = (boardLocationA.getY() - A.getY());
dB = (boardLocationB.getY() - B.getY());
dC = (boardLocationC.getY() - C.getY());
dD = (boardLocationD.getY() - D.getY());
// Take the average of the four
dyAB = (dA + dB + dC + dD)/4;
errorAB += Math.abs(dyAB- dA) + Math.abs(dyAB- dB) + Math.abs(dyAB- dC) + Math.abs(dyAB- dD); // Accumulate the error
// Now do the same using angleCD, find the error caused by angleCD
A = new Point(placementLocationA.getX(),placementLocationA.getY());
A = Utils2D.rotateTranslateCenterPoint(A, angleCD,0,0,center);
B = new Point(placementLocationB.getX(),placementLocationB.getY());
B = Utils2D.rotateTranslateCenterPoint(B, angleCD,0,0,center);
C = new Point(placementLocationC.getX(),placementLocationC.getY());
C = Utils2D.rotateTranslateCenterPoint(C, angleCD,0,0,center);
D = new Point(placementLocationD.getX(),placementLocationD.getY());
D = Utils2D.rotateTranslateCenterPoint(D, angleCD,0,0,center);
dA = (boardLocationA.getX() - A.getX());
dB = (boardLocationB.getX() - B.getX());
dC = (boardLocationC.getX() - C.getX());
dD = (boardLocationD.getX() - D.getX());
// Take the average of the four
dxCD = (dA + dB + dC + dD)/4;
double errorCD = Math.abs(dxCD- dA) + Math.abs(dxCD- dB) + Math.abs(dxCD- dC) + Math.abs(dxCD- dD);
dA = (boardLocationA.getY() - A.getY());
dB = (boardLocationB.getY() - B.getY());
dC = (boardLocationC.getY() - C.getY());
dD = (boardLocationD.getY() - D.getY());
// Take the average of the four
dyCD = (dA + dB + dC + dD)/4;
errorCD += Math.abs(dyCD- dA) + Math.abs(dyCD- dB) + Math.abs(dyCD- dC) + Math.abs(dyCD- dD); // Accumulate the error
// Now take the mid-point between the two angles,
// and do the same math
double angleMP = (angleAB + angleCD)/2; // MP = mid-point angle between angleAB and angleCD
double deltaAngle = Math.abs(angleAB - angleCD);
A = new Point(placementLocationA.getX(),placementLocationA.getY());
A = Utils2D.rotateTranslateCenterPoint(A, angleMP,0,0,center);
B = new Point(placementLocationB.getX(),placementLocationB.getY());
B = Utils2D.rotateTranslateCenterPoint(B, angleMP,0,0,center);
C = new Point(placementLocationC.getX(),placementLocationC.getY());
C = Utils2D.rotateTranslateCenterPoint(C, angleMP,0,0,center);
D = new Point(placementLocationD.getX(),placementLocationD.getY());
D = Utils2D.rotateTranslateCenterPoint(D, angleMP,0,0,center);
dA = (boardLocationA.getX() - A.getX());
dB = (boardLocationB.getX() - B.getX());
dC = (boardLocationC.getX() - C.getX());
dD = (boardLocationD.getX() - D.getX());
// Take the average of the four
dxMP = (dA + dB + dC + dD)/4;
double errorMP = Math.abs(dxMP- dA) + Math.abs(dxMP- dB) + Math.abs(dxMP- dC) + Math.abs(dxMP- dD);
dA = (boardLocationA.getY() - A.getY());
dB = (boardLocationB.getY() - B.getY());
dC = (boardLocationC.getY() - C.getY());
dD = (boardLocationD.getY() - D.getY());
// Take the average of the four
dyMP = (dA + dB + dC + dD)/4;
errorMP += Math.abs(dyMP- dA) + Math.abs(dyMP- dB) + Math.abs(dyMP- dC) + Math.abs(dyMP- dD); // Accumulate the error