#include #include "ezwin.h" using namespace std ; // This must be imported. extern float (*fTab[])(const float) ; void PlotPoints(SimpleWindow &W, float (*f)(const float)) { int i ; const int Res = 100 ; const float invRes = 1.0f/Res ; float centX = W.GetWidth()*0.5 ; float centY = W.GetHeight()*0.5 ; float maxDim = ( centX > centY ? centX : centY ) * 0.9; float grLW = maxDim*0.020 ; float axLW = maxDim*0.010 ; // Plot the function float lastx = -1.0f ; float lasty = f(-1.0f) ; for (i=-Res+1; i<=Res; ++i) { float x = i*invRes ; float y = f(x) ; if (-1.0<=lasty && lasty<=1.0 && -1.0<=y && y<=1.0) { Position begP(centX + lastx*maxDim, centY - lasty*maxDim) ; Position endP(centX + x*maxDim, centY - y*maxDim) ; W.RenderLine(begP, endP, Blue, grLW) ; } lastx = x ; lasty = y ; } // Draw the axes Position ULcorn(centX - maxDim, centY + maxDim) ; Position URcorn(centX + maxDim, centY + maxDim) ; Position LRcorn(centX + maxDim, centY - maxDim) ; Position LLcorn(centX - maxDim, centY - maxDim) ; W.RenderLine(ULcorn, URcorn, Black, axLW) ; W.RenderLine(URcorn, LRcorn, Black, axLW) ; W.RenderLine(LRcorn, LLcorn, Black, axLW) ; W.RenderLine(LLcorn, ULcorn, Black, axLW) ; Position CTop(centX, centY + maxDim) ; Position CBot(centX, centY - maxDim) ; Position CLef(centX - maxDim, centY ) ; Position CRig(centX + maxDim, centY ) ; W.RenderLine(CTop, CBot, Black, axLW) ; W.RenderLine(CLef, CRig, Black, axLW) ; } SimpleWindow *pW; int funcIndex = 0 ; Position home(0.0f, 0.0f) ; int refreshPlot() { PlotPoints(*pW, fTab[funcIndex]) ; return 0 ; } int nextPlot(const Position &Ignored) { pW->Close() ; delete pW ; if (fTab[++funcIndex] == NULL) funcIndex = 0 ; ostringstream winName ; winName << "Function " << funcIndex+1 ; pW = new SimpleWindow(winName.str(), 18.0f, 18.0f, home) ; pW->Open() ; pW->SetMouseClickCallback(nextPlot) ; pW->SetRefreshCallback(refreshPlot) ; PlotPoints(*pW, fTab[funcIndex]) ; return 0 ; } int ApiMain() { pW = new SimpleWindow("Function 1", 18.0f, 18.0f, home) ; pW->Open() ; pW->SetMouseClickCallback(nextPlot) ; pW->SetRefreshCallback(refreshPlot) ; PlotPoints(*pW, fTab[0]) ; return 0 ; } int ApiEnd() { pW->Close() ; return 0 ; }