Calculate largest rectangle in a rotated rectangle

后端 未结 8 2277
你的背包
你的背包 2020-11-28 03:20

I\'m trying to find the best way to calculate the biggest (in area) rectangle which can be contained inside a rotated rectangle.

Some pictures should help (I hope) i

8条回答
  •  借酒劲吻你
    2020-11-28 04:09

    enter image description here

    Here is the easiest way to do this... :)

    Step 1
    //Before Rotation
    
    int originalWidth = 640;
    int originalHeight = 480;
    
    Step 2
    //After Rotation
    int newWidth = 701;  //int newWidth = 654;  //int newWidth = 513;
    int newHeight = 564; //int newHeight = 757; //int newHeight = 664;
    
    Step 3
    //Difference in height and width
    int widthDiff ;
    int heightDiff;
    int ASPECT_RATIO = originalWidth/originalHeight; //Double check the Aspect Ratio
    
    if (newHeight > newWidth) {
    
        int ratioDiff = newHeight - newWidth;
        if (newWidth < Constant.camWidth) {
            widthDiff = (int) Math.floor(newWidth / ASPECT_RATIO);
            heightDiff = (int) Math.floor((originalHeight - (newHeight - originalHeight)) / ASPECT_RATIO);
        }
        else {
            widthDiff = (int) Math.floor((originalWidth - (newWidth - originalWidth) - ratioDiff) / ASPECT_RATIO);
            heightDiff = originalHeight - (newHeight - originalHeight);
        }
    
    } else {
        widthDiff = originalWidth - (originalWidth);
        heightDiff = originalHeight - (newHeight - originalHeight);
    }
    
    Step 4
    //Calculation
    int targetRectanleWidth = originalWidth - widthDiff;
    int targetRectanleHeight = originalHeight - heightDiff;
    
    Step 5
    int centerPointX = newWidth/2;
    int centerPointY = newHeight/2;
    
    Step 6
    int x1 = centerPointX - (targetRectanleWidth / 2); 
    int y1 = centerPointY - (targetRectanleHeight / 2);
    int x2 = centerPointX + (targetRectanleWidth / 2);
    int y2 = centerPointY + (targetRectanleHeight / 2);
    
    Step 7
    x1 = (x1 < 0 ? 0 : x1);
    y1 = (y1 < 0 ? 0 : y1);
    

提交回复
热议问题