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