void drawFunctions() { const float STEP = graphWin.findSmartStepX(); glPointSize(2.0f); //Draw selected (f(x)) for(float x=graphWin.xMin(); x<graphWin.xMax(); x+=STEP) graphWin.plot(x, f(x), 1.0f,0.5f,0.0f); //Draw f(x) = x for(float x=graphWin.xMin(); x<graphWin.xMax(); x+=STEP) graphWin.plot( x, x, 0.5f,0.5f,0.5f); //Draw g(x) for(float x=graphWin.xMin()+STEP; x<graphWin.xMax(); x+=STEP) graphWin.plot(x, g(x), 1.0f,0.5f,1.0f); clear(); printHeader(); std::cout << "x est compris dans l'ensemble [" << graphWin.xMin() << ";" << graphWin.xMax() << "[" << std::endl; std::cout << "y est compris dans l'ensemble [" << graphWin.yMin() << ";" << graphWin.yMax() << "[" << std::endl << std::endl; std::cout << "Information graphique : " << std::endl << "- Fonction choisie (f(x)) en orange" << std::endl << "- Fonction h(x) = x, en gris" << std::endl << "- Fonction g(x) = x + l*f(x), l = 1, en rose" << std::endl << "- Les axes x,y en bleu " << std::endl << "- Les vecteurs unitaires en rouge " << std::endl << "- Les solutions de la fonction en vert" << std::endl << std::endl; vector<long double> solutions = findRoot(); std::cout << "Solutions : " << std::endl; glColor3f(0.0f, 1.0f, 0.0f); glPointSize(10); //Draw solutions for(unsigned int i = 0;i < solutions.size(); ++i) { std::cout << "[" << i << "] -> " << static_cast<double>(solutions[i]) << std::endl; glBegin( GL_POINTS ); glVertex3d(solutions[i], 0, 0.0); glEnd(); } }
void Click(int button, int state, int x, int y) { double posX, posY; switch(button) { case 0: posX = ((double)x / (double)graphWin.xWinFunc()) * (graphWin.xMax() - graphWin.xMin()) + graphWin.xMin(); posY = ((1 - (double)y / (double)graphWin.yWinFunc())) * (graphWin.yMax() - graphWin.yMin()) + graphWin.yMin(); dessinerCourbe(posX, posY); break; } }
void Reshape(int w, int h) { if (h == 0) h = 1; if (w == 0) w = 1; glViewport(0, 0, w, h); graphWin.setWinPixels( w, h ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); float xRatio = static_cast<float>(w)/static_cast<float>(graphWin.xWinFunc()); float yRatio = static_cast<float>(h)/static_cast<float>(graphWin.yWinFunc()); // Volume de clipping : (left, right, bottom, top, near, far) glOrtho(graphWin.xMin()*xRatio, graphWin.xMax()*xRatio, graphWin.yMin()*yRatio, graphWin.yMax()*yRatio, -2.0f, 2.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
std::vector<long double> findRoot() { const long double epsilon = std::numeric_limits<double>::epsilon(); const long double STEP = 2*epsilon; std::vector<long double> solutions; //FindRoot //Recherche les racines du côté x >= 0 long double oldStep = 2*epsilon; for(long double x=oldStep; x<graphWin.xMax(); x+=fabsl(g(x)-x)) { //Dessin des barres comme dans le cours graphWin.segment(x-oldStep,x-oldStep,x-oldStep,g(x-oldStep)); if(x > g(x)) { if(f == f1) graphWin.segment(x-oldStep,g(x-oldStep),x,g(x-oldStep)); else graphWin.segment(x-oldStep,x-oldStep,x,x-oldStep); } else graphWin.segment(x-oldStep,x,x,x); if(fabsl(g(x)-x) <= epsilon) { solutions.push_back(x); x+=STEP;//On ajoute suffisement de STEP pour que la fonction puisse repartir et chercher d'autres solutions } oldStep = fabsl(g(x)-x); } oldStep = 2*epsilon; //Recherche les racines du côté x < 0 for(long double x=-epsilon; x > graphWin.xMin(); x-=fabsl(g(x)-x)) { //Dessin des barres comme dans le cours graphWin.segment(x+oldStep,x+oldStep,x+oldStep,g(x+oldStep)); if(x < g(x)) { if(f == f1) graphWin.segment(x+oldStep,g(x+oldStep),x,g(x+oldStep)); else graphWin.segment(x+oldStep,x+oldStep,x,x+oldStep); } else graphWin.segment(x+oldStep,x,x,x); if(fabsl(g(x)-x) <= epsilon) { solutions.push_back(x); x-=STEP;//On ajoute le STEP initiale pour que la fonction puisse repartir et chercher d'autres solutions } oldStep = fabsl(g(x)-x); } sort(solutions.begin(),solutions.end()); return solutions; }