1 //----------------------------------------------------------------------------
2 // Class definition include
3 //----------------------------------------------------------------------------
4 #include "pPlotterLayer.h"
6 // ----------------------------------------------------------------------------
7 // WX headers inclusion.
8 // For compilers that support precompilation, includes <wx/wx.h>.
9 // ----------------------------------------------------------------------------
18 //----------------------------------------------------------------------------
19 // Class implementation
20 //----------------------------------------------------------------------------
22 IMPLEMENT_ABSTRACT_CLASS(pPlotterLayer, mpLayer)
23 //----------------------------------------------------------------------------
25 //----------------------------------------------------------------------------
29 *@param flags Label alignment, pass one of #mpALIGN_NE, #mpALIGN_NW, #mpALIGN_SW, #mpALIGN_SE.
31 pPlotterLayer:: pPlotterLayer(wxString name , int flags )
35 points.DeleteContents(TRUE);
41 Draw the line from (x1,y1) to (x2,y2) only passing by the
42 positive points in the line
44 void pPlotterLayer::draw(wxDC & dc,mpWindow & w,double x1,double y1,double x2,double y2, int orgy)
48 float m=((float)(y2-y1))/(x2-x1);
52 double sizedc = dc.GetSize().GetHeight()-orgy;
59 dc.DrawLine(0,GetYTranslated(sizedc,y0), x2,GetYTranslated(sizedc,y2));
61 else if(y2<=0 && y1>=0)
64 dc.DrawLine(0,GetYTranslated(sizedc,y0),x0,GetYTranslated(sizedc,0) );
66 else if(y2>=0 && y1<=0)
69 dc.DrawLine(0,GetYTranslated(sizedc,y0),x2,GetYTranslated(sizedc,y2) );
77 dc.DrawLine(x1,GetYTranslated(sizedc,y1), x2,GetYTranslated(sizedc,y2));
78 else if(y1>=0 && y2<=0)
79 dc.DrawLine(x1,GetYTranslated(sizedc,y1),x0,GetYTranslated(sizedc,0) );
80 else if(y1<=0 && y2>=0)
81 dc.DrawLine(x0,GetYTranslated(sizedc,0),x2,GetYTranslated(sizedc,y2));
88 * Draw the function with th spline points
91 void pPlotterLayer::drawSplineCurve(wxDC & dc,mpWindow & w, int orgy)
93 std::vector<double> vx=getXSpline();
94 std::vector<double> vy=getYSpline();
97 int /*counter=0,*/ minX,maxX,minY,maxY; // JPRx
99 This is the offset of every point scale to the window
102 int offsetpx=getOffsetPixelsXv();
103 int offsetpy=getOffsetPixelsYv();
110 getMaxShowed(maxX,maxY);
111 getMinShowed(minX,minY);
118 Fill the function if it represented an histogram
120 ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+2));
131 for(int i=0;(i+1)< size;i++)
135 double cxi=(vx.at(i)-minX-offsetX)*scaleX+offsetpx;
136 double cyi=(vy.at(i)-minY-offsetY)*scaleY+offsetpy;
138 double cxj=(vx.at(i+1)-minX-offsetX)*scaleX+offsetpx;
139 double cyj=(vy.at(i+1)-minY-offsetY)*scaleY+offsetpy;
143 draw(dc,w,cxi,cyi,cxj,cyj,orgy);
146 if(!initializePolygon(ppoints,cxi,cyi,cxj,cyj) && ((cxi<=0 && cxj>=0)||(cxi>=0 && cxj>=0)))
159 //point.x=vx.at(size-1)* scaleX + offsetpx;
160 point.x=ppoints[j-1].x;
162 //ppoints[vx.size()]=point;
165 Fill the function if it represented an histogram
167 //ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+1));
169 //point.x=vx.at(0)*scaleX + offsetpx;
170 point.x=ppoints[0].x;
172 //ppoints[vx.size()+1]=point;
175 dc.SetBrush(wxBrush( wxColour(239,238,242) ,wxSOLID ));
176 dc.SetPen(wxPen( wxColour(0,0,0) ,1,wxSOLID ));
177 //dc.DrawPolygon(vx.size()+2,ppoints,0,0);
178 for(int i = 0; i <= j + 1; i++){
179 int sizedc = dc.GetSize().GetHeight()-orgy;
180 ppoints[i].y = GetYTranslated(sizedc, ppoints[i].y);
182 dc.DrawPolygon(j+2,ppoints,0,0);
189 it define the first point of the polygon for be drawing
190 returns true if the first and second point of the polygon are set
192 bool pPlotterLayer::initializePolygon(wxPoint* points,double x1, double y1,double x2, double y2)
197 float m=((float)(y2-y1))/(x2-x1);
201 if(points[0].x<=0&& points[1].x<=0 && points[0].y<=0&& points[1].y<=0)
204 //int offsetpx=getOffsetPixelsXv(); //JPRx
206 //analyzing the curve
212 //dc.DrawLine(0,y0, x2,y2);
220 else if(y2<=0 && y1>=0)
224 //dc.DrawLine(0,y0,x0,0 );
234 else if(y2>=0 && y1<=0)
238 //dc.DrawLine(0,y0,x2,y2 );
253 //dc.DrawLine(x1,y1, x2,y2);
260 else if(y1>=0 && y2<=0)
262 //dc.DrawLine(x1,y1,x0,0 );
269 else if(y1<=0 && y2>=0)
271 //dc.DrawLine(x0,0,x2,y2);
288 * Draw the lines between the points of the function
290 void pPlotterLayer::drawFunction(wxDC & dc,mpWindow & w, int orgy)
294 int scrwX,scrwY,offsetpx,offsetpy,maxX,minX,maxY,minY;
295 wxPoint* ppoints=NULL;
298 This is the offset of every point scale to the window
301 offsetpx=getOffsetPixelsXv();
302 offsetpy=getOffsetPixelsYv();
306 dc.GetSize(&scrwX, &scrwY);
308 //Lines between the points
310 int sizedc = dc.GetSize().GetHeight()-orgy;
317 getMaxShowed(maxX,maxY);
318 getMinShowed(minX,minY);
322 //int xTraslation=getXTraslation(); //JPRx
323 wxNode* node= points.GetFirst();
324 // int i=1;//points.GetCount()+1; //JPRx
327 Fill it if it is an histogram
330 // pFunctionPoint* pointk; //JPRx
334 Fill the function if it represented an histogram
336 ppoints=(wxPoint*)malloc(sizeof(int)*2*(points.GetCount()+2));
339 point.y=GetYTranslated(sizedc,-5000);
346 while (node!=NULL && node->GetNext()!=NULL)
348 pFunctionPoint* pointi=(pFunctionPoint*)node->GetData();
349 wxNode* nextNode=node->GetNext();
350 pFunctionPoint* pointj=(pFunctionPoint*)nextNode->GetData();
353 int pxi=(pointi->getRealX()-minX)-offsetX;
354 int pyi=(pointi->getRealY()-minY)-offsetY;
355 int pxj=(pointj->getRealX()-minX)-offsetX;
356 int pyj=(pointj->getRealY()-minY)-offsetY;
359 int cxi=pxi* scaleX + offsetpx; //+ xTraslation;
360 int cxj=pxj* scaleX + offsetpx; //+ xTraslation;
361 int cyi=pyi* scaleY+ offsetpy ;
362 int cyj=pyj* scaleY+ offsetpy ;
363 //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
365 draw(dc,w,pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy,orgy );
366 //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
369 if(!initializePolygon(ppoints,cxi,cyi,cxj,cyj) && ((cxi<=0 && cxj>=0)||(cxi>=0 && cxj>=0)))
372 point.y=GetYTranslated(sizedc,cyj);
379 node=node->GetNext();
384 //point.x=vx.at(size-1)* scaleX + offsetpx;
385 point.x=ppoints[j-1].x;
386 point.y=GetYTranslated(sizedc,0);
387 //ppoints[vx.size()]=point;
390 Fill the function if it represented an histogram
392 //ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+1));
394 //point.x=vx.at(0)*scaleX + offsetpx;
395 point.x=ppoints[0].x;
396 point.y=GetYTranslated(sizedc,0);
397 //ppoints[vx.size()+1]=point;
400 dc.SetBrush(wxBrush( wxColour(239,238,242) ,wxSOLID ));
401 dc.SetPen(wxPen( wxColour(0,0,0) ,1,wxSOLID ));
402 //dc.DrawPolygon(vx.size()+2,ppoints,0,0);
403 dc.DrawPolygon(j+2,ppoints,0,0);
410 * Draw the points of the function
412 void pPlotterLayer::drawPoints(wxDC & dc,mpWindow & w, int orgy)
417 int minX,maxX,minY,maxY;
419 double sizedc = dc.GetSize().GetHeight()-orgy;
422 This is the offset of every point scale to the window
425 int offsetpx=getOffsetPixelsXv();
426 int offsetpy=getOffsetPixelsYv();
428 // int xTraslation=getXTraslation(); //EED
429 /*wxPen mypen(*wxBLACK, 5, wxSOLID);
432 getMaxShowed(maxX,maxY);
433 getMinShowed(minX,minY);
435 //we have to draw the points
437 while (GetNextXY(x, y))
440 //set the points of the polygons
442 dc.SetBrush(wxBrush( wxColour(255,0,0),wxSOLID ));
443 //dc.SetPen(wxPen(wxColour(0,0,0),1,wxSOLID) );
444 points[0].x=((x-minX-offsetX)*scaleX + offsetpx-MOVE);
445 points[0].y=GetYTranslated(sizedc,((y-minY-offsetY)*scaleY+ offsetpy-MOVE));
446 points[1].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
447 points[1].y=GetYTranslated(sizedc,((y-minY-offsetY)*scaleY+ offsetpy-MOVE));
448 points[2].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
449 points[2].y=GetYTranslated(sizedc,((y-minY-offsetY)*scaleY+ offsetpy+MOVE));
450 points[3].x=((x-minX-offsetX)*scaleX+ offsetpx-MOVE);
451 points[3].y=GetYTranslated(sizedc,((y-minY-offsetY)*scaleY+ offsetpy+MOVE));
452 if((x-minX-offsetX)*scaleX >=0 && (y-minY-offsetY/*w.getMinScrY()*/)*scaleY>=0)
453 dc.DrawPolygon(4,points,0,0);
458 * Draw the line between the last point of the function
459 * and the position of the mouse
461 void pPlotterLayer::drawLineToMousePoint(wxDC & dc,mpWindow & w, int orgy)
463 int x,y,sizeP,maxX,maxY,minX,minY;
469 getDirection(direction);
471 //Lines betwen the points
474 wxNode *node=points.GetLast();
475 wxNode *node1=points.GetFirst();
479 This is the offset of every point scale to the window
482 int offsetpx = getOffsetPixelsXv();
483 int offsetpy = getOffsetPixelsYv();
485 getMaxShowed(maxX,maxY);
486 getMinShowed(minX,minY);
489 // int xTraslation=getXTraslation(); //EED
493 pFunctionPoint* lastPoint=(pFunctionPoint*)node->GetData();
494 pFunctionPoint* firstPoint=(pFunctionPoint*)node1->GetData();
497 if(lastPoint->getRealX()<x && direction && sizeP >=2)
499 dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
502 else if(firstPoint->getRealX()>x && !direction && sizeP >=2 )
504 dc.DrawLine((firstPoint->getRealX()-minX-offsetX)*scaleX+ offsetpx,(firstPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy,(x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
509 dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
513 if( w.drawGuideLines() )
515 dc.SetPen(wxPen( wxColour(255,0,0),1,wxDOT ));
516 // Drawing the horizontal guide line
517 dc.DrawLine( 0, (y-minY)*scaleY + offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
518 // Drawing the vertical guide line
519 dc.DrawLine( (x-minX)*scaleX + offsetpx,0, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
526 This implementation will plot the locus in the visible area and
527 put a label according to the aligment specified.
529 void pPlotterLayer::Plot(wxDC & dc, mpWindow & w)
531 bool show,drawing,actual;
532 int scrwX,scrwY,maxX,maxY,minX,minY,type;
542 * Managing the scale and zoom
545 dc.GetSize(&scrwX, &scrwY);
548 //getMax(maxX, maxY);
550 getMaxShowed(maxX,maxY);
551 getMinShowed(minX,minY);
553 getFactorZoom(factorZoom);
554 getOffsets(offsetX,offsetY);
561 if((maxX-minX)!=0 && (maxY-minY)!=0)
564 scaleX=(((double)scrwX-100)/(maxX-minX))*factorZoom;
565 scaleY=(((double)scrwY-50)/(maxY-minY))*factorZoom;
571 //w.setMinScrX(offsetX);
572 //w.setMinScrY(offsetY);
583 // int orgy=w.GetScrY()-40;
584 // dc.SetDeviceOrigin( orgx ,orgy);
585 // dc.SetAxisOrientation(true,true);
588 //dc.SetDeviceOrigin( orgx ,orgy);
589 dc.SetDeviceOrigin( orgx ,0);
590 int sizedc = dc.GetSize().GetHeight()-orgy;
591 //dc.SetAxisOrientation(true,false);
595 //if the user dont want to see the points of the function and if he stops drawing
596 //we have to draw the function
597 if(!show && !drawing && type==1)
598 drawFunction(dc,w,orgy);
599 else if(!show && !drawing && type==2)
600 drawSplineCurve(dc,w,orgy);
601 //we just draw the point that the user just clicked
602 else if(drawing && type==1)
604 drawFunction(dc,w,orgy);
605 drawLineToMousePoint(dc,w,orgy);
606 drawPoints(dc,w,orgy);
608 else if(drawing && type==1)
610 drawSplineCurve(dc,w,orgy);
611 drawLineToMousePoint(dc,w,orgy);
612 drawPoints(dc,w,orgy);
614 else if(show && type==1)
616 drawFunction(dc,w,orgy);
617 drawPoints(dc,w,orgy);
619 else if(show && type==2)
621 drawSplineCurve(dc,w,orgy);
622 drawPoints(dc,w,orgy);
625 // Drawing the guides according to real values entered according to the integrated interaction (IS NOT WORKING!!!)
628 dc.SetPen(wxPen( wxColour(255,0,0),1,wxDOT ));
629 int offsetpx = getOffsetPixelsXv();
630 // int offsetpy = getOffsetPixelsYv(); //EED
632 int realX_guide = w.getRealGuideX();
633 if( realX_guide!=-1 )
635 dc.DrawLine( (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx,
636 GetYTranslated(sizedc,0),
637 (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx,
638 GetYTranslated(sizedc,scrwY));
641 int realY_guide = w.getRealGuideY();
642 if( realY_guide!=-1 )
645 GetYTranslated(sizedc,(realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx),
647 GetYTranslated(sizedc,(realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx));