2010-08-17 100 views
0

在C++中使用Openframeworks库时,我有一个发光半径(max_distance),它是由在屏幕上拖动鼠标(mouseDragX)来确定的。它工作正常。如何添加/减去值而不仅仅是该值

但我不是每次调整它的大小(通过拖动鼠标),我都希望它不要从0开始,并直接按住鼠标拖动。

max_distance = mouseDragX/2; 

反倒是,如果我已经拖鼠标说200之前的阻力,下一次我拖动鼠标,并进入相反的方向(负数)的权值的max_distance减少,而不仅仅是那个数量。

我认为这将是

max_distance += mouseDragX/2; 

,但似乎要杀死它完全

你能帮助我吗?

#include "testApp.h" 


//-------------------------------------------------------------- 
void testApp::setup(){ 

ofSetWindowShape(700,700); 
max_distance = 700; // ofDist didn't work(?) // ofDist(0,0,700,700); 
ofEnableSmoothing(); 
ofEnableAlphaBlending(); 


} 

//-------------------------------------------------------------- 
void testApp::update(){ 
max_distance = mouseDragX/2; 
if (max_distance < 0) max_distance = 0; 
} 

//-------------------------------------------------------------- 
void testApp::draw(){ 


string str = "mouseDragX: "; 
str += ofToString(mouseDragX)+" "; 
ofSetWindowTitle(str); 

int i,j; 
int height = ofGetHeight(); 
int width = ofGetWidth(); 

for(i = 0; i <= height; i += 20) { 
    for(j = 0; j <= width; j += 20) { 
     float dist_color = getDistance(mouseX, mouseY, i, j); // for definition of getDistance, look below! 
     dist_color = dist_color/max_distance * 100; 

    // to get the colors into the range between 0 and 255, multiply the values by 5. 
    ofSetColor(dist_color*5,dist_color*5,dist_color*5, 123); 
    ofEllipse(i, j, 20, 20); 
    } 
    } 


} 

//-------------------------------------------------------------- 
void testApp::keyPressed (int key){ 

} 

//-------------------------------------------------------------- 
void testApp::keyReleased (int key){ 

} 

//-------------------------------------------------------------- 
void testApp::mouseMoved(int x, int y){ 
// shift values down 
for (int i = 0; i < 1; /*<<- length of array*/ i++) { 
pmouseX[i] = pmouseX[i+1]; 
pmouseY[i] = pmouseY[i+1]; 
} 
// make pmouseX/Y[0] be the previous mouse position. [1] = current 
pmouseX[1] = mouseX; 
pmouseY[1] = mouseY; 

} 

//-------------------------------------------------------------- 
void testApp::mouseDragged(int x, int y, int button){ 

mouseDragX = (mouseX - pmouseX[0]); 

} 

//-------------------------------------------------------------- 
void testApp::mousePressed(int x, int y, int button){ 
// mouseDragX = mouseDragY = 0; // The drag starts here 
} 

//-------------------------------------------------------------- 
void testApp::mouseReleased(){ 

} 

float testApp::getDistance(int startX, int startY, int endX, int endY){ 
return sqrt((endX-startX)*(endX-startX) + (endY-startY)*(endY-startY)); 
} 

非常感谢。

回答

0

如果我理解正确,你想要这样的东西。

// Every time the mouse *stops* moving, (say on mouse-up 
// message) save previous max_distance 
int base = max_distance; 

// when mouse moves 
max_distance = base + mouseDragX/2; 
+0

,它保留了更近一步的内存,但引入了各种古怪的行为,这可能是正确的方式,但我只是不能解决为什么它不能正常工作,特别是因为代码是如此简单:( 我现在正式放弃。 – 2010-08-22 10:04:11

0

如果max_distancemouseDragXint的值,则除以2得出可导致损失的整数除法。

如果mouseDragX的值在某个时刻为1,则尤其如此。这将导致1/2(整数除法)并返回0

实施例:

让我们考虑的是mouseDragX需要3个不同的值(3个循环):

3, 1, -4 

人们会期望max_distance将增加(3/2) + (1/2) - (4/2) = 0

但由于整数截断,这将导致1 + 0 - 2 = -1事实上的结果。

如果使用float!而非int,只是轮max_distance为int当你真正需要它的价值是什么?

+0

这是很好的建议。 实际上,它在视觉上似乎没有明显的差别。但即使遵循你的建议,我仍然不能使max_distance累积起来,而不是仅仅被mouseDragX覆盖:( – 2010-08-17 12:50:13

相关问题