tags:

views:

152

answers:

2

Hello!

Using the Openframeworks library in C++, I have the radius of a glow (max_distance) that is determined by the stretch of the mouse dragging across the screen (mouseDragX). It works fine.

But rather than every time I resize it (by dragging the mouse), I want it not to start at 0 and follow the mouse drag directly.

max_distance = mouseDragX/2;

But rather, if I have already dragged the mouse to the right to say 200 on a previous drag, that the next time I drag the mouse, and go into the opposite direction (negative numbers) that the value of max_distance decreases by that amount, instead of just being that amount.

I thought it would be

max_distance += mouseDragX/2;

but that seems to kill it altogether

Can you help me?

#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));
}

Thank you so much.

A: 

If max_distance and mouseDragX are int values, the division by 2 results in an integer division that can induce losses.

This is especially true if mouseDragX value's is 1 at some time. This will result in 1 / 2 (integer division) and returns 0.

Example:

Lets consider that mouseDragX takes 3 different values (3 cycles):

3, 1, -4

One would expect that max_distance will be increased by (3 / 2) + (1 / 2) - (4 / 2) = 0.

But due to integer truncation, this will infact result to 1 + 0 - 2 = -1.

What if you use floats instead of int, and just round max_distance to an int when you really need it's value ?

ereOn
That's great advice.In practice, visually it seems to make no discernible difference. But even after following your advice I still cant make max_distance accumulative rather than just overwritten by mouseDragX :(
Arif Driessen
A: 

If I understand correctly, you want todo something like this.

// 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;
Michael J
that retains the memory which is one step closer, but introduces all sorts of quirky behavior. It's probably the right way to go, but I just can't work out why it's not working properly. especially since the code is so simple. :(I now officially give up.
Arif Driessen