Using OF Function in a Function

Hello,

I am trying to write a distance function that will compute the distance between a (x,y) coordinate and the mouse coordinates. But when I call mouseX or mouseY from within the function that I am writing I get the error “mouseX was not declared in the scope”

Is there any way to use a OF function from within a function?

Here is my code:

float distance(int n,int valX, int valY){
    float d;
    d = sqrt((valY-mouseY)*(valY-mouseY)+(valX-mouseX)(valX-mouseX));
    return d;
}

May it be

float ofApp::distance(int n,int valX, int valY)

instead of

float distance(int n,int valX, int valY)

you can also do ofGetMouseX() / ofGetMouseY() from anywhere to access the mouseX and mouseY values. (They are members of ofApp and not accessible elsewhere in your code).

1 Like

If I did this how would I call it in the draw section for instance?

Yes brilliant that will work for just what I want

Just like any regular call

void ofApp::draw() {
    float dist = distance(n, valx, valy);
}

float ofApp::distance(int n,int valX, int valY){
    float d;
    d = sqrt((valY-mouseY)*(valY-mouseY)+(valX-mouseX)(valX-mouseX));
    return d;
}

Produces this error:
error: no ‘float ofApp::distance(int, int, int)’ member function declared in class ‘ofApp’

Sorry I’m still learning…

you have to also define it in the h file of ofApp, ie:

class ofApp : public ofSimpleApp{
 .....
float distance(int n,int valX, int valY);
....
}

I see :smile:

in the ofApp.h

public:
float distance(int n,int valX, int valY);

in the ofApp.cpp

void ofApp::draw() {
    float dist = distance(1, 100, 200);
}

float ofApp::distance(int n, int valX, int valY) {
    float d;
    d = sqrt(((valY - mouseY) * (valY - mouseY)) + ((valX - mouseX) * (valX - mouseX)))
    return d;
}

instead of (1, 100, 200) you need to put your variables of course.

Okay I’m starting to see how this works but it still produces an error.

error: ‘(valX - ((ofApp*)this)->ofApp::.ofBaseApp::mouseX)’ cannot be used as a function

Can you share with us your ofApp.h and ofApp.cpp?

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{
public:
    float distance(int n,int valX, int valY);
	void setup();
	void update();
	void draw();

	void keyPressed(int key);
	void keyReleased(int key);
	void mouseMoved(int x, int y);
	void mouseDragged(int x, int y, int button);
	void mousePressed(int x, int y, int button);
	void mouseReleased(int x, int y, int button);
	void windowResized(int w, int h);
	void dragEvent(ofDragInfo dragInfo);
	void gotMessage(ofMessage msg);
};

#include "ofApp.h"

/*float distance(int valX, int valY){
float d,x,y;
x = ofGetMouseX();
y = ofGetMouseY();
d = sqrt((valY-y)*(valY-y)+(valX-x)*(valX-x));
return d;
}*/
float ofApp::distance(int n,int valX, int valY){
float d;
d = sqrt((valY-mouseY)*(valY-mouseY)+(valX-mouseX)(valX-mouseX));
return d;
}

int squareW = 100, squareH = 100;
int pieceR = 20;
int numSpaces = 8;
int blackLocX[12],blackLocY[12];
int redLocX[12],redLocY[12];



//--------------------------------------------------------------
void ofApp::setup(){
int i,j;
//startLoc
for(j = 0 ; j < 3 ; j++){
        if(j == 0){
            for(i = 0 ; i < 4 ; i++){
                blackLocX[i] = i*200+50;
                blackLocY[i] = 50;
        }
        }
        if(j == 1){
            for(i = 4 ; i < 8 ; i++){
                blackLocX[i] = (i-4)*200+150;
                blackLocY[i] = 150;
        }
        }
        if(j == 2){
            for(i = 8 ; i < 12 ; i++){
                blackLocX[i] = (i-8)*200+50;
                blackLocY[i] = 250;
        }
        }
}
for(j = 0 ; j < 3 ; j++){
        if(j == 0){
            for(i = 0 ; i < 4 ; i++){
                redLocX[i] = i*200+50;
                redLocY[i] = 550;
        }
        }
        if(j == 1){
            for(i = 4 ; i < 8 ; i++){
                redLocX[i] = (i-4)*200+150;
                redLocY[i] = 650;
        }
        }
        if(j == 2){
            for(i = 8 ; i < 12 ; i++){
                redLocX[i] = (i-8)*200+50;
                redLocY[i] = 750;
        }
        }
}


}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){
int i,j;
//Draw Checkers
for(j = 0 ; j < numSpaces ; j++){
    for(i = 0 ; i < numSpaces ; i++){
        if(j%2 == 0 && i%2 == 0)ofSetColor(0);
        if(j%2 == 0 && i%2 == 1)ofSetColor(255);
        if(j%2 == 1 && i%2 == 1)ofSetColor(0);
        if(j%2 == 1 && i%2 == 0)ofSetColor(255);
        ofRect(i*squareW,j*squareH,squareW,squareH);
    }
}
for(i = 0 ; i < 12 ; i++){
        ofSetColor(200);
    ofCircle(blackLocX[i], blackLocY[i], pieceR);
}
for(i = 0 ; i < 12 ; i++){
        ofSetColor(50);
    ofCircle(redLocX[i], redLocY[i], pieceR);
}
}

ofApp.h

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{
public:
float distance(int n,int valX, int valY);
    void setup();
    void update();
    void draw();

    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y);
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);

    int squareW, squareH;
    int pieceR;
    int numSpaces;
    int blackLocX[12], blackLocY[12];
    int redLocX[12], redLocY[12];
};

ofApp.cpp

#include "ofApp.h"

/*float distance(int valX, int valY){
float d,x,y;
x = ofGetMouseX();
y = ofGetMouseY();
d = sqrt((valY-y)*(valY-y)+(valX-x)*(valX-x));
return d;
}*/


float ofApp::distance(int n,int valX, int valY) {
    float d;
    d = sqrt((valY-mouseY)*(valY-mouseY)+(valX-mouseX)(valX-mouseX));
    return d;
}

//--------------------------------------------------------------
void ofApp::setup(){
    squareW = 100;
    squareH = 100;
    pieceR = 20;
    numSpaces = 8;

    int i,j;
    //startLoc
    for(j = 0 ; j < 3 ; j++){
        if(j == 0){
            for(i = 0 ; i < 4 ; i++){
                blackLocX[i] = i*200+50;
                blackLocY[i] = 50;
            }
        }
        if(j == 1){
            for(i = 4 ; i < 8 ; i++){
                blackLocX[i] = (i-4)*200+150;
                blackLocY[i] = 150;
            }
        }
        if(j == 2){
            for(i = 8 ; i < 12 ; i++){
                blackLocX[i] = (i-8)*200+50;
                blackLocY[i] = 250;
            }
        }
    }
    for(j = 0 ; j < 3 ; j++){
        if(j == 0){
            for(i = 0 ; i < 4 ; i++){
                redLocX[i] = i*200+50;
                redLocY[i] = 550;
            }
        }
        if(j == 1){
            for(i = 4 ; i < 8 ; i++){
               redLocX[i] = (i-4)*200+150;
                redLocY[i] = 650;
            }
        }
        if(j == 2){
            for(i = 8 ; i < 12 ; i++){
                redLocX[i] = (i-8)*200+50;
                redLocY[i] = 750;
            }
        }
    }
}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){
    int i,j;
    //Draw Checkers
    for(j = 0 ; j < numSpaces ; j++){
        for(i = 0 ; i < numSpaces ; i++){
            if(j%2 == 0 && i%2 == 0)ofSetColor(0);
            if(j%2 == 0 && i%2 == 1)ofSetColor(255);
            if(j%2 == 1 && i%2 == 1)ofSetColor(0);
            if(j%2 == 1 && i%2 == 0)ofSetColor(255);
            ofRect(i*squareW,j*squareH,squareW,squareH);
        }
    }
    for(i = 0 ; i < 12 ; i++){
        ofSetColor(200);
        ofCircle(blackLocX[i], blackLocY[i], pieceR);
    }
    for(i = 0 ; i < 12 ; i++){
        ofSetColor(50);
        ofCircle(redLocX[i], redLocY[i], pieceR);
    }

    //We print out the distance to the console
    float dist = distance(1, 100, 200); //change variables here
    ofLogNotice("ofApp") << "Distance:" << distance;
}

btw it is difficult to share long code on the forum.

Now changing the mouse getters is better as Zach suggested:

float ofApp::distance(int n,int valX, int valY) {
    float d,x,y;
    x = ofGetMouseX();
    y = ofGetMouseY();
    d = sqrt((valY-y)*(valY-y)+(valX-x)(valX-x));
    return d;
}

I will just stick with Zach’s method for now. Thanks tho!

Hello, in OF exist a function for compute distance between two points:
example:
float distance = ofDist(x,y, ofGetMouseX(), ofGetMouseY());
you see the apropiatte doc in OF reference but the function receiva four parameters:
float x1 = the x1 coordinate of point A.
float y1 = the y1 coordinate of point A
float x2 = the x2 coordinate of point B
float y2 = the y2 coordinate of point B
and return distance between point A and B.

This may be a bit naive, but is there a * missing from
d = sqrt((valY-mouseY)(valY-mouseY)+(valX-mouseX)(valX-mouseX));
i.e.
d = sqrt((valY-mouseY)
(valY-mouseY)+(valX-mouseX) * (valX-mouseX));

You can also use the ofGetAppPtr() method to return a pointer to the baseApp. You can then access arbitrary member variables and member functions that are scoped to the base app, including mouseX and mouseY.

You can use this in external classes:

cout << ofGetAppPtr()->mouseX << endl;