<commit_before>// @(#)root/graf:$Name:  $:$Id: TPolyLine.cxx,v 1.14 2002/10/31 07:27:35 brun Exp $
// Author: <NAME>   12/12/94

/*************************************************************************
 * Copyright (C) 1995-2000, <NAME> and <NAME>.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Riostream.h"
#include "TROOT.h"
#include "TVirtualPad.h"
#include "TPolyLine.h"

ClassImp(TPolyLine)

//______________________________________________________________________________
//
//  a PolyLine is defined by an array on N points in a 2-D space.
//
// One can draw the contour of the polyline or/and its fill area.
// Example:
//{
//   Double_t x[5] = {.2,.7,.6,.25,.2};
//   Double_t y[5] = {.5,.1,.9,.7,.5};
//   TPolyLine *pline = new TPolyLine(5,x,y);
//   pline->SetFillColor(38);
//   pline->SetLineColor(2);
//   pline->SetLineWidth(4);
//   pline->Draw("f");
//   pline->Draw();
//}

//______________________________________________________________________________
TPolyLine::TPolyLine(): TObject()
{
//*-*-*-*-*-*-*-*-*-*-*PolyLine default constructor*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  ============================
   fN = 0;
   fX = 0;
   fY = 0;
   fLastPoint = -1;
}

//______________________________________________________________________________
TPolyLine::TPolyLine(Int_t n, Option_t *option)
      :TObject(), TAttLine(), TAttFill()
{
//*-*-*-**-*-*PolyLine normal constructor without initialisation*-*-*-*-*-*-*-*
//*-*         ==================================================
   fLastPoint = -1;
   fN = n;
   fX = new Double_t[fN];
   fY = new Double_t[fN];
   fOption = option;
}

//______________________________________________________________________________
TPolyLine::TPolyLine(Int_t n, Float_t *x, Float_t *y, Option_t *option)
      :TObject(), TAttLine(), TAttFill()
{
//*-*-*-*-*-*-*-*-*-*-*PolyLine normal constructor*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  ===========================
   fLastPoint = -1;
   fN = n;
   fX = new Double_t[fN];
   fY = new Double_t[fN];
   if (!x || !y) return;
   for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i];}
   fLastPoint = fN-1;
   fOption = option;
}

//______________________________________________________________________________
TPolyLine::TPolyLine(Int_t n, Double_t *x, Double_t *y, Option_t *option)
      :TObject(), TAttLine(), TAttFill()
{
//*-*-*-*-*-*-*-*-*-*-*PolyLine normal constructor*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  ===========================
   fLastPoint = -1;
   fN = n;
   fX = new Double_t[fN];
   fY = new Double_t[fN];
   if (!x || !y) return;
   for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i];}
   fLastPoint = fN-1;
   fOption = option;
}

//______________________________________________________________________________
TPolyLine::~TPolyLine()
{
//*-*-*-*-*-*-*-*-*-*-*PolyLine default destructor*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  ===========================
   if (fX) delete [] fX;
   if (fY) delete [] fY;
}

//______________________________________________________________________________
TPolyLine::TPolyLine(const TPolyLine &polyline) : TObject(polyline), TAttLine(polyline), TAttFill(polyline)
{
   ((TPolyLine&)polyline).Copy(*this);
}

//______________________________________________________________________________
void TPolyLine::Copy(TObject &obj) const
{
//*-*-*-*-*-*-*Copy this polyline to polyline*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*          ==============================

   TObject::Copy(obj);
   TAttLine::Copy(((TPolyLine&)obj));
   TAttFill::Copy(((TPolyLine&)obj));
   ((TPolyLine&)obj).fN = fN;
   ((TPolyLine&)obj).fX = new Double_t[fN];
   ((TPolyLine&)obj).fY = new Double_t[fN];
   for (Int_t i=0; i<fN;i++)  {((TPolyLine&)obj).fX[i] = fX[i]; ((TPolyLine&)obj).fY[i] = fY[i];}
   ((TPolyLine&)obj).fOption = fOption;
   ((TPolyLine&)obj).fLastPoint = fLastPoint;
}

//______________________________________________________________________________
Int_t TPolyLine::DistancetoPrimitive(Int_t px, Int_t py)
{
//*-*-*-*-*-*-*-*-*Compute distance from point px,py to a polyline*-*-*-*-*-*
//*-*              ===============================================
//  Compute the closest distance of approach from point px,py to each segment
//  of the polyline.
//  Returns when the distance found is below DistanceMaximum.
//  The distance is computed in pixels units.
//
   const Int_t big = 9999;
   const Int_t kMaxDiff = 10;

//*-*- check if point is near one of the points
   Int_t i, pxp, pyp, d;
   Int_t distance = big;

   for (i=0;i<Size();i++) {
      pxp = gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
      pyp = gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
      d   = TMath::Abs(pxp-px) + TMath::Abs(pyp-py);
      if (d < distance) distance = d;
   }
   if (distance < kMaxDiff) return distance;

//*-*- check if point is near one of the connecting lines
   for (i=0;i<Size()-1;i++) {
      d = DistancetoLine(px, py, gPad->XtoPad(fX[i]), gPad->YtoPad(fY[i]), gPad->XtoPad(fX[i+1]), gPad->YtoPad(fY[i+1]));
      if (d < distance) distance = d;
   }
   return distance;
}

//______________________________________________________________________________<commit_msg>Modify TPolyLine::DistancetoPrimitive to support the case of a filled and closed polyline. In this case the object is picked when the mouse is inside the polygone, otherwise the object is picked only when the mouse is closed to one of lines of the polyline.<commit_after>// @(#)root/graf:$Name:  $:$Id: TPolyLine.cxx,v 1.15 2002/11/05 16:09:15 brun Exp $
// Author: <NAME>   12/12/94

/*************************************************************************
 * Copyright (C) 1995-2000, <NAME> and <NAME>.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Riostream.h"
#include "TROOT.h"
#include "TVirtualPad.h"
#include "TPolyLine.h"

ClassImp(TPolyLine)

//______________________________________________________________________________
//
//  a PolyLine is defined by an array on N points in a 2-D space.
//
// One can draw the contour of the polyline or/and its fill area.
// Example:
//{
//   Double_t x[5] = {.2,.7,.6,.25,.2};
//   Double_t y[5] = {.5,.1,.9,.7,.5};
//   TPolyLine *pline = new TPolyLine(5,x,y);
//   pline->SetFillColor(38);
//   pline->SetLineColor(2);
//   pline->SetLineWidth(4);
//   pline->Draw("f");
//   pline->Draw();
//}

//______________________________________________________________________________
TPolyLine::TPolyLine(): TObject()
{
//*-*-*-*-*-*-*-*-*-*-*PolyLine default constructor*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  ============================
   fN = 0;
   fX = 0;
   fY = 0;
   fLastPoint = -1;
}

//______________________________________________________________________________
TPolyLine::TPolyLine(Int_t n, Option_t *option)
      :TObject(), TAttLine(), TAttFill()
{
//*-*-*-**-*-*PolyLine normal constructor without initialisation*-*-*-*-*-*-*-*
//*-*         ==================================================
   fLastPoint = -1;
   fN = n;
   fX = new Double_t[fN];
   fY = new Double_t[fN];
   fOption = option;
}

//______________________________________________________________________________
TPolyLine::TPolyLine(Int_t n, Float_t *x, Float_t *y, Option_t *option)
      :TObject(), TAttLine(), TAttFill()
{
//*-*-*-*-*-*-*-*-*-*-*PolyLine normal constructor*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  ===========================
   fLastPoint = -1;
   fN = n;
   fX = new Double_t[fN];
   fY = new Double_t[fN];
   if (!x || !y) return;
   for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i];}
   fLastPoint = fN-1;
   fOption = option;
}

//______________________________________________________________________________
TPolyLine::TPolyLine(Int_t n, Double_t *x, Double_t *y, Option_t *option)
      :TObject(), TAttLine(), TAttFill()
{
//*-*-*-*-*-*-*-*-*-*-*PolyLine normal constructor*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  ===========================
   fLastPoint = -1;
   fN = n;
   fX = new Double_t[fN];
   fY = new Double_t[fN];
   if (!x || !y) return;
   for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i];}
   fLastPoint = fN-1;
   fOption = option;
}

//______________________________________________________________________________
TPolyLine::~TPolyLine()
{
//*-*-*-*-*-*-*-*-*-*-*PolyLine default destructor*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  ===========================
   if (fX) delete [] fX;
   if (fY) delete [] fY;
}

//______________________________________________________________________________
TPolyLine::TPolyLine(const TPolyLine &polyline) : TObject(polyline), TAttLine(polyline), TAttFill(polyline)
{
   ((TPolyLine&)polyline).Copy(*this);
}

//______________________________________________________________________________
void TPolyLine::Copy(TObject &obj) const
{
//*-*-*-*-*-*-*Copy this polyline to polyline*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*          ==============================

   TObject::Copy(obj);
   TAttLine::Copy(((TPolyLine&)obj));
   TAttFill::Copy(((TPolyLine&)obj));
   ((TPolyLine&)obj).fN = fN;
   ((TPolyLine&)obj).fX = new Double_t[fN];
   ((TPolyLine&)obj).fY = new Double_t[fN];
   for (Int_t i=0; i<fN;i++)  {((TPolyLine&)obj).fX[i] = fX[i]; ((TPolyLine&)obj).fY[i] = fY[i];}
   ((TPolyLine&)obj).fOption = fOption;
   ((TPolyLine&)obj).fLastPoint = fLastPoint;
}

//______________________________________________________________________________
Int_t TPolyLine::DistancetoPrimitive(Int_t px, Int_t py)
{
//*-*-*-*-*-*-*-*-*Compute distance from point px,py to a polyline*-*-*-*-*-*
//*-*              ===============================================
//  Compute the closest distance of approach from point px,py to each segment
//  of the polyline.
//  Returns when the distance found is below DistanceMaximum.
//  The distance is computed in pixels units.
//
   const Int_t big = 9999;
   const Int_t kMaxDiff = 10;

//*-*- check if point is near one of the points
   Int_t i, pxp, pyp, d;
   Int_t distance = big;

   for (i=0;i<Size();i++) {
      pxp = gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
      pyp = gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
      d   = TMath::Abs(pxp-px) + TMath::Abs(pyp-py);
      if (d < distance) distance = d;
   }
   if (distance < kMaxDiff) return distance;

//*-*- check if point is near one of the connecting lines
   for (i=0;i<Size()-1;i++) {
      d = DistancetoLine(px, py, gPad->XtoPad(fX[i]), gPad->YtoPad(fY[i]), gPad->XtoPad(fX[i+1]), gPad->YtoPad(fY[i+1]));
      if (d < distance) distance = d;
   }

//*-*- in case of a closed and filled polyline, check if we are inside
   if (fFillColor && fFillStyle && fX[0] == fX[fLastPoint] && fY[0] == fY[fLastPoint]) {
      if (TMath::IsInside(gPad->AbsPixeltoX(px),gPad->AbsPixeltoY(py),fLastPoint+1,fX,fY)) distance = 0;
   }
   return distance;
}

//______________________________________________________________________________
{'fraction_non_alphanumeric': 0.2486369536996971, 'fraction_numerical': 0.012981393336218087, 'mean_word_length': 3.2175182481751823, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2934765', 'n_tokens_mistral': 3885, 'n_tokens_neox': 3704, 'n_words': 799}