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)
48 float m=((float)(y2-y1))/(x2-x1);
58 dc.DrawLine(0,y0, x2,y2);
60 else if(y2<=0 && y1>=0)
63 dc.DrawLine(0,y0,x0,0 );
65 else if(y2>=0 && y1<=0)
68 dc.DrawLine(0,y0,x2,y2 );
76 dc.DrawLine(x1,y1, x2,y2);
77 else if(y1>=0 && y2<=0)
78 dc.DrawLine(x1,y1,x0,0 );
79 else if(y1<=0 && y2>=0)
80 dc.DrawLine(x0,0,x2,y2);
87 * Draw the function with th spline points
90 void pPlotterLayer::drawSplineCurve(wxDC & dc,mpWindow & w)
92 std::vector<double> vx=getXSpline();
93 std::vector<double> vy=getYSpline();
96 int counter=0,minX,maxX,minY,maxY;
98 This is the offset of every point scale to the window
101 int offsetpx=getOffsetPixelsXv();
102 int offsetpy=getOffsetPixelsYv();
109 getMaxShowed(maxX,maxY);
110 getMinShowed(minX,minY);
117 Fill the function if it represented an histogram
119 ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+2));
130 for(int i=0;(i+1)< size;i++)
134 double cxi=(vx.at(i)-minX-offsetX)*scaleX+offsetpx;
135 double cyi=(vy.at(i)-minY-offsetY)*scaleY+offsetpy;
137 double cxj=(vx.at(i+1)-minX-offsetX)*scaleX+offsetpx;
138 double cyj=(vy.at(i+1)-minY-offsetY)*scaleY+offsetpy;
142 draw(dc,w,cxi,cyi,cxj,cyj);
145 if(!initializePolygon(ppoints,cxi,cyi,cxj,cyj) && ((cxi<=0 && cxj>=0)||(cxi>=0 && cxj>=0)))
158 //point.x=vx.at(size-1)* scaleX + offsetpx;
159 point.x=ppoints[j-1].x;
161 //ppoints[vx.size()]=point;
164 Fill the function if it represented an histogram
166 //ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+1));
168 //point.x=vx.at(0)*scaleX + offsetpx;
169 point.x=ppoints[0].x;
171 //ppoints[vx.size()+1]=point;
174 dc.SetBrush(wxBrush( wxColour(239,238,242) ,wxSOLID ));
175 dc.SetPen(wxPen( wxColour(0,0,0) ,1,wxSOLID ));
176 //dc.DrawPolygon(vx.size()+2,ppoints,0,0);
177 dc.DrawPolygon(j+2,ppoints,0,0);
184 it define the first point of the polygon for be drawing
185 returs true if the first and second point of the polygon are setted
187 bool pPlotterLayer::initializePolygon(wxPoint* points,double x1, double y1,double x2, double y2)
192 float m=((float)(y2-y1))/(x2-x1);
196 if(points[0].x<=0&& points[1].x<=0 && points[0].y<=0&& points[1].y<=0)
199 int offsetpx=getOffsetPixelsXv();
201 //analyzing the curve
207 //dc.DrawLine(0,y0, x2,y2);
215 else if(y2<=0 && y1>=0)
219 //dc.DrawLine(0,y0,x0,0 );
229 else if(y2>=0 && y1<=0)
233 //dc.DrawLine(0,y0,x2,y2 );
248 //dc.DrawLine(x1,y1, x2,y2);
255 else if(y1>=0 && y2<=0)
257 //dc.DrawLine(x1,y1,x0,0 );
264 else if(y1<=0 && y2>=0)
266 //dc.DrawLine(x0,0,x2,y2);
282 * Draw le lines between the points of the function
284 void pPlotterLayer::drawFunction(wxDC & dc,mpWindow & w)
288 int scrwX,scrwY,offsetpx,offsetpy,maxX,minX,maxY,minY;
289 wxPoint* ppoints=NULL;
292 This is the offset of every point scale to the window
295 offsetpx=getOffsetPixelsXv();
296 offsetpy=getOffsetPixelsYv();
302 dc.GetSize(&scrwX, &scrwY);
304 //Lines between the points
311 getMaxShowed(maxX,maxY);
312 getMinShowed(minX,minY);
316 int xTraslation=getXTraslation();
317 wxNode* node= points.GetFirst();
318 int i=1;//points.GetCount()+1;
321 Fill it if it is an histogram
324 pFunctionPoint* pointk;
328 Fill the function if it represented an histogram
330 ppoints=(wxPoint*)malloc(sizeof(int)*2*(points.GetCount()+2));
340 while (node!=NULL && node->GetNext()!=NULL)
342 pFunctionPoint* pointi=(pFunctionPoint*)node->GetData();
343 wxNode* nextNode=node->GetNext();
344 pFunctionPoint* pointj=(pFunctionPoint*)nextNode->GetData();
347 int pxi=(pointi->getRealX()-minX)-offsetX;
348 int pyi=(pointi->getRealY()-minY)-offsetY;
349 int pxj=(pointj->getRealX()-minX)-offsetX;
350 int pyj=(pointj->getRealY()-minY)-offsetY;
353 int cxi=pxi* scaleX + offsetpx; //+ xTraslation;
354 int cxj=pxj* scaleX + offsetpx; //+ xTraslation;
355 int cyi=pyi* scaleY+ offsetpy ;
356 int cyj=pyj* scaleY+ offsetpy ;
357 //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
359 draw(dc,w,pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
360 //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
363 if(!initializePolygon(ppoints,cxi,cyi,cxj,cyj) && ((cxi<=0 && cxj>=0)||(cxi>=0 && cxj>=0)))
373 node=node->GetNext();
378 //point.x=vx.at(size-1)* scaleX + offsetpx;
379 point.x=ppoints[j-1].x;
381 //ppoints[vx.size()]=point;
384 Fill the function if it represented an histogram
386 //ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+1));
388 //point.x=vx.at(0)*scaleX + offsetpx;
389 point.x=ppoints[0].x;
391 //ppoints[vx.size()+1]=point;
394 dc.SetBrush(wxBrush( wxColour(239,238,242) ,wxSOLID ));
395 dc.SetPen(wxPen( wxColour(0,0,0) ,1,wxSOLID ));
396 //dc.DrawPolygon(vx.size()+2,ppoints,0,0);
397 dc.DrawPolygon(j+2,ppoints,0,0);
404 * Draw the points of the function
406 void pPlotterLayer::drawPoints(wxDC & dc,mpWindow & w)
411 int minX,maxX,minY,maxY;
415 This is the offset of every point scale to the window
418 int offsetpx=getOffsetPixelsXv();
419 int offsetpy=getOffsetPixelsYv();
421 int xTraslation=getXTraslation();
422 /*wxPen mypen(*wxBLACK, 5, wxSOLID);
425 getMaxShowed(maxX,maxY);
426 getMinShowed(minX,minY);
428 //we have to draw the points
430 while (GetNextXY(x, y))
433 //set the points of the polygons
435 dc.SetBrush(wxBrush( wxColour(255,0,0),wxSOLID ));
436 //dc.SetPen(wxPen(wxColour(0,0,0),1,wxSOLID) );
437 points[0].x=((x-minX-offsetX)*scaleX + offsetpx-MOVE);
438 points[0].y=((y-minY-offsetY)*scaleY+ offsetpy-MOVE);
439 points[1].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
440 points[1].y=((y-minY-offsetY)*scaleY+ offsetpy-MOVE);
441 points[2].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
442 points[2].y=((y-minY-offsetY)*scaleY+ offsetpy+MOVE);
443 points[3].x=((x-minX-offsetX)*scaleX+ offsetpx-MOVE);
444 points[3].y=((y-minY-offsetY)*scaleY+ offsetpy+MOVE);
445 if((x-minX-offsetX)*scaleX >=0 && (y-minY-offsetY/*w.getMinScrY()*/)*scaleY>=0)
446 dc.DrawPolygon(4,points,0,0);
451 * Draw the line between the last point of the function
452 * and the position of the mouse
454 void pPlotterLayer::drawLineToMousePoint(wxDC & dc,mpWindow & w)
456 int x,y,sizeP,maxX,maxY,minX,minY;
462 getDirection(direction);
464 //Lines betwen the points
467 wxNode *node=points.GetLast();
468 wxNode *node1=points.GetFirst();
472 This is the offset of every point scale to the window
475 int offsetpx = getOffsetPixelsXv();
476 int offsetpy = getOffsetPixelsYv();
478 getMaxShowed(maxX,maxY);
479 getMinShowed(minX,minY);
482 int xTraslation=getXTraslation();
486 pFunctionPoint* lastPoint=(pFunctionPoint*)node->GetData();
487 pFunctionPoint* firstPoint=(pFunctionPoint*)node1->GetData();
490 if(lastPoint->getRealX()<x && direction && sizeP >=2)
492 dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
495 else if(firstPoint->getRealX()>x && !direction && sizeP >=2 )
497 dc.DrawLine((firstPoint->getRealX()-minX-offsetX)*scaleX+ offsetpx,(firstPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy,(x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
502 dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
506 if( w.drawGuideLines() )
508 dc.SetPen(wxPen( wxColour(255,0,0),1,wxDOT ));
509 // Drawing the horizontal guide line
510 dc.DrawLine( 0, (y-minY)*scaleY + offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
511 // Drawing the vertical guide line
512 dc.DrawLine( (x-minX)*scaleX + offsetpx,0, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
519 This implementation will plot the locus in the visible area and
520 put a label according to the aligment specified.
522 void pPlotterLayer::Plot(wxDC & dc, mpWindow & w)
524 bool show,drawing,actual;
525 int scrwX,scrwY,maxX,maxY,minX,minY,type;
535 * Managing the scale and zoom
538 dc.GetSize(&scrwX, &scrwY);
540 //getMax(maxX, maxY);
542 getMaxShowed(maxX,maxY);
543 getMinShowed(minX,minY);
545 getFactorZoom(factorZoom);
546 getOffsets(offsetX,offsetY);
553 if((maxX-minX)!=0 && (maxY-minY)!=0)
556 scaleX=(((double)scrwX-100)/(maxX-minX))*factorZoom;
557 scaleY=(((double)scrwY-50)/(maxY-minY))*factorZoom;
563 //w.setMinScrX(offsetX);
564 //w.setMinScrY(offsetY);
572 int orgy=w.GetScrY()-40;
573 dc.SetDeviceOrigin( orgx ,orgy);
574 dc.SetAxisOrientation(true,true);
577 //if the user dont want to see the points of the function and if he stops drawing
578 //we have to draw the function
579 if(!show && !drawing && type==1)
581 else if(!show && !drawing && type==2)
582 drawSplineCurve(dc,w);
583 //we just draw the point that the user just clicked
584 else if(drawing && type==1)
587 drawLineToMousePoint(dc,w);
590 else if(drawing && type==1)
592 drawSplineCurve(dc,w);
593 drawLineToMousePoint(dc,w);
596 else if(show && type==1)
601 else if(show && type==2)
603 drawSplineCurve(dc,w);
607 // Drawing the guides according to real values entered according to the integrated interaction (IS NOT WORKING!!!)
610 dc.SetPen(wxPen( wxColour(255,0,0),1,wxDOT ));
611 int offsetpx = getOffsetPixelsXv();
612 int offsetpy = getOffsetPixelsYv();
614 int realX_guide = w.getRealGuideX();
615 if( realX_guide!=-1 )
617 dc.DrawLine( (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx, 0, (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx, scrwY);
620 int realY_guide = w.getRealGuideY();
621 if( realY_guide!=-1 )
623 dc.DrawLine( 0,(realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx, scrwX, (realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx);