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