TPolyLine.cxx

Go to the documentation of this file.
00001 // @(#)root/graf:$Id: TPolyLine.cxx 36100 2010-10-06 08:17:12Z brun $
00002 // Author: Rene Brun   12/12/94
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "Riostream.h"
00013 #include "TROOT.h"
00014 #include "TMath.h"
00015 #include "TVirtualPad.h"
00016 #include "TPolyLine.h"
00017 #include "TClass.h"
00018 
00019 ClassImp(TPolyLine)
00020 
00021 
00022 //______________________________________________________________________________
00023 //
00024 //  a PolyLine is defined by an array on N points in a 2-D space.
00025 //
00026 // One can draw the contour of the polyline or/and its fill area.
00027 // Example:
00028 //{
00029 //   Double_t x[5] = {.2,.7,.6,.25,.2};
00030 //   Double_t y[5] = {.5,.1,.9,.7,.5};
00031 //   TPolyLine *pline = new TPolyLine(5,x,y);
00032 //   pline->SetFillColor(38);
00033 //   pline->SetLineColor(2);
00034 //   pline->SetLineWidth(4);
00035 //   pline->Draw("f");
00036 //   pline->Draw();
00037 //}
00038 
00039 
00040 //______________________________________________________________________________
00041 TPolyLine::TPolyLine(): TObject()
00042 {
00043    // PolyLine default constructor.
00044 
00045    fN = 0;
00046    fX = 0;
00047    fY = 0;
00048    fLastPoint = -1;
00049 }
00050 
00051 
00052 //______________________________________________________________________________
00053 TPolyLine::TPolyLine(Int_t n, Option_t *option)
00054       :TObject(), TAttLine(), TAttFill()
00055 {
00056    // PolyLine normal constructor without initialisation.
00057    // Allocates n points.  The option string is ignored.
00058 
00059    fOption = option;
00060    fLastPoint = -1;
00061    if (n <= 0) {
00062       fN = 0;
00063       fLastPoint = -1;
00064       fX = fY = 0;
00065       return;
00066    }
00067    fN = n;
00068    fX = new Double_t[fN];
00069    fY = new Double_t[fN];
00070 }
00071 
00072 
00073 //______________________________________________________________________________
00074 TPolyLine::TPolyLine(Int_t n, Float_t *x, Float_t *y, Option_t *option)
00075       :TObject(), TAttLine(), TAttFill()
00076 {
00077    // PolyLine normal constructor (single precision).
00078    // Makes n points with (x, y) coordinates from x and y.
00079    // The option string is ignored.
00080 
00081    fOption = option;
00082    fLastPoint = -1;
00083    if (n <= 0) {
00084       fN = 0;
00085       fLastPoint = -1;
00086       fX = fY = 0;
00087       return;
00088    }
00089    fN = n;
00090    fX = new Double_t[fN];
00091    fY = new Double_t[fN];
00092    if (!x || !y) return;
00093    for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i];}
00094    fLastPoint = fN-1;
00095 }
00096 
00097 
00098 //______________________________________________________________________________
00099 TPolyLine::TPolyLine(Int_t n, Double_t *x, Double_t *y, Option_t *option)
00100       :TObject(), TAttLine(), TAttFill()
00101 {
00102    // PolyLine normal constructor (double precision).
00103    // Makes n points with (x, y) coordinates from x and y.
00104    // The option string is ignored.
00105 
00106    fOption = option;
00107    fLastPoint = -1;
00108    if (n <= 0) {
00109       fN = 0;
00110       fLastPoint = -1;
00111       fX = fY = 0;
00112       return;
00113    }
00114    fN = n;
00115    fX = new Double_t[fN];
00116    fY = new Double_t[fN];
00117    if (!x || !y) return;
00118    for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i];}
00119    fLastPoint = fN-1;
00120 }
00121 
00122 //______________________________________________________________________________
00123 TPolyLine& TPolyLine::operator=(const TPolyLine& pl)
00124 {
00125    //assignment operator
00126    if(this!=&pl) {
00127       TObject::operator=(pl);
00128       TAttLine::operator=(pl);
00129       TAttFill::operator=(pl);
00130       fN=pl.fN;
00131       fLastPoint=pl.fLastPoint;
00132       fX=pl.fX;
00133       fY=pl.fY;
00134       fOption=pl.fOption;
00135    }
00136    return *this;
00137 }
00138 
00139 //______________________________________________________________________________
00140 TPolyLine::~TPolyLine()
00141 {
00142    // PolyLine default destructor.
00143 
00144    if (fX) delete [] fX;
00145    if (fY) delete [] fY;
00146 }
00147 
00148 
00149 //______________________________________________________________________________
00150 TPolyLine::TPolyLine(const TPolyLine &polyline) : TObject(polyline), TAttLine(polyline), TAttFill(polyline)
00151 {
00152    // PolyLine copy constructor.
00153 
00154    fN = 0;
00155    fLastPoint = -1;
00156    ((TPolyLine&)polyline).Copy(*this);
00157 }
00158 
00159 
00160 //______________________________________________________________________________
00161 void TPolyLine::Copy(TObject &obj) const
00162 {
00163    // Copy this polyline to polyline.
00164 
00165    TObject::Copy(obj);
00166    TAttLine::Copy(((TPolyLine&)obj));
00167    TAttFill::Copy(((TPolyLine&)obj));
00168    ((TPolyLine&)obj).fN = fN;
00169    if (fN > 0) {
00170       ((TPolyLine&)obj).fX = new Double_t[fN];
00171       ((TPolyLine&)obj).fY = new Double_t[fN];
00172       for (Int_t i=0; i<fN;i++)  {((TPolyLine&)obj).fX[i] = fX[i]; ((TPolyLine&)obj).fY[i] = fY[i];}
00173    } else {
00174       ((TPolyLine&)obj).fX = 0;
00175       ((TPolyLine&)obj).fY = 0;
00176    }
00177    ((TPolyLine&)obj).fOption = fOption;
00178    ((TPolyLine&)obj).fLastPoint = fLastPoint;
00179 }
00180 
00181 
00182 //______________________________________________________________________________
00183 Int_t TPolyLine::DistancetoPrimitive(Int_t px, Int_t py)
00184 {
00185    // Returns closest distance in pixels from point (px, py) to a polyline.
00186    //
00187    // First looks for distances to the points of the polyline.  Stops search
00188    // and returns if a vertex of the polyline is found to be closer than 10
00189    // pixels.  Thus the return value may depend on the ordering of points
00190    // in the polyline.
00191    //
00192    // Then looks for distances to the lines of the polyline.  There is no
00193    // arbitrary cutoff; any distance may be found.
00194    //
00195    // Finally checks whether (px, py) is inside a closed and filled polyline.
00196    // (Must be EXACTLY closed.  "Filled" means fill color and fill style are
00197    // both non-zero.) If so, returns zero.
00198    //
00199    // Returns 9999 if the polyline has no points.
00200 
00201    const Int_t big = 9999;
00202    const Int_t kMaxDiff = 10;
00203 
00204    // check if point is near one of the points
00205    Int_t i, pxp, pyp, d;
00206    Int_t distance = big;
00207    if (Size() <= 0) return distance;
00208 
00209    for (i=0;i<Size();i++) {
00210       pxp = gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
00211       pyp = gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
00212       d   = TMath::Abs(pxp-px) + TMath::Abs(pyp-py);
00213       if (d < distance) distance = d;
00214    }
00215    if (distance < kMaxDiff) return distance;
00216 
00217    // check if point is near one of the connecting lines
00218    for (i=0;i<Size()-1;i++) {
00219       d = DistancetoLine(px, py, gPad->XtoPad(fX[i]), gPad->YtoPad(fY[i]), gPad->XtoPad(fX[i+1]), gPad->YtoPad(fY[i+1]));
00220       if (d < distance) distance = d;
00221    }
00222 
00223    // in case of a closed and filled polyline, check if we are inside
00224    if (fFillColor && fFillStyle && fX[0] == fX[fLastPoint] && fY[0] == fY[fLastPoint]) {
00225       if (TMath::IsInside(gPad->AbsPixeltoX(px),gPad->AbsPixeltoY(py),fLastPoint+1,fX,fY)) distance = 0;
00226    }
00227    return distance;
00228 }
00229 
00230 
00231 //______________________________________________________________________________
00232 void TPolyLine::Draw(Option_t *option)
00233 {
00234    // Draw this polyline with its current attributes.
00235 
00236    AppendPad(option);
00237 }
00238 
00239 
00240 //______________________________________________________________________________
00241 void TPolyLine::DrawPolyLine(Int_t n, Double_t *x, Double_t *y, Option_t *option)
00242 {
00243 // Draw this polyline with new coordinates.
00244 
00245    TPolyLine *newpolyline = new TPolyLine(n,x,y);
00246    TAttLine::Copy(*newpolyline);
00247    TAttFill::Copy(*newpolyline);
00248    newpolyline->fOption = fOption;
00249    newpolyline->SetBit(kCanDelete);
00250    newpolyline->AppendPad(option);
00251 }
00252 
00253 
00254 //______________________________________________________________________________
00255 void TPolyLine::ExecuteEvent(Int_t event, Int_t px, Int_t py)
00256 {
00257    // Execute action corresponding to one event.
00258    //
00259    //  This member function is called when a polyline is clicked with the locator
00260    //
00261    //  If Left button clicked on one of the line end points, this point
00262    //     follows the cursor until button is released.
00263    //
00264    //  if Middle button clicked, the line is moved parallel to itself
00265    //     until the button is released.
00266 
00267    Int_t i, d;
00268    Double_t xmin, xmax, ymin, ymax, dx, dy, dxr, dyr;
00269    const Int_t kMaxDiff = 10;
00270    static Bool_t middle;
00271    static Int_t ipoint, pxp, pyp;
00272    static Int_t px1,px2,py1,py2;
00273    static Int_t pxold, pyold, px1old, py1old, px2old, py2old;
00274    static Int_t dpx, dpy;
00275    static Int_t *x=0, *y=0;
00276 
00277    if (!gPad->IsEditable()) return;
00278 
00279    Int_t np = Size();
00280 
00281    switch (event) {
00282 
00283    case kButton1Down:
00284       gVirtualX->SetLineColor(-1);
00285       TAttLine::Modify();  //Change line attributes only if necessary
00286       px1 = gPad->XtoAbsPixel(gPad->GetX1());
00287       py1 = gPad->YtoAbsPixel(gPad->GetY1());
00288       px2 = gPad->XtoAbsPixel(gPad->GetX2());
00289       py2 = gPad->YtoAbsPixel(gPad->GetY2());
00290       ipoint = -1;
00291 
00292 
00293       if (x || y) break;
00294       x = new Int_t[np+1];
00295       y = new Int_t[np+1];
00296       for (i=0;i<np;i++) {
00297          pxp = gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
00298          pyp = gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
00299          gVirtualX->DrawLine(pxp-4, pyp-4, pxp+4,  pyp-4);
00300          gVirtualX->DrawLine(pxp+4, pyp-4, pxp+4,  pyp+4);
00301          gVirtualX->DrawLine(pxp+4, pyp+4, pxp-4,  pyp+4);
00302          gVirtualX->DrawLine(pxp-4, pyp+4, pxp-4,  pyp-4);
00303          x[i] = pxp;
00304          y[i] = pyp;
00305          d   = TMath::Abs(pxp-px) + TMath::Abs(pyp-py);
00306          if (d < kMaxDiff) ipoint =i;
00307       }
00308       dpx = 0;
00309       dpy = 0;
00310       pxold = px;
00311       pyold = py;
00312       if (ipoint < 0) return;
00313       if (ipoint == 0) {
00314          px1old = 0;
00315          py1old = 0;
00316          px2old = gPad->XtoAbsPixel(fX[1]);
00317          py2old = gPad->YtoAbsPixel(fY[1]);
00318       } else if (ipoint == fN-1) {
00319          px1old = gPad->XtoAbsPixel(gPad->XtoPad(fX[fN-2]));
00320          py1old = gPad->YtoAbsPixel(gPad->YtoPad(fY[fN-2]));
00321          px2old = 0;
00322          py2old = 0;
00323       } else {
00324          px1old = gPad->XtoAbsPixel(gPad->XtoPad(fX[ipoint-1]));
00325          py1old = gPad->YtoAbsPixel(gPad->YtoPad(fY[ipoint-1]));
00326          px2old = gPad->XtoAbsPixel(gPad->XtoPad(fX[ipoint+1]));
00327          py2old = gPad->YtoAbsPixel(gPad->YtoPad(fY[ipoint+1]));
00328       }
00329       pxold = gPad->XtoAbsPixel(gPad->XtoPad(fX[ipoint]));
00330       pyold = gPad->YtoAbsPixel(gPad->YtoPad(fY[ipoint]));
00331 
00332       break;
00333 
00334 
00335    case kMouseMotion:
00336 
00337       middle = kTRUE;
00338       for (i=0;i<np;i++) {
00339          pxp = gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
00340          pyp = gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
00341          d   = TMath::Abs(pxp-px) + TMath::Abs(pyp-py);
00342          if (d < kMaxDiff) middle = kFALSE;
00343       }
00344 
00345 
00346    // check if point is close to an axis
00347       if (middle) gPad->SetCursor(kMove);
00348       else gPad->SetCursor(kHand);
00349       break;
00350 
00351    case kButton1Motion:
00352       if (middle) {
00353          for(i=0;i<np-1;i++) {
00354             gVirtualX->DrawLine(x[i]+dpx, y[i]+dpy, x[i+1]+dpx, y[i+1]+dpy);
00355             pxp = x[i]+dpx;
00356             pyp = y[i]+dpy;
00357             gVirtualX->DrawLine(pxp-4, pyp-4, pxp+4,  pyp-4);
00358             gVirtualX->DrawLine(pxp+4, pyp-4, pxp+4,  pyp+4);
00359             gVirtualX->DrawLine(pxp+4, pyp+4, pxp-4,  pyp+4);
00360             gVirtualX->DrawLine(pxp-4, pyp+4, pxp-4,  pyp-4);
00361          }
00362          pxp = x[np-1]+dpx;
00363          pyp = y[np-1]+dpy;
00364          gVirtualX->DrawLine(pxp-4, pyp-4, pxp+4,  pyp-4);
00365          gVirtualX->DrawLine(pxp+4, pyp-4, pxp+4,  pyp+4);
00366          gVirtualX->DrawLine(pxp+4, pyp+4, pxp-4,  pyp+4);
00367          gVirtualX->DrawLine(pxp-4, pyp+4, pxp-4,  pyp-4);
00368          dpx += px - pxold;
00369          dpy += py - pyold;
00370          pxold = px;
00371          pyold = py;
00372          for(i=0;i<np-1;i++) {
00373             gVirtualX->DrawLine(x[i]+dpx, y[i]+dpy, x[i+1]+dpx, y[i+1]+dpy);
00374             pxp = x[i]+dpx;
00375             pyp = y[i]+dpy;
00376             gVirtualX->DrawLine(pxp-4, pyp-4, pxp+4,  pyp-4);
00377             gVirtualX->DrawLine(pxp+4, pyp-4, pxp+4,  pyp+4);
00378             gVirtualX->DrawLine(pxp+4, pyp+4, pxp-4,  pyp+4);
00379             gVirtualX->DrawLine(pxp-4, pyp+4, pxp-4,  pyp-4);
00380          }
00381          pxp = x[np-1]+dpx;
00382          pyp = y[np-1]+dpy;
00383          gVirtualX->DrawLine(pxp-4, pyp-4, pxp+4,  pyp-4);
00384          gVirtualX->DrawLine(pxp+4, pyp-4, pxp+4,  pyp+4);
00385          gVirtualX->DrawLine(pxp+4, pyp+4, pxp-4,  pyp+4);
00386          gVirtualX->DrawLine(pxp-4, pyp+4, pxp-4,  pyp-4);
00387       } else {
00388          if (px1old) gVirtualX->DrawLine(px1old, py1old, pxold,  pyold);
00389          if (px2old) gVirtualX->DrawLine(pxold,  pyold,  px2old, py2old);
00390          gVirtualX->DrawLine(pxold-4, pyold-4, pxold+4,  pyold-4);
00391          gVirtualX->DrawLine(pxold+4, pyold-4, pxold+4,  pyold+4);
00392          gVirtualX->DrawLine(pxold+4, pyold+4, pxold-4,  pyold+4);
00393          gVirtualX->DrawLine(pxold-4, pyold+4, pxold-4,  pyold-4);
00394          pxold = px;
00395          pxold = TMath::Max(pxold, px1);
00396          pxold = TMath::Min(pxold, px2);
00397          pyold = py;
00398          pyold = TMath::Max(pyold, py2);
00399          pyold = TMath::Min(pyold, py1);
00400          if (px1old) gVirtualX->DrawLine(px1old, py1old, pxold,  pyold);
00401          if (px2old) gVirtualX->DrawLine(pxold,  pyold,  px2old, py2old);
00402          gVirtualX->DrawLine(pxold-4, pyold-4, pxold+4,  pyold-4);
00403          gVirtualX->DrawLine(pxold+4, pyold-4, pxold+4,  pyold+4);
00404          gVirtualX->DrawLine(pxold+4, pyold+4, pxold-4,  pyold+4);
00405          gVirtualX->DrawLine(pxold-4, pyold+4, pxold-4,  pyold-4);
00406       }
00407       break;
00408 
00409    case kButton1Up:
00410 
00411    // Compute x,y range
00412       xmin = gPad->GetUxmin();
00413       xmax = gPad->GetUxmax();
00414       ymin = gPad->GetUymin();
00415       ymax = gPad->GetUymax();
00416       dx   = xmax-xmin;
00417       dy   = ymax-ymin;
00418       dxr  = dx/(1 - gPad->GetLeftMargin() - gPad->GetRightMargin());
00419       dyr  = dy/(1 - gPad->GetBottomMargin() - gPad->GetTopMargin());
00420 
00421    // Range() could change the size of the pad pixmap and therefore should
00422    // be called before the other paint routines
00423          gPad->Range(xmin - dxr*gPad->GetLeftMargin(),
00424                      ymin - dyr*gPad->GetBottomMargin(),
00425                      xmax + dxr*gPad->GetRightMargin(),
00426                      ymax + dyr*gPad->GetTopMargin());
00427          gPad->RangeAxis(xmin, ymin, xmax, ymax);
00428 
00429       if (middle) {
00430          for(i=0;i<np;i++) {
00431             fX[i] = gPad->PadtoX(gPad->AbsPixeltoX(x[i]+dpx));
00432             fY[i] = gPad->PadtoY(gPad->AbsPixeltoY(y[i]+dpy));
00433          }
00434       } else {
00435          fX[ipoint] = gPad->PadtoX(gPad->AbsPixeltoX(pxold));
00436          fY[ipoint] = gPad->PadtoY(gPad->AbsPixeltoY(pyold));
00437       }
00438       delete [] x; x = 0;
00439       delete [] y; y = 0;
00440       gPad->Modified(kTRUE);
00441       gVirtualX->SetLineColor(-1);
00442    }
00443 }
00444 
00445 
00446 //______________________________________________________________________________
00447 void TPolyLine::ls(Option_t *) const
00448 {
00449    // List this polyline with its attributes.
00450    // The option string is ignored.
00451 
00452    TROOT::IndentLevel();
00453    printf("TPolyLine  N=%d\n",fN);
00454 }
00455 
00456 
00457 //______________________________________________________________________________
00458 Int_t TPolyLine::Merge(TCollection *li)
00459 {
00460    // Merge polylines in the collection in this polyline
00461 
00462    if (!li) return 0;
00463    TIter next(li);
00464 
00465    //first loop to count the number of entries
00466    TPolyLine *pl;
00467    Int_t npoints = 0;
00468    while ((pl = (TPolyLine*)next())) {
00469       if (!pl->InheritsFrom(TPolyLine::Class())) {
00470          Error("Add","Attempt to add object of class: %s to a %s",pl->ClassName(),this->ClassName());
00471          return -1;
00472       }
00473       npoints += pl->Size();
00474    }
00475 
00476    //extend this polyline to hold npoints
00477    if (npoints > 1) SetPoint(npoints-1,0,0);
00478 
00479    //merge all polylines
00480    next.Reset();
00481    while ((pl = (TPolyLine*)next())) {
00482       Int_t np = pl->Size();
00483       Double_t *x = pl->GetX();
00484       Double_t *y = pl->GetY();
00485       for (Int_t i=0;i<np;i++) {
00486          SetPoint(i,x[i],y[i]);
00487       }
00488    }
00489 
00490    return npoints;
00491 }
00492 
00493 
00494 //______________________________________________________________________________
00495 void TPolyLine::Paint(Option_t *option)
00496 {
00497    // Paint this polyline with its current attributes.
00498 
00499    if (TestBit(kPolyLineNDC)) {
00500       if (strlen(option) > 0) PaintPolyLineNDC(fLastPoint+1, fX, fY, option);
00501       else                    PaintPolyLineNDC(fLastPoint+1, fX, fY, fOption.Data());
00502    } else {
00503       if (strlen(option) > 0) PaintPolyLine(fLastPoint+1, fX, fY, option);
00504       else                    PaintPolyLine(fLastPoint+1, fX, fY, fOption.Data());
00505    }
00506 }
00507 
00508 
00509 //______________________________________________________________________________
00510 void TPolyLine::PaintPolyLine(Int_t n, Double_t *x, Double_t *y, Option_t *option)
00511 {
00512    // Draw this polyline with new coordinates.
00513    //
00514    //  If option = 'f' or 'F' the fill area is drawn.
00515    //  The default is to draw the lines only.
00516 
00517    if (n <= 0) return;
00518    TAttLine::Modify();  //Change line attributes only if necessary
00519    TAttFill::Modify();  //Change fill area attributes only if necessary
00520    Double_t *xx = x;
00521    Double_t *yy = y;
00522    if (gPad->GetLogx()) {
00523       xx = new Double_t[n];
00524       for (Int_t ix=0;ix<n;ix++) xx[ix] = gPad->XtoPad(x[ix]);
00525    }
00526    if (gPad->GetLogy()) {
00527       yy = new Double_t[n];
00528       for (Int_t iy=0;iy<n;iy++) yy[iy] = gPad->YtoPad(y[iy]);
00529    }
00530    if (*option == 'f' || *option == 'F') gPad->PaintFillArea(n,xx,yy,option);
00531    else                                  gPad->PaintPolyLine(n,xx,yy,option);
00532    if (x != xx) delete [] xx;
00533    if (y != yy) delete [] yy;
00534 }
00535 
00536 
00537 //______________________________________________________________________________
00538 void TPolyLine::PaintPolyLineNDC(Int_t n, Double_t *x, Double_t *y, Option_t *option)
00539 {
00540    // Draw this polyline with new coordinates in NDC.
00541 
00542    TAttLine::Modify();  //Change line attributes only if necessary
00543    TAttFill::Modify();  //Change fill area attributes only if necessary
00544    gPad->PaintPolyLineNDC(n,x,y,option);
00545 }
00546 
00547 
00548 //______________________________________________________________________________
00549 void TPolyLine::Print(Option_t *) const
00550 {
00551    // Dump this polyline with its attributes.
00552    // The option string is ignored.
00553 
00554    printf("PolyLine  N=%d\n",fN);
00555 }
00556 
00557 
00558 //______________________________________________________________________________
00559 void TPolyLine::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
00560 {
00561    // Save primitive as a C++ statement(s) on output stream out
00562 
00563    char quote = '"';
00564    out<<"   "<<endl;
00565    out<<"   Double_t *dum = 0;"<<endl;
00566    if (gROOT->ClassSaved(TPolyLine::Class())) {
00567       out<<"   ";
00568    } else {
00569       out<<"   TPolyLine *";
00570    }
00571    out<<"pline = new TPolyLine("<<fN<<",dum,dum,"<<quote<<fOption<<quote<<");"<<endl;
00572 
00573    SaveFillAttributes(out,"pline",0,1001);
00574    SaveLineAttributes(out,"pline",1,1,1);
00575 
00576    for (Int_t i=0;i<Size();i++) {
00577       out<<"   pline->SetPoint("<<i<<","<<fX[i]<<","<<fY[i]<<");"<<endl;
00578    }
00579    out<<"   pline->Draw("
00580       <<quote<<option<<quote<<");"<<endl;
00581 }
00582 
00583 
00584 //______________________________________________________________________________
00585 void TPolyLine::SetNDC(Bool_t isNDC)
00586 {
00587    // Set NDC mode on if isNDC = kTRUE, off otherwise
00588 
00589    ResetBit(kPolyLineNDC);
00590    if (isNDC) SetBit(kPolyLineNDC);
00591 }
00592 
00593 
00594 //______________________________________________________________________________
00595 Int_t TPolyLine::SetNextPoint(Double_t x, Double_t y)
00596 {
00597    // Set point following LastPoint to x, y.
00598    // Returns index of the point (new last point).
00599 
00600    fLastPoint++;
00601    SetPoint(fLastPoint, x, y);
00602    return fLastPoint;
00603 }
00604 
00605 
00606 //______________________________________________________________________________
00607 void TPolyLine::SetPoint(Int_t n, Double_t x, Double_t y)
00608 {
00609    // Set point number n to (x, y)
00610    // If n is greater than the current size, the arrays are automatically
00611    // extended.
00612 
00613    if (n < 0) return;
00614    if (!fX || !fY || n >= fN) {
00615       // re-allocate the object
00616       Int_t newN = TMath::Max(2*fN,n+1);
00617       Double_t *savex = new Double_t [newN];
00618       Double_t *savey = new Double_t [newN];
00619       if (fX && fN){
00620          memcpy(savex,fX,fN*sizeof(Double_t));
00621          memset(&savex[fN],0,(newN-fN)*sizeof(Double_t));
00622          delete [] fX;
00623       }
00624       if (fY && fN){
00625          memcpy(savey,fY,fN*sizeof(Double_t));
00626          memset(&savey[fN],0,(newN-fN)*sizeof(Double_t));
00627          delete [] fY;
00628       }
00629       fX = savex;
00630       fY = savey;
00631       fN = newN;
00632    }
00633    fX[n] = x;
00634    fY[n] = y;
00635    fLastPoint = TMath::Max(fLastPoint,n);
00636 }
00637 
00638 
00639 //______________________________________________________________________________
00640 void TPolyLine::SetPolyLine(Int_t n)
00641 {
00642    // Resize this polyline to size n.
00643    // If n <= 0 the current arrays of points are deleted.
00644    // If n is greater than the current size, the new points are set to (0, 0)
00645 
00646    if (n <= 0) {
00647       fN = 0;
00648       fLastPoint = -1;
00649       delete [] fX;
00650       delete [] fY;
00651       fX = fY = 0;
00652       return;
00653    }
00654    if (n < fN) {
00655       fN = n;
00656       fLastPoint = n - 1;
00657    } else {
00658       SetPoint(n-1,0,0);
00659    }
00660 }
00661 
00662 
00663 //______________________________________________________________________________
00664 void TPolyLine::SetPolyLine(Int_t n, Float_t *x, Float_t *y, Option_t *option)
00665 {
00666    // Set new values for this polyline (single precision).
00667    //
00668    // If n <= 0 the current arrays of points are deleted.
00669 
00670    if (n <= 0) {
00671       fN = 0;
00672       fLastPoint = -1;
00673       delete [] fX;
00674       delete [] fY;
00675       fX = fY = 0;
00676       return;
00677    }
00678    fN =n;
00679    if (fX) delete [] fX;
00680    if (fY) delete [] fY;
00681    fX = new Double_t[fN];
00682    fY = new Double_t[fN];
00683    for (Int_t i=0; i<fN;i++) {
00684       if (x) fX[i] = (Double_t)x[i];
00685       if (y) fY[i] = (Double_t)y[i];
00686    }
00687    fOption = option;
00688    fLastPoint = fN-1;
00689 }
00690 
00691 
00692 //______________________________________________________________________________
00693 void TPolyLine::SetPolyLine(Int_t n, Double_t *x, Double_t *y, Option_t *option)
00694 {
00695    // Set new values for this polyline (double precision).
00696    //
00697    // If n <= 0 the current arrays of points are deleted.
00698 
00699    if (n <= 0) {
00700       fN = 0;
00701       fLastPoint = -1;
00702       delete [] fX;
00703       delete [] fY;
00704       fX = fY = 0;
00705       return;
00706    }
00707    fN =n;
00708    if (fX) delete [] fX;
00709    if (fY) delete [] fY;
00710    fX = new Double_t[fN];
00711    fY = new Double_t[fN];
00712    for (Int_t i=0; i<fN;i++) {
00713       if (x) fX[i] = x[i];
00714       if (y) fY[i] = y[i];
00715    }
00716    fOption = option;
00717    fLastPoint = fN-1;
00718 }
00719 
00720 
00721 //_______________________________________________________________________
00722 void TPolyLine::Streamer(TBuffer &b)
00723 {
00724    // Stream a class object.
00725 
00726    if (b.IsReading()) {
00727       UInt_t R__s, R__c;
00728       Version_t R__v = b.ReadVersion(&R__s, &R__c);
00729       if (R__v > 1) {
00730          b.ReadClassBuffer(TPolyLine::Class(), this, R__v, R__s, R__c);
00731          return;
00732       }
00733       //====process old versions before automatic schema evolution
00734       TObject::Streamer(b);
00735       TAttLine::Streamer(b);
00736       TAttFill::Streamer(b);
00737       b >> fN;
00738       fX = new Double_t[fN];
00739       fY = new Double_t[fN];
00740       Float_t *x = new Float_t[fN];
00741       Float_t *y = new Float_t[fN];
00742       b.ReadFastArray(x,fN);
00743       b.ReadFastArray(y,fN);
00744       for (Int_t i=0;i<fN;i++) {
00745          fX[i] = x[i];
00746          fY[i] = y[i];
00747       }
00748       fOption.Streamer(b);
00749       b.CheckByteCount(R__s, R__c, TPolyLine::IsA());
00750       //====end of old versions
00751 
00752    } else {
00753       b.WriteClassBuffer(TPolyLine::Class(),this);
00754    }
00755 }

Generated on Tue Jul 5 14:14:28 2011 for ROOT_528-00b_version by  doxygen 1.5.1