Created
November 28, 2014 20:41
-
-
Save tiagosr/0b9f79e85c3cd88a7144 to your computer and use it in GitHub Desktop.
Parameterizable Rectangle Fitter for openFrameworks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ofxRectFit.h | |
// | |
// Created by Tiago on 11/28/14. | |
// | |
// | |
#ifndef __ofxRectFit__ | |
#define __ofxRectFit__ | |
#include "ofMain.h" | |
inline ofRectangle ofxRectFit(const ofRectangle& original, | |
const ofRectangle& container, | |
float fit, | |
float x, float y, bool inside) { | |
float original_ratio = original.width/original.height; | |
float container_ratio = container.width/container.height; | |
ofRectangle mapped(original), fitted; | |
if((container_ratio < original_ratio) ^ inside ) { | |
fitted.height = container.height; | |
fitted.width = container.height*original_ratio; | |
} else { | |
fitted.width = container.width; | |
fitted.height = container.width/original_ratio; | |
} | |
fitted.x = ofMap(x, 0, 1, container.x, container.x + container.width - (fitted.width)); | |
fitted.y = ofMap(y, 0, 1, container.y, container.y + container.height - (fitted.height)); | |
mapped.x = ofMap(x, 0, 1, container.x, container.x + container.width - mapped.width); | |
mapped.y = ofMap(y, 0, 1, container.y, container.y + container.height - mapped.height); | |
return ofRectangle( | |
ofMap(fit, 0, 1, mapped.x, fitted.x), | |
ofMap(fit, 0, 1, mapped.y, fitted.y), | |
ofMap(fit, 0, 1, mapped.width, fitted.width), | |
ofMap(fit, 0, 1, mapped.height, fitted.height) | |
); | |
} | |
#endif /* defined(__ofxRectFit__) */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment