Logo ROOT  
Reference Guide
TArrow.cxx
Go to the documentation of this file.
1 // @(#)root/graf:$Id$
2 // Author: Rene Brun 17/10/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include <iostream>
13 #include "TROOT.h"
14 #include "TMath.h"
15 #include "TArrow.h"
16 #include "TVirtualPad.h"
17 #include "TVirtualPS.h"
18 #include "TVirtualX.h"
19 
23 
25 
26 /** \class TArrow
27 \ingroup BasicGraphics
28 
29 Draw all kinds of Arrows.
30 
31 The different arrow's formats are explained in TArrow::TArrow.
32 The picture below gives some examples.
33 
34 Once an arrow is drawn on the screen:
35 
36 - One can click on one of the edges and move this edge.
37 - One can click on any other arrow part to move the entire arrow.
38 
39 Begin_Macro(source)
40 ../../../tutorials/graphics/arrows.C
41 End_Macro
42 */
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// Arrow default constructor.
46 
48 {
50  fArrowSize = 0.;
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Arrow normal constructor.
55 ///
56 /// Define an arrow between points x1,y1 and x2,y2
57 /// the `arrowsize` is in percentage of the pad height
58 /// Opening angle between the two sides of the arrow is fAngle (60 degrees)
59 /// ~~~ {.cpp}
60 /// option = ">" -------->
61 /// option = "|->" |------->
62 /// option = "<" <--------
63 /// option = "<-|" <-------|
64 /// option = "->-" ---->----
65 /// option = "-<-" ----<----
66 /// option = "-|>-" ---|>----
67 /// option = "<>" <------->
68 /// option = "<|>" <|-----|> arrow defined by a triangle
69 /// ~~~
70 /// Note:
71 ///
72 /// - If FillColor == 0 an open triangle is drawn, otherwise a full triangle is
73 /// drawn with the fill color. The default is filled with LineColor
74 /// - The "Begin" and "end" bars options can be combined with any other options.
75 /// - The 9 options described above cannot be mixed.
76 
78  Float_t arrowsize ,Option_t *option)
79  :TLine(x1,y1,x2,y2), TAttFill(0,1001)
80 {
81 
83  fArrowSize = arrowsize;
84  fOption = option;
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 /// Arrow default destructor.
90 
92 {
93 }
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// Copy constructor.
97 
99 {
101  fArrowSize = 0.;
102  arrow.Copy(*this);
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Copy this arrow to arrow
107 
108 void TArrow::Copy(TObject &obj) const
109 {
110  TLine::Copy(obj);
111  TAttFill::Copy(((TArrow&)obj));
112  ((TArrow&)obj).fAngle = fAngle;
113  ((TArrow&)obj).fArrowSize = fArrowSize;
114  ((TArrow&)obj).fOption = fOption;
115 }
116 
117 ////////////////////////////////////////////////////////////////////////////////
118 /// Draw this arrow with its current attributes.
119 
120 void TArrow::Draw(Option_t *option)
121 {
122  Option_t *opt;
123  if (option && strlen(option)) opt = option;
124  else opt = (char*)GetOption();
125 
126  AppendPad(opt);
127 }
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// Draw this arrow with new coordinates.
131 ///
132 /// - if `arrowsize` is <= 0, `arrowsize` will be the current arrow size
133 /// - if `option=""`, `option` will be the current arrow option
134 
136  Float_t arrowsize ,Option_t *option)
137 {
138 
139  Float_t size = arrowsize;
140  if (size <= 0) size = fArrowSize;
141  if (size <= 0) size = 0.05;
142  const char* opt = option;
143  if (!opt || !opt[0]) opt = fOption.Data();
144  if (!opt || !opt[0]) opt = "|>";
145  TArrow *newarrow = new TArrow(x1,y1,x2,y2,size,opt);
146  newarrow->SetAngle(fAngle);
147  TAttLine::Copy(*newarrow);
148  TAttFill::Copy(*newarrow);
149  newarrow->SetBit(kCanDelete);
150  newarrow->AppendPad(opt);
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Paint this arrow with its current attributes.
155 
156 void TArrow::Paint(Option_t *option)
157 {
158  Option_t *opt;
159  if (option && strlen(option)) opt = option;
160  else opt = (char*)GetOption();
161  if (TestBit(kLineNDC))
162  PaintArrow(gPad->GetX1() + fX1 * (gPad->GetX2() - gPad->GetX1()),
163  gPad->GetY1() + fY1 * (gPad->GetY2() - gPad->GetY1()),
164  gPad->GetX1() + fX2 * (gPad->GetX2() - gPad->GetX1()),
165  gPad->GetY1() + fY2 * (gPad->GetY2() - gPad->GetY1()), fArrowSize, opt);
166  else
167  PaintArrow(gPad->XtoPad(fX1), gPad->YtoPad(fY1), gPad->XtoPad(fX2), gPad->YtoPad(fY2), fArrowSize, opt);
168 }
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 /// Draw this arrow
172 
174  Float_t arrowsize, Option_t *option)
175 {
176 
177  // Option and attributes
178  TString opt = option;
179  opt.ToLower();
182 
183  // Compute the gPad coordinates in TRUE normalized space (NDC)
184  Int_t iw = gPad->GetWw();
185  Int_t ih = gPad->GetWh();
186  Double_t x1p,y1p,x2p,y2p;
187  gPad->GetPadPar(x1p,y1p,x2p,y2p);
188  Int_t ix1 = (Int_t)(iw*x1p);
189  Int_t iy1 = (Int_t)(ih*y1p);
190  Int_t ix2 = (Int_t)(iw*x2p);
191  Int_t iy2 = (Int_t)(ih*y2p);
192  Double_t wndc = TMath::Min(1.,(Double_t)iw/(Double_t)ih);
193  Double_t hndc = TMath::Min(1.,(Double_t)ih/(Double_t)iw);
194  Double_t rh = hndc/(Double_t)ih;
195  Double_t rw = wndc/(Double_t)iw;
196  Double_t x1ndc = (Double_t)ix1*rw;
197  Double_t y1ndc = (Double_t)iy1*rh;
198  Double_t x2ndc = (Double_t)ix2*rw;
199  Double_t y2ndc = (Double_t)iy2*rh;
200 
201  // Ratios to convert user space in TRUE normalized space (NDC)
202  Double_t rx1,ry1,rx2,ry2;
203  gPad->GetRange(rx1,ry1,rx2,ry2);
204  Double_t rx = (x2ndc-x1ndc)/(rx2-rx1);
205  Double_t ry = (y2ndc-y1ndc)/(ry2-ry1);
206 
207  // Arrow position and arrow's middle in NDC space
208  Double_t x1n = rx*(x1-rx1)+x1ndc;
209  Double_t x2n = rx*(x2-rx1)+x1ndc;
210  Double_t y1n = ry*(y1-ry1)+y1ndc;
211  Double_t y2n = ry*(y2-ry1)+y1ndc;
212  Double_t xm = (x1n+x2n)/2;
213  Double_t ym = (y1n+y2n)/2;
214 
215  // Arrow heads size
216  Double_t length = TMath::Sqrt(Double_t((x2n-x1n)*(x2n-x1n)+(y2n-y1n)*(y2n-y1n)));
217  Double_t rSize = 0.7*arrowsize;
218  Double_t dSize = rSize*TMath::Tan(TMath::Pi()*fAngle/360);
219  Double_t cosT = (length > 0) ? (x2n-x1n)/length : 1.;
220  Double_t sinT = (length > 0) ? (y2n-y1n)/length : 0.;
221 
222  // Draw the start and end bars if needed
223  if (opt.BeginsWith("|-")) {
224  Double_t x1ar[2], y1ar[2];
225  x1ar[0] = x1n-sinT*dSize;
226  y1ar[0] = y1n+cosT*dSize;
227  x1ar[1] = x1n+sinT*dSize;
228  y1ar[1] = y1n-cosT*dSize;
229  // NDC to user coordinates
230  for (Int_t i=0; i<2; i++) {
231  x1ar[i] = (1/rx)*(x1ar[i]-x1ndc)+rx1;
232  y1ar[i] = (1/ry)*(y1ar[i]-y1ndc)+ry1;
233  }
234  gPad->PaintLine(x1ar[0],y1ar[0],x1ar[1],y1ar[1]);
235  opt(0) = ' ';
236  }
237  if (opt.EndsWith("-|")) {
238  Double_t x2ar[2], y2ar[2];
239  x2ar[0] = x2n-sinT*dSize;
240  y2ar[0] = y2n+cosT*dSize;
241  x2ar[1] = x2n+sinT*dSize;
242  y2ar[1] = y2n-cosT*dSize;
243  // NDC to user coordinates
244  for (Int_t i=0; i<2; i++) {
245  x2ar[i] = (1/rx)*(x2ar[i]-x1ndc)+rx1;
246  y2ar[i] = (1/ry)*(y2ar[i]-y1ndc)+ry1;
247  }
248  gPad->PaintLine(x2ar[0],y2ar[0],x2ar[1],y2ar[1]);
249  opt(opt.Length()-1) = ' ';
250  }
251 
252  // Move arrow head's position if needed
253  Double_t x1h = x1n;
254  Double_t y1h = y1n;
255  Double_t x2h = x2n;
256  Double_t y2h = y2n;
257  if (opt.Contains("->-") || opt.Contains("-|>-")) {
258  x2h = xm + cosT*rSize/2;
259  y2h = ym + sinT*rSize/2;
260  }
261  if (opt.Contains("-<-") || opt.Contains("-<|-")) {
262  x1h = xm - cosT*rSize/2;
263  y1h = ym - sinT*rSize/2;
264  }
265 
266  // Paint Arrow body
267  if (opt.Contains("|>") && !opt.Contains("-|>-")) {
268  x2n -= cosT*rSize;
269  y2n -= sinT*rSize;
270  }
271  if (opt.Contains("<|") && !opt.Contains("-<|-")) {
272  x1n += cosT*rSize;
273  y1n += sinT*rSize;
274  }
275  x1n = (1/rx)*(x1n-x1ndc)+rx1;
276  y1n = (1/ry)*(y1n-y1ndc)+ry1;
277  x2n = (1/rx)*(x2n-x1ndc)+rx1;
278  y2n = (1/ry)*(y2n-y1ndc)+ry1;
279  gPad->PaintLine(x1n,y1n,x2n,y2n);
280 
281  // Draw the arrow's head(s)
282  if (opt.Contains(">")) {
283  Double_t x2ar[4], y2ar[4];
284 
285  x2ar[0] = x2h - rSize*cosT - sinT*dSize;
286  y2ar[0] = y2h - rSize*sinT + cosT*dSize;
287  x2ar[1] = x2h;
288  y2ar[1] = y2h;
289  x2ar[2] = x2h - rSize*cosT + sinT*dSize;
290  y2ar[2] = y2h - rSize*sinT - cosT*dSize;
291  x2ar[3] = x2ar[0];
292  y2ar[3] = y2ar[0];
293 
294  // NDC to user coordinates
295  for (Int_t i=0; i<4; i++) {
296  x2ar[i] = (1/rx)*(x2ar[i]-x1ndc)+rx1;
297  y2ar[i] = (1/ry)*(y2ar[i]-y1ndc)+ry1;
298  }
299  if (opt.Contains("|>")) {
300  if (gVirtualX) gVirtualX->SetLineStyle(1);
302  if (GetFillColor()) {
303  gPad->PaintFillArea(3,x2ar,y2ar);
304  gPad->PaintPolyLine(4,x2ar,y2ar);
305  } else {
306  gPad->PaintPolyLine(4,x2ar,y2ar);
307  }
308  } else {
309  gPad->PaintPolyLine(3,x2ar,y2ar);
310  }
311  }
312 
313  if (opt.Contains("<")) {
314  Double_t x1ar[4], y1ar[4];
315  x1ar[0] = x1h + rSize*cosT + sinT*dSize;
316  y1ar[0] = y1h + rSize*sinT - cosT*dSize;
317  x1ar[1] = x1h;
318  y1ar[1] = y1h;
319  x1ar[2] = x1h + rSize*cosT - sinT*dSize;
320  y1ar[2] = y1h + rSize*sinT + cosT*dSize;
321  x1ar[3] = x1ar[0];
322  y1ar[3] = y1ar[0];
323 
324  // NDC to user coordinates
325  for (Int_t i=0; i<4; i++) {
326  x1ar[i] = (1/rx)*(x1ar[i]-x1ndc)+rx1;
327  y1ar[i] = (1/ry)*(y1ar[i]-y1ndc)+ry1;
328  }
329  if (opt.Contains("<|")) {
330  if (gVirtualX) gVirtualX->SetLineStyle(1);
332  if (GetFillColor()) {
333  gPad->PaintFillArea(3,x1ar,y1ar);
334  gPad->PaintPolyLine(4,x1ar,y1ar);
335  } else {
336  gPad->PaintPolyLine(4,x1ar,y1ar);
337  }
338  } else {
339  gPad->PaintPolyLine(3,x1ar,y1ar);
340  }
341  }
342 }
343 
344 ////////////////////////////////////////////////////////////////////////////////
345 /// Save primitive as a C++ statement(s) on output stream out
346 
347 void TArrow::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
348 {
349 
350  char quote = '"';
351  if (gROOT->ClassSaved(TArrow::Class())) {
352  out<<" ";
353  } else {
354  out<<" TArrow *";
355  }
356  out<<"arrow = new TArrow("<<fX1<<","<<fY1<<","<<fX2<<","<<fY2
357  <<","<<fArrowSize<<","<<quote<<GetDrawOption()<<quote<<");"<<std::endl;
358 
359  SaveFillAttributes(out,"arrow",0,1);
360  SaveLineAttributes(out,"arrow",1,1,1);
361 
362  if (TestBit(kLineNDC))
363  out<<" arrow->SetNDC();"<<std::endl;
364 
365  if (fAngle!=60)
366  out << " arrow->SetAngle(" << GetAngle() << ");" << std::endl;
367 
368  out<<" arrow->Draw();"<<std::endl;
369 }
370 
371 
372 ////////////////////////////////////////////////////////////////////////////////
373 /// Set default angle.
374 
376 {
378 }
379 
380 
381 ////////////////////////////////////////////////////////////////////////////////
382 /// Set default arrow sive.
383 
385 {
386  fgDefaultArrowSize = ArrowSize;
387 }
388 
389 
390 ////////////////////////////////////////////////////////////////////////////////
391 /// Set default option.
392 
394 {
395 
396  fgDefaultOption = Option;
397 }
398 
399 
400 ////////////////////////////////////////////////////////////////////////////////
401 /// Get default angle.
402 
404 {
405 
406  return fgDefaultAngle;
407 }
408 
409 
410 ////////////////////////////////////////////////////////////////////////////////
411 /// Get default arrow size.
412 
414 {
415 
416  return fgDefaultArrowSize;
417 }
418 
419 
420 ////////////////////////////////////////////////////////////////////////////////
421 /// Get default option.
422 
424 {
425 
426  return fgDefaultOption.Data();
427 }
TArrow.h
TArrow::SetDefaultArrowSize
static void SetDefaultArrowSize(Float_t ArrowSize)
Set default arrow sive.
Definition: TArrow.cxx:384
TArrow::SetDefaultAngle
static void SetDefaultAngle(Float_t Angle)
Set default angle.
Definition: TArrow.cxx:375
TLine::fX1
Double_t fX1
X of 1st point.
Definition: TLine.h:25
TObject::TestBit
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
TArrow::fgDefaultArrowSize
static Float_t fgDefaultArrowSize
Default Arrow Size.
Definition: TArrow.h:36
TLine
A simple line.
Definition: TLine.h:22
Option_t
const char Option_t
Definition: RtypesCore.h:66
TLine::fX2
Double_t fX2
X of 2nd point.
Definition: TLine.h:27
TArrow::PaintArrow
virtual void PaintArrow(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Float_t arrowsize=0.05, Option_t *option=">")
Draw this arrow.
Definition: TArrow.cxx:173
TAttFill::Modify
virtual void Modify()
Change current fill area attributes if necessary.
Definition: TAttFill.cxx:211
gVirtualX
#define gVirtualX
Definition: TVirtualX.h:338
TString::Data
const char * Data() const
Definition: TString.h:369
TArrow::GetOption
Option_t * GetOption() const
Definition: TArrow.h:53
TArrow::SetAngle
virtual void SetAngle(Float_t angle=60)
Definition: TArrow.h:58
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TArrow::Draw
virtual void Draw(Option_t *option="")
Draw this arrow with its current attributes.
Definition: TArrow.cxx:120
TArrow::fAngle
Float_t fAngle
Arrow opening angle (degrees)
Definition: TArrow.h:31
TMath::Sqrt
Double_t Sqrt(Double_t x)
Definition: TMath.h:691
TMath::Tan
Double_t Tan(Double_t)
Definition: TMath.h:647
Float_t
float Float_t
Definition: RtypesCore.h:57
Int_t
int Int_t
Definition: RtypesCore.h:45
TObject::AppendPad
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition: TObject.cxx:107
TString::Contains
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
TString::EndsWith
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2177
TAttLine::SaveLineAttributes
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition: TAttLine.cxx:270
TString::Length
Ssiz_t Length() const
Definition: TString.h:410
TVirtualPS.h
TVirtualX.h
ROOT::Math::VectorUtil::Angle
double Angle(const Vector1 &v1, const Vector2 &v2)
Find Angle between two vectors.
Definition: VectorUtil.h:138
TArrow::SavePrimitive
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TArrow.cxx:347
TArrow::GetAngle
Float_t GetAngle() const
Definition: TArrow.h:51
TString
Basic string class.
Definition: TString.h:136
TAttFill::SaveFillAttributes
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition: TAttFill.cxx:234
TArrow::TArrow
TArrow()
Arrow default constructor.
Definition: TArrow.cxx:47
x1
static const double x1[5]
Definition: RooGaussKronrodIntegrator1D.cxx:346
TROOT.h
TAttLine::Modify
virtual void Modify()
Change current line attributes if necessary.
Definition: TAttLine.cxx:242
TArrow::fOption
TString fOption
Arrow shapes.
Definition: TArrow.h:33
TAttLine::Copy
void Copy(TAttLine &attline) const
Copy this line attributes to a new TAttLine.
Definition: TAttLine.cxx:172
TMath::Pi
constexpr Double_t Pi()
Definition: TMath.h:37
TAttFill::Copy
void Copy(TAttFill &attfill) const
Copy this fill attributes to a new TAttFill.
Definition: TAttFill.cxx:202
TLine::fY1
Double_t fY1
Y of 1st point.
Definition: TLine.h:26
TArrow::fArrowSize
Float_t fArrowSize
Arrow Size.
Definition: TArrow.h:32
TArrow::GetDefaultAngle
static Float_t GetDefaultAngle()
Get default angle.
Definition: TArrow.cxx:403
TAttLine::GetLineColor
virtual Color_t GetLineColor() const
Return the line color.
Definition: TAttLine.h:33
TObject::SetBit
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:696
TObject::GetDrawOption
virtual Option_t * GetDrawOption() const
Get option used by the graphics system to draw this object.
Definition: TObject.cxx:343
TArrow::GetDefaultArrowSize
static Float_t GetDefaultArrowSize()
Get default arrow size.
Definition: TArrow.cxx:413
TArrow::DrawArrow
virtual void DrawArrow(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Float_t arrowsize=0, Option_t *option="")
Draw this arrow with new coordinates.
Definition: TArrow.cxx:135
TVirtualPad.h
TArrow::GetDefaultOption
static Option_t * GetDefaultOption()
Get default option.
Definition: TArrow.cxx:423
TString::BeginsWith
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:615
TArrow::Copy
void Copy(TObject &arrow) const
Copy this arrow to arrow.
Definition: TArrow.cxx:108
TObject::kCanDelete
@ kCanDelete
if object in a list can be deleted
Definition: TObject.h:58
TMath::Min
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:180
TLine::kLineNDC
@ kLineNDC
Use NDC coordinates.
Definition: TLine.h:33
gVirtualPS
R__EXTERN TVirtualPS * gVirtualPS
Definition: TVirtualPS.h:81
Double_t
double Double_t
Definition: RtypesCore.h:59
TArrow::fgDefaultAngle
static Float_t fgDefaultAngle
Default Arrow opening angle (degrees)
Definition: TArrow.h:35
TArrow::~TArrow
virtual ~TArrow()
Arrow default destructor.
Definition: TArrow.cxx:91
arrow
Definition: RArrowDS.hxx:9
TAttFill::SetFillColor
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
TArrow
Draw all kinds of Arrows.
Definition: TArrow.h:29
TArrow::fgDefaultOption
static TString fgDefaultOption
Default Arrow shapes.
Definition: TArrow.h:37
x2
static const double x2[5]
Definition: RooGaussKronrodIntegrator1D.cxx:364
gPad
#define gPad
Definition: TVirtualPad.h:287
TAttFill::GetFillColor
virtual Color_t GetFillColor() const
Return the fill area color.
Definition: TAttFill.h:30
TArrow::Paint
virtual void Paint(Option_t *option="")
Paint this arrow with its current attributes.
Definition: TArrow.cxx:156
TLine::fY2
Double_t fY2
Y of 2nd point.
Definition: TLine.h:28
TAttFill
Fill Area Attributes class.
Definition: TAttFill.h:19
TArrow::SetDefaultOption
static void SetDefaultOption(Option_t *Option)
Set default option.
Definition: TArrow.cxx:393
Class
void Class()
Definition: Class.C:29
TString::ToLower
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1125
TAttLine::SetLineStyle
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition: TAttLine.h:42
TLine::Copy
void Copy(TObject &line) const
Copy this line to line.
Definition: TLine.cxx:63
TMath.h
gROOT
#define gROOT
Definition: TROOT.h:406
int