I want to place something in a frame as big as possible but it should keep it's proportion.
Could someone help, i have spend like 45 minutes on the math and it seems not to make any sense...
- float containerW = 200;
- float containerH = 100;
- float contentW, contentH;
- float[] newWH;
- void setup() {
- size(400, 400);
- stroke(0);
- noFill();
- }
- void draw() {
- background(255);
- strokeWeight(1.0);
- contentW = mouseX;
- contentH = mouseY;
- rect(0, 0, containerW, containerH);
- fill(255, 0, 0, 50);
- rect(0, 0, mouseX, mouseY);
- noFill();
- newWH = calculateFitSize(containerW, containerH, contentW, contentH);
- strokeWeight(3.0);
- rect(0, 0, newWH[0], newWH[1]);
- }
- float[] calculateFitSize(float containerW, float containerH, float contentW, float contentH) {
- // returnFloat[] = new float[2];
- float containerProportion = containerW / containerH;
- float contentProportion = contentW / contentH;
- println("containerProportion "+containerProportion);
- println("contentProportion "+contentProportion);
- //
- if (containerProportion >= 1 && contentProportion >= containerProportion) { //container landscape (or square)
- return new float[] { containerW, (contentH / 100) * (containerW / (contentW / 100)) };
- }
- else if(containerProportion >= 1 && contentProportion <= containerProportion) {
- return new float[] { containerH, (contentW / 100) * (containerH / (contentH / 100)) };
- }
- else { // potrait
- //return new float[] { containerH, contentW * (containerH / (contentH / 100)) };
- return new float[] { 10, 10 };
- }
- }
1