diff --git a/src/ofApp.cpp b/src/ofApp.cpp index 027a8f2..6d23d9c 100644 --- a/src/ofApp.cpp +++ b/src/ofApp.cpp @@ -47,7 +47,9 @@ void ofApp::draw(){ ofSetColor(color); ofDrawRectangle(webcam.getWidth(), 0, 128, 128); if (color_picked == 1 && key_pressed == 0) { - SetCursorPos((center.x - x_offset) * x_scale, (center.y - y_offset) * y_scale); + scaleTrapeze(); + scaleRectangel(); + SetCursorPos(cp_cursor_position.x, cp_cursor_position.y); } } @@ -77,9 +79,10 @@ void ofApp::mousePressed(int x, int y, int button) { if (calibrated == 4) { color = webcam.getPixels().getColor(x, y); printf("%i, %i, %i\n", color.r, color.g, color.b); + printf("%f, %f, %f, %f \n", y_offset, y_scale, x_offset, x_scale); color_picked = 1; } - if (calibrated != 4) { + if (calibrated != 4 && key_pressed == 0) { if (calibrated == 0) { GetCursorPos(&cp_top_left); } @@ -91,14 +94,15 @@ void ofApp::mousePressed(int x, int y, int button) { } if (calibrated == 3) { GetCursorPos(&cp_bottom_left); - y_offset = (cp_top_left.y + cp_top_right.y) / 2.0; - - y_scale = 1080 / ((cp_bottom_left.y + cp_bottom_right.y) / 2.0); x_offset = (cp_top_left.x + cp_bottom_left.x) / 2.0; - x_scale = 1920 / ((cp_top_right.x + cp_bottom_right.x) / 2.0); + x_scale = ofGetScreenWidth() / (cp_bottom_right.x - x_offset); + y_offset = (cp_top_left.y + cp_top_right.y) / 2.0; + + y_scale = ofGetScreenHeight() / ((cp_bottom_left.y + cp_bottom_right.y) / 2.0 - y_offset); + } calibrated++; } @@ -133,3 +137,22 @@ void ofApp::gotMessage(ofMessage msg){ void ofApp::dragEvent(ofDragInfo dragInfo){ } + + +void ofApp::scaleTrapeze(void) { + left_pitch = (cp_top_left.y - cp_bottom_left.y) / (cp_top_left.x - cp_bottom_left.x); + right_pitch = (cp_top_right.y - cp_bottom_right.y) / (cp_top_right.x - cp_bottom_right.x); + middle = cp_top_right.x - cp_top_left.x; + + if (center.x > middle) { + center.x = center.x + (((center.y - cp_top_right.y) / right_pitch) * ((cp_bottom_right.x - middle) - ((center.y - cp_top_right.y) / right_pitch)) / (center.x - middle)); + } + else { + center.x = center.x + (((center.y - cp_top_left.y) / left_pitch) * ((middle - cp_bottom_left.x) - ((center.y - cp_top_left.y) / left_pitch)) / (middle - center.x)); + } +} + +void ofApp::scaleRectangel(void) { + cp_cursor_position.x = center.x * x_scale - x_offset; + cp_cursor_position.y = center.y * y_scale - y_offset; +} \ No newline at end of file diff --git a/src/ofApp.h b/src/ofApp.h index 03624ca..8856a9f 100644 --- a/src/ofApp.h +++ b/src/ofApp.h @@ -23,6 +23,8 @@ class ofApp : public ofBaseApp{ void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); + void scaleRectangel(void); + void scaleTrapeze(void); ofVideoGrabber webcam; ofxCv::ContourFinder contour; @@ -39,8 +41,11 @@ class ofApp : public ofBaseApp{ float y_offset; float x_scale; float x_offset; + float left_pitch; + float right_pitch; + float middle; - POINT cp_top_left, cp_top_right, cp_bottom_left, cp_bottom_right; + POINT cp_top_left, cp_top_right, cp_bottom_left, cp_bottom_right, cp_cursor_position; };