Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created May 22, 2012 19:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roxlu/2771225 to your computer and use it in GitHub Desktop.
Save roxlu/2771225 to your computer and use it in GitHub Desktop.
1D linear convection
#include "testApp.h"
// generate random data for "u"
void testApp::setup(){
ofBackground(22,33,44);
float dt = 0.1;
for(int i = 0; i < N; ++i) {
u[i] = ofNoise(dt*i);
}
}
// "propagate" the wave
void testApp::convec(float* data) {
float dt = 0.01;
memcpy(u_copy, u, N * sizeof(float));
float c = 1.7; // how fast do you want the wave to propagate
for(int i = 1; i < N; ++i) {
u[i] = u_copy[i] - c * dt * (u_copy[i] - u_copy[i-1]);
}
}
// Use mouseX to change the value in u
void testApp::update(){
ofSetWindowTitle(ofToString(ofGetFrameRate()));
int dx = (float(mouseX) / ofGetWidth()) * N;
dx = std::max<int>(0, std::min<int>(dx, N));
u[dx] = mouseY * 0.01;
convec(u);
}
void testApp::draw(){
drawConvection(u);
}
void testApp::drawConvection(float* data) {
float s = (float)ofGetWidth() / N;
float y = 0;
// draw as line
glColor3f(1,1,1);
glPointSize(2.0f);
glBegin(GL_LINE_STRIP);
for(int i = 0; i < N; ++i) {
glVertex2f(i * s, y + data[i] * 100);
}
glEnd();
// draw as points
glColor3f(1,0,0);
glPointSize(2.0f);
glBegin(GL_POINTS);
for(int i = 0; i < N; ++i) {
glVertex2f(i * s, y + data[i] * 100);
}
glEnd();
}
void testApp::keyPressed(int key){}
void testApp::keyReleased(int key){}
void testApp::mouseMoved(int x, int y){ }
void testApp::mouseDragged(int x, int y, int button){}
void testApp::mousePressed(int x, int y, int button){}
void testApp::mouseReleased(int x, int y, int button){}
void testApp::windowResized(int w, int h){ }
void testApp::gotMessage(ofMessage msg){}
void testApp::dragEvent(ofDragInfo dragInfo){}
#pragma once
#include "ofMain.h"
#define N 64
class testApp : public ofBaseApp{
public:
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);
void drawConvection(float* data);
void convec(float* data);
float u[N];
float u_copy[N];
};
@roxlu
Copy link
Author

roxlu commented May 22, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment