Skip to content

Instantly share code, notes, and snippets.

@bakercp
Created August 27, 2012 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakercp/3492711 to your computer and use it in GitHub Desktop.
Save bakercp/3492711 to your computer and use it in GitHub Desktop.
Test of ofRectangle::ScaleToMe() method.
#include "ofMain.h"
#include "testApp.h"
#include "ofAppGlutWindow.h"
//========================================================================
int main( ){
ofAppGlutWindow window;
ofSetupOpenGL(&window, 1024,768, OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp( new testApp());
}
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
subjectWidth = 80;
subjectHeight = 60;
currentScaleMode = OF_RECTSCALEMODE_FIT;
float height_4 = ofGetHeight() / 4.0f;
float width_4 = ofGetWidth() / 4.0f;
target_0.setFromCenter(width_4, height_4 ,300,100);
target_1.setFromCenter(width_4 * 3, height_4 ,100,200);
target_2.setFromCenter(width_4, height_4 * 3 ,200,200);
target_3.setFromCenter(width_4 * 3, height_4 * 3 ,100,300);
}
//--------------------------------------------------------------
void testApp::update(){
subject_0.setFromCenter(ofGetMouseX(), ofGetMouseY(), subjectWidth, subjectHeight);
result_0 = target_0.scaleToMe(subject_0,currentScaleMode);
result_1 = target_1.scaleToMe(subject_0,currentScaleMode);
result_2 = target_2.scaleToMe(subject_0,currentScaleMode);
result_3 = target_3.scaleToMe(subject_0,currentScaleMode);
}
//--------------------------------------------------------------
void testApp::draw(){
ofBackground(0);
ofEnableAlphaBlending();
ofNoFill();
ofSetColor(0,255,0,127);
ofRect(subject_0);
ofFill();
ofSetColor(255,0,0,127);
ofRect(target_0);
ofRect(target_1);
ofRect(target_2);
ofRect(target_3);
ofFill();
ofSetColor(0,0,255,127);
ofRect(result_0);
ofRect(result_1);
ofRect(result_2);
ofRect(result_3);
string scaleModeString = "";
switch (currentScaleMode) {
case OF_RECTSCALEMODE_FIT:
scaleModeString = "OF_RECTSCALEMODE_FIT";
break;
case OF_RECTSCALEMODE_FILL:
scaleModeString = "OF_RECTSCALEMODE_FILL";
break;
case OF_RECTSCALEMODE_CENTER:
scaleModeString = "OF_RECTSCALEMODE_CENTER";
break;
case OF_RECTSCALEMODE_STRETCH:
scaleModeString = "OF_RECTSCALEMODE_STRETCH";
break;
default:
scaleModeString = "OF_RECTSCALEMODE_FIT";
break;
}
ofSetColor(255);
ofDrawBitmapString("Presse (Spacebar) to Set Current Scale Mode : " + scaleModeString, 10, ofGetHeight() - 10);
ofDisableAlphaBlending();
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
if(key == ' ') {
currentScaleMode = (ofRectScaleMode)(( (int)currentScaleMode + 1 ) % 4);
} else if(key == 'r') {
subjectWidth = 80 + ofRandom(-160,160);
subjectHeight = 60 + ofRandom(-120,120);
}
}
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
ofRectScaleMode currentScaleMode;
float subjectWidth;
float subjectHeight;
ofRectangle subject_0;
ofRectangle target_0;
ofRectangle result_0;
ofRectangle target_1;
ofRectangle result_1;
ofRectangle target_2;
ofRectangle result_2;
ofRectangle target_3;
ofRectangle result_3;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment