Skip to content

Instantly share code, notes, and snippets.

@denkspuren
Created November 19, 2014 14:24
Show Gist options
  • Save denkspuren/c22658e0827dd0056f9e to your computer and use it in GitHub Desktop.
Save denkspuren/c22658e0827dd0056f9e to your computer and use it in GitHub Desktop.
FunctionViewer displays a function object at specified coordinates in window
class Function {
float calc(float x) {
float y = sin(x);
return y;
}
}
class FunctionViewer {
Function function;
float xPos_min, xPos_max, yPos_min, yPos_max;
FunctionViewer(Function f,
float xPos_min, float xPos_max,
float yPos_min, float yPos_max) {
function = f;
this.xPos_min = xPos_min; this.xPos_max = xPos_max;
this.yPos_min = yPos_min; this.yPos_max = yPos_max;
}
void display(float x_min, float x_max,
float y_min, float y_max) {
float x,y,yPos;
for(float xPos=xPos_min;xPos<=xPos_max;xPos++) {
x = map(xPos,xPos_min,xPos_max,x_min,x_max);
y = function.calc(x);
yPos = map(y,y_min,y_max,yPos_max,yPos_min);
point(xPos,yPos);
}
}
}
Function f = new Function();
FunctionViewer sinViewer =
new FunctionViewer(f,50,350,50,250);
void setup() {
size(400,300);
// println(f.calc(0.5));
}
void draw() {
background(255);
sinViewer.display(0,TWO_PI,-1,+1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment