1
2
3
4
5
|
#include <cassert>#include <cmath>#include <iostream>#include <vector>#include <Windows.h>
#include <glut.h>
using namespace std;class plot{static boolinstanziiert;static floatschritt,links,rechts,hoeheHalbierende,oben,unten;static vector<float(*)(float)> funktionen;static void setNaechsteFarbe(bool reset = false){static int index = 0;if(reset) index = 0;const float farben[][4] ={{1.f, 0.f, 0.f, .5f},{0.f, 1.f, 0.f, .5f},{0.f, 0.f, 1.f, .5f},};glColor4fv(farben[index]);index = (index + 1) % 3;}static void zeichneHalbierende(){glLineWidth(1.f);glColor3f(1.f, 1.f, 1.f);glBegin(GL_LINES);glVertex3f(links,hoeheHalbierende, 0.f);glVertex3f(rechts,hoeheHalbierende, 0.f);glEnd();}static void zeichneRahmen(){glLineWidth(8.f);glColor3f(.15f, .15f, .15f);glBegin(GL_LINE_STRIP);glVertex3f(links,unten,0.f);glVertex3f(rechts,unten,0.f);glVertex3f(rechts,oben,0.f);glVertex3f(links,oben,0.f);glVertex3f(links,unten,0.f);glEnd();}static void init(){glClearColor(0.f, 0.f, 0.f, 0.f);//glEnable(GL_DEPTH_TEST); brauch ich nichtglEnable(GL_LINE_SMOOTH);glEnable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Standard-BlendFunc}static void reshapeFunc(int breite, int hoehe){if(hoehe == 0) hoehe = 1;if(hoehe < breite){glViewport((breite - hoehe) / 2, 0, hoehe, hoehe);}else{glViewport(0, (hoehe - breite) / 2, breite, breite);}glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(links, rechts, unten, oben, 1., -1.);glMatrixMode(GL_MODELVIEW);}static void displayFunc(){glClear(GL_COLOR_BUFFER_BIT);//|//GL_DEPTH_BUFFER_BIT); // brauch ich nichtzeichneHalbierende();glLineWidth(4.f);for(vector<float(*)(float)>::const_iterator it = funktionen.begin(); it != funktionen.end(); it++){if(it == funktionen.begin()) setNaechsteFarbe(true);glBegin(GL_LINE_STRIP);for(float i = links; i < rechts + 2 * schritt; i += schritt){glVertex3f(i, (*it)(i), 0.f);}glEnd();setNaechsteFarbe();}zeichneRahmen();glutSwapBuffers();}static void keyboardFunc(unsigned char taste, int, int){if(taste == 27)exit(0);}static unsigned int __stdcall threadFunktion(void*){glutMainLoop();}public:plot(){assert(!instanziiert);instanziiert = true;schritt= .005f;links= 0.f;rechts= 10.f;hoeheHalbierende= 0.f;oben= hoeheHalbierende + (links - rechts) / 2;unten= hoeheHalbierende - (links - rechts) / 2;
funktionen.push_back(sin);funktionen.push_back(cos);funktionen.push_back(tan);
int argc = 1;char* argumente = "";glutInit(&argc, &argumente);glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);glutCreateWindow("");glutReshapeFunc(reshapeFunc);glutDisplayFunc(displayFunc);glutKeyboardFunc(keyboardFunc);init();// TODO: neuen Thread erzeugenglutMainLoop();}};bool plot::instanziiert= false;float plot::schritt= 0.f;float plot::links= 0.f;float plot::rechts= 0.f;float plot::hoeheHalbierende= 0.f;float plot::oben= 0.f;float plot::unten= 0.f;vector<float(*)(float)> plot::funktionen= vector<float(*)(float)>();int main(){plot();}
|