calculate new width and height keeping aspect ratio

I'm struggling with making a sketch resizable with keeping the aspect ration. I'm almost there but i'm not really a genius in math. Could someone help me with the last bits?

main:

Resizer resizer;

int testRectangleW = 400;
int testRectangleH = 400;

void setup() {
  size(400, 400, OPENGL);

  resizer = new Resizer(this);
  resizer.forceAspectRatio(true);
}

void draw() {

  background(0);

  line(0, 0, width, height);

  float fitScaleValue = resizer.getFitScaleValue(width, height, testRectangleW, testRectangleH);

  rect(0, 0, testRectangleW*fitScaleValue, testRectangleH*fitScaleValue);

}

class Resizer:

public class Resizer {

  PApplet p;
  java.awt.Insets insets;

  boolean enabled = true;

  boolean forceAspectRatio = false;

  public Resizer(PApplet p) {
    this.p = p;
    p.registerMethod("pre", this);
    p.frame.setResizable(true);
  }

  // . . . . . . . . . . . . . . . . . . . . . .

  public void pre() {
    if (enabled) {

      // we have to update in case the frame has changed
      insets = p.frame.getInsets();

      // is there any OS that has insets on another side?
      // atleast it's future proof now 
      if (p.width != p.frame.getWidth()-(insets.left+insets.right) || p.height !=  p.frame.getHeight()-(insets.top+insets.bottom)) {
        String renderer = g.getClass().getName();
        int reqWidth = p.frame.getWidth()-(insets.left+insets.right);
        int reqHeight = p.frame.getHeight()-(insets.top+insets.bottom);

        if (!forceAspectRatio) {
          size(reqWidth, reqHeight, renderer);
        }
        else { //forceAspectRatio
          //float aspectRatio = (float) p.height / p.width;
          //float newAspectRatio = reqHeight/reqWidth;
          //println(aspectRatio, newAspectRatio);

          float s = getFitScaleValue(reqWidth, reqHeight, p.width, p.height); 
          frame.setSize((int)(s*p.width), (int)(s*p.height));

        }
      }
    }
  }

  // . . . . . . . . . . . . . . . . . . . . . .

  public Resizer enable(boolean b) {
    enabled = b;
    return this;
  }  

  // . . . . . . . . . . . . . . . . . . . . . .

  public boolean enabled(boolean b) {
    return enabled;
  }  


  // . . . . . . . . . . . . . . . . . . . . . .

  public Resizer forceAspectRatio(boolean b) {
    forceAspectRatio = b;
    return this;
  }

  // . . . . . . . . . . . . . . . . . . . . . .

  public boolean forceAspectRatio() {
    return forceAspectRatio;
  }

  // . . . . . . . . . . . . . . . . . . . . . .

  // return a normalized value to scale the original content to
  public float getFitScaleValue(float containerW, float containerH, float contentW, float contentH) {
    float containerProportion = containerW / containerH;
    float contentProportion = contentW / contentH;

    if (contentProportion >= containerProportion) { //container landscape (or square)
      return containerW / contentW;
    }
    else {
      return containerH / contentH;
    }
  }

  // . . . . . . . . . . . . . . . . . . . . . .

}
Tagged:
Sign In or Register to comment.