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; // JPRx
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 returns true if the first and second point of the polygon are set
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(); //JPRx
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);
283 * Draw the lines between the points of the function
285 void pPlotterLayer::drawFunction(wxDC & dc,mpWindow & w)
289 int scrwX,scrwY,offsetpx,offsetpy,maxX,minX,maxY,minY;
290 wxPoint* ppoints=NULL;
293 This is the offset of every point scale to the window
296 offsetpx=getOffsetPixelsXv();
297 offsetpy=getOffsetPixelsYv();
301 dc.GetSize(&scrwX, &scrwY);
303 //Lines between the points
310 getMaxShowed(maxX,maxY);
311 getMinShowed(minX,minY);
315 //int xTraslation=getXTraslation(); //JPRx
316 wxNode* node= points.GetFirst();
317 // int i=1;//points.GetCount()+1; //JPRx
320 Fill it if it is an histogram
323 // pFunctionPoint* pointk; //JPRx
327 Fill the function if it represented an histogram
329 ppoints=(wxPoint*)malloc(sizeof(int)*2*(points.GetCount()+2));
339 while (node!=NULL && node->GetNext()!=NULL)
341 pFunctionPoint* pointi=(pFunctionPoint*)node->GetData();
342 wxNode* nextNode=node->GetNext();
343 pFunctionPoint* pointj=(pFunctionPoint*)nextNode->GetData();
346 int pxi=(pointi->getRealX()-minX)-offsetX;
347 int pyi=(pointi->getRealY()-minY)-offsetY;
348 int pxj=(pointj->getRealX()-minX)-offsetX;
349 int pyj=(pointj->getRealY()-minY)-offsetY;
352 int cxi=pxi* scaleX + offsetpx; //+ xTraslation;
353 int cxj=pxj* scaleX + offsetpx; //+ xTraslation;
354 int cyi=pyi* scaleY+ offsetpy ;
355 int cyj=pyj* scaleY+ offsetpy ;
356 //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
358 draw(dc,w,pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
359 //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
362 if(!initializePolygon(ppoints,cxi,cyi,cxj,cyj) && ((cxi<=0 && cxj>=0)||(cxi>=0 && cxj>=0)))
372 node=node->GetNext();
377 //point.x=vx.at(size-1)* scaleX + offsetpx;
378 point.x=ppoints[j-1].x;
380 //ppoints[vx.size()]=point;
383 Fill the function if it represented an histogram
385 //ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+1));
387 //point.x=vx.at(0)*scaleX + offsetpx;
388 point.x=ppoints[0].x;
390 //ppoints[vx.size()+1]=point;
393 dc.SetBrush(wxBrush( wxColour(239,238,242) ,wxSOLID ));
394 dc.SetPen(wxPen( wxColour(0,0,0) ,1,wxSOLID ));
395 //dc.DrawPolygon(vx.size()+2,ppoints,0,0);
396 dc.DrawPolygon(j+2,ppoints,0,0);
403 * Draw the points of the function
405 void pPlotterLayer::drawPoints(wxDC & dc,mpWindow & w)
410 int minX,maxX,minY,maxY;
414 This is the offset of every point scale to the window
417 int offsetpx=getOffsetPixelsXv();
418 int offsetpy=getOffsetPixelsYv();
420 // int xTraslation=getXTraslation(); //EED
421 /*wxPen mypen(*wxBLACK, 5, wxSOLID);
424 getMaxShowed(maxX,maxY);
425 getMinShowed(minX,minY);
427 //we have to draw the points
429 while (GetNextXY(x, y))
432 //set the points of the polygons
434 dc.SetBrush(wxBrush( wxColour(255,0,0),wxSOLID ));
435 //dc.SetPen(wxPen(wxColour(0,0,0),1,wxSOLID) );
436 points[0].x=((x-minX-offsetX)*scaleX + offsetpx-MOVE);
437 points[0].y=((y-minY-offsetY)*scaleY+ offsetpy-MOVE);
438 points[1].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
439 points[1].y=((y-minY-offsetY)*scaleY+ offsetpy-MOVE);
440 points[2].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
441 points[2].y=((y-minY-offsetY)*scaleY+ offsetpy+MOVE);
442 points[3].x=((x-minX-offsetX)*scaleX+ offsetpx-MOVE);
443 points[3].y=((y-minY-offsetY)*scaleY+ offsetpy+MOVE);
444 if((x-minX-offsetX)*scaleX >=0 && (y-minY-offsetY/*w.getMinScrY()*/)*scaleY>=0)
445 dc.DrawPolygon(4,points,0,0);
450 * Draw the line between the last point of the function
451 * and the position of the mouse
453 void pPlotterLayer::drawLineToMousePoint(wxDC & dc,mpWindow & w)
455 int x,y,sizeP,maxX,maxY,minX,minY;
461 getDirection(direction);
463 //Lines betwen the points
466 wxNode *node=points.GetLast();
467 wxNode *node1=points.GetFirst();
471 This is the offset of every point scale to the window
474 int offsetpx = getOffsetPixelsXv();
475 int offsetpy = getOffsetPixelsYv();
477 getMaxShowed(maxX,maxY);
478 getMinShowed(minX,minY);
481 // int xTraslation=getXTraslation(); //EED
485 pFunctionPoint* lastPoint=(pFunctionPoint*)node->GetData();
486 pFunctionPoint* firstPoint=(pFunctionPoint*)node1->GetData();
489 if(lastPoint->getRealX()<x && direction && sizeP >=2)
491 dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
494 else if(firstPoint->getRealX()>x && !direction && sizeP >=2 )
496 dc.DrawLine((firstPoint->getRealX()-minX-offsetX)*scaleX+ offsetpx,(firstPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy,(x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
501 dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
505 if( w.drawGuideLines() )
507 dc.SetPen(wxPen( wxColour(255,0,0),1,wxDOT ));
508 // Drawing the horizontal guide line
509 dc.DrawLine( 0, (y-minY)*scaleY + offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
510 // Drawing the vertical guide line
511 dc.DrawLine( (x-minX)*scaleX + offsetpx,0, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
518 This implementation will plot the locus in the visible area and
519 put a label according to the aligment specified.
521 void pPlotterLayer::Plot(wxDC & dc, mpWindow & w)
523 bool show,drawing,actual;
524 int scrwX,scrwY,maxX,maxY,minX,minY,type;
534 * Managing the scale and zoom
537 dc.GetSize(&scrwX, &scrwY);
539 //getMax(maxX, maxY);
541 getMaxShowed(maxX,maxY);
542 getMinShowed(minX,minY);
544 getFactorZoom(factorZoom);
545 getOffsets(offsetX,offsetY);
552 if((maxX-minX)!=0 && (maxY-minY)!=0)
555 scaleX=(((double)scrwX-100)/(maxX-minX))*factorZoom;
556 scaleY=(((double)scrwY-50)/(maxY-minY))*factorZoom;
562 //w.setMinScrX(offsetX);
563 //w.setMinScrY(offsetY);
574 // int orgy=w.GetScrY()-40;
575 // dc.SetDeviceOrigin( orgx ,orgy);
576 // dc.SetAxisOrientation(true,true);
579 dc.SetDeviceOrigin( orgx ,orgy);
580 dc.SetAxisOrientation(true,false);
584 //if the user dont want to see the points of the function and if he stops drawing
585 //we have to draw the function
586 if(!show && !drawing && type==1)
588 else if(!show && !drawing && type==2)
589 drawSplineCurve(dc,w);
590 //we just draw the point that the user just clicked
591 else if(drawing && type==1)
594 drawLineToMousePoint(dc,w);
597 else if(drawing && type==1)
599 drawSplineCurve(dc,w);
600 drawLineToMousePoint(dc,w);
603 else if(show && type==1)
608 else if(show && type==2)
610 drawSplineCurve(dc,w);
614 // Drawing the guides according to real values entered according to the integrated interaction (IS NOT WORKING!!!)
617 dc.SetPen(wxPen( wxColour(255,0,0),1,wxDOT ));
618 int offsetpx = getOffsetPixelsXv();
619 // int offsetpy = getOffsetPixelsYv(); //EED
621 int realX_guide = w.getRealGuideX();
622 if( realX_guide!=-1 )
624 dc.DrawLine( (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx, 0, (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx, scrwY);
627 int realY_guide = w.getRealGuideY();
628 if( realY_guide!=-1 )
630 dc.DrawLine( 0,(realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx, scrwX, (realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx);