Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "TMath.h"
14#include "TArrow.h"
15#include "TVirtualPad.h"
16#include "TVirtualPS.h"
17#include "TVirtualX.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/visualisation/graphics/arrows.C
40End_Macro
41*/
42
43////////////////////////////////////////////////////////////////////////////////
44/// Arrow default constructor.
45
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
86
87////////////////////////////////////////////////////////////////////////////////
88/// Arrow default destructor.
89
93
94////////////////////////////////////////////////////////////////////////////////
95/// Copy constructor.
96
98{
100 fArrowSize = 0.;
101 arrow.TArrow::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
136{
137
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);
148 newarrow->SetBit(kCanDelete);
149 newarrow->AppendPad(opt);
150 return newarrow;
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// Paint this arrow with its current attributes.
155
157{
158 if (!gPad) return;
159 Option_t *opt;
160 if (option && strlen(option)) opt = option;
161 else opt = (char*)GetOption();
162 if (TestBit(kLineNDC))
163 PaintArrow(gPad->GetX1() + fX1 * (gPad->GetX2() - gPad->GetX1()),
164 gPad->GetY1() + fY1 * (gPad->GetY2() - gPad->GetY1()),
165 gPad->GetX1() + fX2 * (gPad->GetX2() - gPad->GetX1()),
166 gPad->GetY1() + fY2 * (gPad->GetY2() - gPad->GetY1()), fArrowSize, opt);
167 else
168 PaintArrow(gPad->XtoPad(fX1), gPad->YtoPad(fY1), gPad->XtoPad(fX2), gPad->YtoPad(fY2), fArrowSize, opt);
169}
170
171////////////////////////////////////////////////////////////////////////////////
172/// Draw this arrow with new coordinates.
173
176{
177 if (!gPad) return;
178 // Compute the gPad coordinates in TRUE normalized space (NDC)
179 Int_t iw = gPad->GetWw();
180 Int_t ih = gPad->GetWh();
182 gPad->GetPadPar(x1p,y1p,x2p,y2p);
183 Int_t ix1 = (Int_t)(iw*x1p);
184 Int_t iy1 = (Int_t)(ih*y1p);
185 Int_t ix2 = (Int_t)(iw*x2p);
186 Int_t iy2 = (Int_t)(ih*y2p);
187 if (ix1==ix2||iy1==iy2) return;
188
189 // Option and attributes
190 TString opt = option;
191 opt.ToLower();
194
195
204
205 // Ratios to convert user space in TRUE normalized space (NDC)
207 gPad->GetRange(rx1,ry1,rx2,ry2);
208 Double_t rx = (x2ndc-x1ndc)/(rx2-rx1);
209 Double_t ry = (y2ndc-y1ndc)/(ry2-ry1);
210
211 // Arrow position and arrow's middle in NDC space
212 Double_t x1n = rx*(x1-rx1)+x1ndc;
213 Double_t x2n = rx*(x2-rx1)+x1ndc;
214 Double_t y1n = ry*(y1-ry1)+y1ndc;
215 Double_t y2n = ry*(y2-ry1)+y1ndc;
216 Double_t xm = (x1n+x2n)/2;
217 Double_t ym = (y1n+y2n)/2;
218
219 // Arrow heads size
223 Double_t cosT = (length > 0) ? (x2n-x1n)/length : 1.;
224 Double_t sinT = (length > 0) ? (y2n-y1n)/length : 0.;
225
226 // Draw the start and end bars if needed
227 if (opt.BeginsWith("|-")) {
228 Double_t x1ar[2], y1ar[2];
229 x1ar[0] = x1n-sinT*dSize;
230 y1ar[0] = y1n+cosT*dSize;
231 x1ar[1] = x1n+sinT*dSize;
232 y1ar[1] = y1n-cosT*dSize;
233 // NDC to user coordinates
234 for (Int_t i=0; i<2; i++) {
235 x1ar[i] = (1/rx)*(x1ar[i]-x1ndc)+rx1;
236 y1ar[i] = (1/ry)*(y1ar[i]-y1ndc)+ry1;
237 }
238 gPad->PaintLine(x1ar[0],y1ar[0],x1ar[1],y1ar[1]);
239 opt(0) = ' ';
240 }
241 if (opt.EndsWith("-|")) {
242 Double_t x2ar[2], y2ar[2];
243 x2ar[0] = x2n-sinT*dSize;
244 y2ar[0] = y2n+cosT*dSize;
245 x2ar[1] = x2n+sinT*dSize;
246 y2ar[1] = y2n-cosT*dSize;
247 // NDC to user coordinates
248 for (Int_t i=0; i<2; i++) {
249 x2ar[i] = (1/rx)*(x2ar[i]-x1ndc)+rx1;
250 y2ar[i] = (1/ry)*(y2ar[i]-y1ndc)+ry1;
251 }
252 gPad->PaintLine(x2ar[0],y2ar[0],x2ar[1],y2ar[1]);
253 opt(opt.Length()-1) = ' ';
254 }
255
256 // Move arrow head's position if needed
257 Double_t x1h = x1n;
258 Double_t y1h = y1n;
259 Double_t x2h = x2n;
260 Double_t y2h = y2n;
261 if (opt.Contains("->-") || opt.Contains("-|>-")) {
262 x2h = xm + cosT*rSize/2;
263 y2h = ym + sinT*rSize/2;
264 }
265 if (opt.Contains("-<-") || opt.Contains("-<|-")) {
266 x1h = xm - cosT*rSize/2;
267 y1h = ym - sinT*rSize/2;
268 }
269
270 // Paint Arrow body
271 if (opt.Contains("|>") && !opt.Contains("-|>-")) {
272 x2n -= cosT*rSize;
273 y2n -= sinT*rSize;
274 }
275 if (opt.Contains("<|") && !opt.Contains("-<|-")) {
276 x1n += cosT*rSize;
277 y1n += sinT*rSize;
278 }
279 x1n = (1/rx)*(x1n-x1ndc)+rx1;
280 y1n = (1/ry)*(y1n-y1ndc)+ry1;
281 x2n = (1/rx)*(x2n-x1ndc)+rx1;
282 y2n = (1/ry)*(y2n-y1ndc)+ry1;
283 gPad->PaintLine(x1n,y1n,x2n,y2n);
284
285 // Draw the arrow's head(s)
286 if (opt.Contains(">")) {
287 Double_t x2ar[4], y2ar[4];
288
289 x2ar[0] = x2h - rSize*cosT - sinT*dSize;
290 y2ar[0] = y2h - rSize*sinT + cosT*dSize;
291 x2ar[1] = x2h;
292 y2ar[1] = y2h;
293 x2ar[2] = x2h - rSize*cosT + sinT*dSize;
294 y2ar[2] = y2h - rSize*sinT - cosT*dSize;
295 x2ar[3] = x2ar[0];
296 y2ar[3] = y2ar[0];
297
298 // NDC to user coordinates
299 for (Int_t i=0; i<4; i++) {
300 x2ar[i] = (1/rx)*(x2ar[i]-x1ndc)+rx1;
301 y2ar[i] = (1/ry)*(y2ar[i]-y1ndc)+ry1;
302 }
303 if (opt.Contains("|>")) {
304 if (gVirtualX) gVirtualX->SetLineStyle(1);
306 if (GetFillColor()) {
307 gPad->PaintFillArea(3,x2ar,y2ar);
308 gPad->PaintPolyLine(4,x2ar,y2ar);
309 } else {
310 gPad->PaintPolyLine(4,x2ar,y2ar);
311 }
312 } else {
313 gPad->PaintPolyLine(3,x2ar,y2ar);
314 }
315 }
316
317 if (opt.Contains("<")) {
318 Double_t x1ar[4], y1ar[4];
319 x1ar[0] = x1h + rSize*cosT + sinT*dSize;
320 y1ar[0] = y1h + rSize*sinT - cosT*dSize;
321 x1ar[1] = x1h;
322 y1ar[1] = y1h;
323 x1ar[2] = x1h + rSize*cosT - sinT*dSize;
324 y1ar[2] = y1h + rSize*sinT + cosT*dSize;
325 x1ar[3] = x1ar[0];
326 y1ar[3] = y1ar[0];
327
328 // NDC to user coordinates
329 for (Int_t i=0; i<4; i++) {
330 x1ar[i] = (1/rx)*(x1ar[i]-x1ndc)+rx1;
331 y1ar[i] = (1/ry)*(y1ar[i]-y1ndc)+ry1;
332 }
333 if (opt.Contains("<|")) {
334 if (gVirtualX) gVirtualX->SetLineStyle(1);
336 if (GetFillColor()) {
337 gPad->PaintFillArea(3,x1ar,y1ar);
338 gPad->PaintPolyLine(4,x1ar,y1ar);
339 } else {
340 gPad->PaintPolyLine(4,x1ar,y1ar);
341 }
342 } else {
343 gPad->PaintPolyLine(3,x1ar,y1ar);
344 }
345 }
346}
347
348////////////////////////////////////////////////////////////////////////////////
349/// Draw this arrow with new coordinates in NDC.
350
353{
354 if (!gPad) return;
355 PaintArrow(gPad->GetX1() + u1 * (gPad->GetX2() - gPad->GetX1()),
356 gPad->GetY1() + v1 * (gPad->GetY2() - gPad->GetY1()),
357 gPad->GetX1() + u2 * (gPad->GetX2() - gPad->GetX1()),
358 gPad->GetY1() + v2 * (gPad->GetY2() - gPad->GetY1()), arrowsize, option);
359}
360
361////////////////////////////////////////////////////////////////////////////////
362/// Save primitive as a C++ statement(s) on output stream out
363
364void TArrow::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
365{
366 SavePrimitiveConstructor(out, Class(), "arrow",
367 TString::Format("%g, %g, %g, %g, %g, \"%s\"", fX1, fY1, fX2, fY2, fArrowSize,
368 TString(GetDrawOption()).ReplaceSpecialCppChars().Data()));
369
370 SaveFillAttributes(out, "arrow", 0, 1);
371 SaveLineAttributes(out, "arrow", 1, 1, 1);
372
373 if (TestBit(kLineNDC))
374 out << " arrow->SetNDC();\n";
375
376 if (GetAngle() != 60)
377 out << " arrow->SetAngle(" << GetAngle() << ");\n";
378
379 out << " arrow->Draw();\n";
380}
381
382////////////////////////////////////////////////////////////////////////////////
383/// Set default angle.
384
386{
387 fgDefaultAngle = Angle;
388}
389
390
391////////////////////////////////////////////////////////////////////////////////
392/// Set default arrow size.
393
398
399
400////////////////////////////////////////////////////////////////////////////////
401/// Set default option.
402
404{
405 fgDefaultOption = Option;
406}
407
408
409////////////////////////////////////////////////////////////////////////////////
410/// Get default angle.
411
416
417
418////////////////////////////////////////////////////////////////////////////////
419/// Get default arrow size.
420
425
426
427////////////////////////////////////////////////////////////////////////////////
428/// Get default option.
429
431{
432 return fgDefaultOption.Data();
433}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:374
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t SetFillColor
Option_t Option_t TPoint TPoint const char y1
R__EXTERN TVirtualPS * gVirtualPS
Definition TVirtualPS.h:81
#define gPad
#define gVirtualX
Definition TVirtualX.h:337
Draw all kinds of Arrows.
Definition TArrow.h:29
Float_t GetAngle() const
Definition TArrow.h:51
Option_t * GetOption() const override
Definition TArrow.h:53
static Float_t GetDefaultAngle()
Get default angle.
Definition TArrow.cxx:412
Float_t fArrowSize
Arrow Size.
Definition TArrow.h:32
static void SetDefaultOption(Option_t *Option)
Set default option.
Definition TArrow.cxx:403
static TClass * Class()
static void SetDefaultArrowSize(Float_t ArrowSize)
Set default arrow size.
Definition TArrow.cxx:394
static void SetDefaultAngle(Float_t Angle)
Set default angle.
Definition TArrow.cxx:385
static Float_t fgDefaultAngle
Default Arrow opening angle (degrees)
Definition TArrow.h:35
static Option_t * GetDefaultOption()
Get default option.
Definition TArrow.cxx:430
static Float_t fgDefaultArrowSize
Default Arrow Size.
Definition TArrow.h:36
virtual TArrow * 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
void Paint(Option_t *option="") override
Paint this arrow with its current attributes.
Definition TArrow.cxx:156
TArrow()
Arrow default constructor.
Definition TArrow.cxx:46
Float_t fAngle
Arrow opening angle (degrees)
Definition TArrow.h:31
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TArrow.cxx:364
static TString fgDefaultOption
Default Arrow shapes.
Definition TArrow.h:37
TString fOption
Arrow shapes.
Definition TArrow.h:33
virtual void PaintArrowNDC(Double_t u1, Double_t v1, Double_t u2, Double_t v2, Float_t arrowsize=0.05, Option_t *option=">")
Draw this arrow with new coordinates in NDC.
Definition TArrow.cxx:351
void Draw(Option_t *option="") override
Draw this arrow with its current attributes.
Definition TArrow.cxx:119
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 with new coordinates.
Definition TArrow.cxx:174
~TArrow() override
Arrow default destructor.
Definition TArrow.cxx:90
static Float_t GetDefaultArrowSize()
Get default arrow size.
Definition TArrow.cxx:421
void Copy(TObject &arrow) const override
Copy this arrow to arrow.
Definition TArrow.cxx:107
Fill Area Attributes class.
Definition TAttFill.h:20
virtual Color_t GetFillColor() const
Return the fill area color.
Definition TAttFill.h:31
void Copy(TAttFill &attfill) const
Copy this fill attributes to a new TAttFill.
Definition TAttFill.cxx:207
virtual void Modify()
Change current fill area attributes if necessary.
Definition TAttFill.cxx:216
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:239
virtual Color_t GetLineColor() const
Return the line color.
Definition TAttLine.h:35
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition TAttLine.h:44
virtual void Modify()
Change current line attributes if necessary.
Definition TAttLine.cxx:247
void Copy(TAttLine &attline) const
Copy this line attributes to a new TAttLine.
Definition TAttLine.cxx:177
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:275
Use the TLine constructor to create a simple line.
Definition TLine.h:22
Double_t fY1
Y of 1st point.
Definition TLine.h:26
Double_t fX1
X of 1st point.
Definition TLine.h:25
@ kLineNDC
Use NDC coordinates.
Definition TLine.h:33
void Copy(TObject &line) const override
Copy this line to line.
Definition TLine.cxx:76
Double_t fX2
X of 2nd point.
Definition TLine.h:27
Double_t fY2
Y of 2nd point.
Definition TLine.h:28
Mother of all ROOT objects.
Definition TObject.h:41
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:203
virtual Option_t * GetDrawOption() const
Get option used by the graphics system to draw this object.
Definition TObject.cxx:441
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:203
static void SavePrimitiveConstructor(std::ostream &out, TClass *cl, const char *variable_name, const char *constructor_agrs="", Bool_t empty_line=kTRUE)
Save object constructor in the output stream "out".
Definition TObject.cxx:771
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:66
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:417
void ToLower()
Change string to lower-case.
Definition TString.cxx:1182
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2244
const char * Data() const
Definition TString.h:376
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:623
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2378
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:666
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198
constexpr Double_t Pi()
Definition TMath.h:37
Double_t Tan(Double_t)
Returns the tangent of an angle of x radians.
Definition TMath.h:604