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);
282 * Draw the 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();
300 dc.GetSize(&scrwX, &scrwY);
302 //Lines between the points
309 getMaxShowed(maxX,maxY);
310 getMinShowed(minX,minY);
314 //int xTraslation=getXTraslation(); //JPRx
315 wxNode* node= points.GetFirst();
316 // int i=1;//points.GetCount()+1; //JPRx
319 Fill it if it is an histogram
322 // pFunctionPoint* pointk; //JPRx
326 Fill the function if it represented an histogram
328 ppoints=(wxPoint*)malloc(sizeof(int)*2*(points.GetCount()+2));
338 while (node!=NULL && node->GetNext()!=NULL)
340 pFunctionPoint* pointi=(pFunctionPoint*)node->GetData();
341 wxNode* nextNode=node->GetNext();
342 pFunctionPoint* pointj=(pFunctionPoint*)nextNode->GetData();
345 int pxi=(pointi->getRealX()-minX)-offsetX;
346 int pyi=(pointi->getRealY()-minY)-offsetY;
347 int pxj=(pointj->getRealX()-minX)-offsetX;
348 int pyj=(pointj->getRealY()-minY)-offsetY;
351 int cxi=pxi* scaleX + offsetpx; //+ xTraslation;
352 int cxj=pxj* scaleX + offsetpx; //+ xTraslation;
353 int cyi=pyi* scaleY+ offsetpy ;
354 int cyj=pyj* scaleY+ offsetpy ;
355 //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
357 draw(dc,w,pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
358 //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
361 if(!initializePolygon(ppoints,cxi,cyi,cxj,cyj) && ((cxi<=0 && cxj>=0)||(cxi>=0 && cxj>=0)))
371 node=node->GetNext();
376 //point.x=vx.at(size-1)* scaleX + offsetpx;
377 point.x=ppoints[j-1].x;
379 //ppoints[vx.size()]=point;
382 Fill the function if it represented an histogram
384 //ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+1));
386 //point.x=vx.at(0)*scaleX + offsetpx;
387 point.x=ppoints[0].x;
389 //ppoints[vx.size()+1]=point;
392 dc.SetBrush(wxBrush( wxColour(239,238,242) ,wxSOLID ));
393 dc.SetPen(wxPen( wxColour(0,0,0) ,1,wxSOLID ));
394 //dc.DrawPolygon(vx.size()+2,ppoints,0,0);
395 dc.DrawPolygon(j+2,ppoints,0,0);
402 * Draw the points of the function
404 void pPlotterLayer::drawPoints(wxDC & dc,mpWindow & w)
409 int minX,maxX,minY,maxY;
413 This is the offset of every point scale to the window
416 int offsetpx=getOffsetPixelsXv();
417 int offsetpy=getOffsetPixelsYv();
419 int xTraslation=getXTraslation();
420 /*wxPen mypen(*wxBLACK, 5, wxSOLID);
423 getMaxShowed(maxX,maxY);
424 getMinShowed(minX,minY);
426 //we have to draw the points
428 while (GetNextXY(x, y))
431 //set the points of the polygons
433 dc.SetBrush(wxBrush( wxColour(255,0,0),wxSOLID ));
434 //dc.SetPen(wxPen(wxColour(0,0,0),1,wxSOLID) );
435 points[0].x=((x-minX-offsetX)*scaleX + offsetpx-MOVE);
436 points[0].y=((y-minY-offsetY)*scaleY+ offsetpy-MOVE);
437 points[1].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
438 points[1].y=((y-minY-offsetY)*scaleY+ offsetpy-MOVE);
439 points[2].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
440 points[2].y=((y-minY-offsetY)*scaleY+ offsetpy+MOVE);
441 points[3].x=((x-minX-offsetX)*scaleX+ offsetpx-MOVE);
442 points[3].y=((y-minY-offsetY)*scaleY+ offsetpy+MOVE);
443 if((x-minX-offsetX)*scaleX >=0 && (y-minY-offsetY/*w.getMinScrY()*/)*scaleY>=0)
444 dc.DrawPolygon(4,points,0,0);
449 * Draw the line between the last point of the function
450 * and the position of the mouse
452 void pPlotterLayer::drawLineToMousePoint(wxDC & dc,mpWindow & w)
454 int x,y,sizeP,maxX,maxY,minX,minY;
460 getDirection(direction);
462 //Lines betwen the points
465 wxNode *node=points.GetLast();
466 wxNode *node1=points.GetFirst();
470 This is the offset of every point scale to the window
473 int offsetpx = getOffsetPixelsXv();
474 int offsetpy = getOffsetPixelsYv();
476 getMaxShowed(maxX,maxY);
477 getMinShowed(minX,minY);
480 int xTraslation=getXTraslation();
484 pFunctionPoint* lastPoint=(pFunctionPoint*)node->GetData();
485 pFunctionPoint* firstPoint=(pFunctionPoint*)node1->GetData();
488 if(lastPoint->getRealX()<x && direction && sizeP >=2)
490 dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
493 else if(firstPoint->getRealX()>x && !direction && sizeP >=2 )
495 dc.DrawLine((firstPoint->getRealX()-minX-offsetX)*scaleX+ offsetpx,(firstPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy,(x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
500 dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
504 if( w.drawGuideLines() )
506 dc.SetPen(wxPen( wxColour(255,0,0),1,wxDOT ));
507 // Drawing the horizontal guide line
508 dc.DrawLine( 0, (y-minY)*scaleY + offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
509 // Drawing the vertical guide line
510 dc.DrawLine( (x-minX)*scaleX + offsetpx,0, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
517 This implementation will plot the locus in the visible area and
518 put a label according to the aligment specified.
520 void pPlotterLayer::Plot(wxDC & dc, mpWindow & w)
522 bool show,drawing,actual;
523 int scrwX,scrwY,maxX,maxY,minX,minY,type;
533 * Managing the scale and zoom
536 dc.GetSize(&scrwX, &scrwY);
538 //getMax(maxX, maxY);
540 getMaxShowed(maxX,maxY);
541 getMinShowed(minX,minY);
543 getFactorZoom(factorZoom);
544 getOffsets(offsetX,offsetY);
551 if((maxX-minX)!=0 && (maxY-minY)!=0)
554 scaleX=(((double)scrwX-100)/(maxX-minX))*factorZoom;
555 scaleY=(((double)scrwY-50)/(maxY-minY))*factorZoom;
561 //w.setMinScrX(offsetX);
562 //w.setMinScrY(offsetY);
570 int orgy=w.GetScrY()-40;
571 dc.SetDeviceOrigin( orgx ,orgy);
572 dc.SetAxisOrientation(true,true);
575 //if the user dont want to see the points of the function and if he stops drawing
576 //we have to draw the function
577 if(!show && !drawing && type==1)
579 else if(!show && !drawing && type==2)
580 drawSplineCurve(dc,w);
581 //we just draw the point that the user just clicked
582 else if(drawing && type==1)
585 drawLineToMousePoint(dc,w);
588 else if(drawing && type==1)
590 drawSplineCurve(dc,w);
591 drawLineToMousePoint(dc,w);
594 else if(show && type==1)
599 else if(show && type==2)
601 drawSplineCurve(dc,w);
605 // Drawing the guides according to real values entered according to the integrated interaction (IS NOT WORKING!!!)
608 dc.SetPen(wxPen( wxColour(255,0,0),1,wxDOT ));
609 int offsetpx = getOffsetPixelsXv();
610 int offsetpy = getOffsetPixelsYv();
612 int realX_guide = w.getRealGuideX();
613 if( realX_guide!=-1 )
615 dc.DrawLine( (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx, 0, (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx, scrwY);
618 int realY_guide = w.getRealGuideY();
619 if( realY_guide!=-1 )
621 dc.DrawLine( 0,(realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx, scrwX, (realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx);