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
23
24/** \class TArrow
25\ingroup BasicGraphics
26
27Draw all kinds of Arrows.
28
29The different arrow's formats are explained in TArrow::TArrow.
30The picture below gives some examples.
31
32Once an arrow is drawn on the screen:
33
34- One can click on one of the edges and move this edge.
35- One can click on any other arrow part to move the entire arrow.
36
37Begin_Macro(source)
38../../../tutorials/visualisation/graphics/arrows.C
39End_Macro
40*/
41
42////////////////////////////////////////////////////////////////////////////////
43/// Arrow default constructor.
44
50
51////////////////////////////////////////////////////////////////////////////////
52/// Arrow normal constructor.
53///
54/// Define an arrow between points x1,y1 and x2,y2
55/// the `arrowsize` is in percentage of the pad height
56/// Opening angle between the two sides of the arrow is fAngle (60 degrees)
57/// ~~~ {.cpp}
58/// option = ">" -------->
59/// option = "|->" |------->
60/// option = "<" <--------
61/// option = "<-|" <-------|
62/// option = "->-" ---->----
63/// option = "-<-" ----<----
64/// option = "-|>-" ---|>----
65/// option = "<>" <------->
66/// option = "<|>" <|-----|> arrow defined by a triangle
67/// ~~~
68/// Note:
69///
70/// - If FillColor == 0 an open triangle is drawn, otherwise a full triangle is
71/// drawn with the fill color. The default is filled with LineColor
72/// - The "Begin" and "end" bars options can be combined with any other options.
73/// - The 9 options described above cannot be mixed.
74
85
86////////////////////////////////////////////////////////////////////////////////
87/// Arrow default destructor.
88
92
93////////////////////////////////////////////////////////////////////////////////
94/// Copy constructor.
95
97{
99 fArrowSize = 0.;
100 arrow.TArrow::Copy(*this);
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Copy this arrow to arrow
105
106void TArrow::Copy(TObject &obj) const
107{
108 TLine::Copy(obj);
109 TAttFill::Copy(((TArrow&)obj));
110 ((TArrow&)obj).fAngle = fAngle;
111 ((TArrow&)obj).fArrowSize = fArrowSize;
112 ((TArrow&)obj).fOption = fOption;
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Draw this arrow with its current attributes.
117
119{
120 Option_t *opt;
121 if (option && strlen(option)) opt = option;
122 else opt = (char*)GetOption();
123
124 AppendPad(opt);
125}
126
127////////////////////////////////////////////////////////////////////////////////
128/// Draw this arrow with new coordinates.
129///
130/// - if `arrowsize` is <= 0, `arrowsize` will be the current arrow size
131/// - if `option=""`, `option` will be the current arrow option
132
135{
136
138 if (size <= 0) size = fArrowSize;
139 if (size <= 0) size = 0.05;
140 const char* opt = option;
141 if (!opt || !opt[0]) opt = fOption.Data();
142 if (!opt || !opt[0]) opt = "|>";
143 TArrow *newarrow = new TArrow(x1,y1,x2,y2,size,opt);
144 newarrow->SetAngle(fAngle);
147 newarrow->SetBit(kCanDelete);
148 newarrow->AppendPad(opt);
149 return newarrow;
150}
151
152////////////////////////////////////////////////////////////////////////////////
153/// Paint this arrow with its current attributes.
154
156{
157 if (!gPad) return;
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 with new coordinates.
172
175{
176 if (!gPad) return;
177 // Compute the gPad coordinates in TRUE normalized space (NDC)
178 Int_t iw = gPad->GetWw();
179 Int_t ih = gPad->GetWh();
181 gPad->GetPadPar(x1p,y1p,x2p,y2p);
182 Int_t ix1 = (Int_t)(iw*x1p);
183 Int_t iy1 = (Int_t)(ih*y1p);
184 Int_t ix2 = (Int_t)(iw*x2p);
185 Int_t iy2 = (Int_t)(ih*y2p);
186 if (ix1==ix2||iy1==iy2) return;
187
188 // Option and attributes
189 TString opt = option;
190 opt.ToLower();
193
194
203
204 // Ratios to convert user space in TRUE normalized space (NDC)
206 gPad->GetRange(rx1,ry1,rx2,ry2);
207 Double_t rx = (x2ndc-x1ndc)/(rx2-rx1);
208 Double_t ry = (y2ndc-y1ndc)/(ry2-ry1);
209
210 // Arrow position and arrow's middle in NDC space
211 Double_t x1n = rx*(x1-rx1)+x1ndc;
212 Double_t x2n = rx*(x2-rx1)+x1ndc;
213 Double_t y1n = ry*(y1-ry1)+y1ndc;
214 Double_t y2n = ry*(y2-ry1)+y1ndc;
215 Double_t xm = (x1n+x2n)/2;
216 Double_t ym = (y1n+y2n)/2;
217
218 // Arrow heads size
222 Double_t cosT = (length > 0) ? (x2n-x1n)/length : 1.;
223 Double_t sinT = (length > 0) ? (y2n-y1n)/length : 0.;
224
225 // Draw the start and end bars if needed
226 if (opt.BeginsWith("|-")) {
227 Double_t x1ar[2], y1ar[2];
228 x1ar[0] = x1n-sinT*dSize;
229 y1ar[0] = y1n+cosT*dSize;
230 x1ar[1] = x1n+sinT*dSize;
231 y1ar[1] = y1n-cosT*dSize;
232 // NDC to user coordinates
233 for (Int_t i=0; i<2; i++) {
234 x1ar[i] = (1/rx)*(x1ar[i]-x1ndc)+rx1;
235 y1ar[i] = (1/ry)*(y1ar[i]-y1ndc)+ry1;
236 }
237 gPad->PaintLine(x1ar[0],y1ar[0],x1ar[1],y1ar[1]);
238 opt(0) = ' ';
239 }
240 if (opt.EndsWith("-|")) {
241 Double_t x2ar[2], y2ar[2];
242 x2ar[0] = x2n-sinT*dSize;
243 y2ar[0] = y2n+cosT*dSize;
244 x2ar[1] = x2n+sinT*dSize;
245 y2ar[1] = y2n-cosT*dSize;
246 // NDC to user coordinates
247 for (Int_t i=0; i<2; i++) {
248 x2ar[i] = (1/rx)*(x2ar[i]-x1ndc)+rx1;
249 y2ar[i] = (1/ry)*(y2ar[i]-y1ndc)+ry1;
250 }
251 gPad->PaintLine(x2ar[0],y2ar[0],x2ar[1],y2ar[1]);
252 opt(opt.Length()-1) = ' ';
253 }
254
255 // Move arrow head's position if needed
256 Double_t x1h = x1n;
257 Double_t y1h = y1n;
258 Double_t x2h = x2n;
259 Double_t y2h = y2n;
260 if (opt.Contains("->-") || opt.Contains("-|>-")) {
261 x2h = xm + cosT*rSize/2;
262 y2h = ym + sinT*rSize/2;
263 }
264 if (opt.Contains("-<-") || opt.Contains("-<|-")) {
265 x1h = xm - cosT*rSize/2;
266 y1h = ym - sinT*rSize/2;
267 }
268
269 // Paint Arrow body
270 if (opt.Contains("|>") && !opt.Contains("-|>-")) {
271 x2n -= cosT*rSize;
272 y2n -= sinT*rSize;
273 }
274 if (opt.Contains("<|") && !opt.Contains("-<|-")) {
275 x1n += cosT*rSize;
276 y1n += sinT*rSize;
277 }
278 x1n = (1/rx)*(x1n-x1ndc)+rx1;
279 y1n = (1/ry)*(y1n-y1ndc)+ry1;
280 x2n = (1/rx)*(x2n-x1ndc)+rx1;
281 y2n = (1/ry)*(y2n-y1ndc)+ry1;
282 gPad->PaintLine(x1n,y1n,x2n,y2n);
283
284 // Draw the arrow's head(s)
285 if (opt.Contains(">")) {
286 Double_t x2ar[4], y2ar[4];
287
288 x2ar[0] = x2h - rSize*cosT - sinT*dSize;
289 y2ar[0] = y2h - rSize*sinT + cosT*dSize;
290 x2ar[1] = x2h;
291 y2ar[1] = y2h;
292 x2ar[2] = x2h - rSize*cosT + sinT*dSize;
293 y2ar[2] = y2h - rSize*sinT - cosT*dSize;
294 x2ar[3] = x2ar[0];
295 y2ar[3] = y2ar[0];
296
297 // NDC to user coordinates
298 for (Int_t i=0; i<4; i++) {
299 x2ar[i] = (1/rx)*(x2ar[i]-x1ndc)+rx1;
300 y2ar[i] = (1/ry)*(y2ar[i]-y1ndc)+ry1;
301 }
302 if (opt.Contains("|>")) {
303 if (gVirtualX) gVirtualX->SetLineStyle(1);
305 if (GetFillColor()) {
306 gPad->PaintFillArea(3,x2ar,y2ar);
307 gPad->PaintPolyLine(4,x2ar,y2ar);
308 } else {
309 gPad->PaintPolyLine(4,x2ar,y2ar);
310 }
311 } else {
312 gPad->PaintPolyLine(3,x2ar,y2ar);
313 }
314 }
315
316 if (opt.Contains("<")) {
317 Double_t x1ar[4], y1ar[4];
318 x1ar[0] = x1h + rSize*cosT + sinT*dSize;
319 y1ar[0] = y1h + rSize*sinT - cosT*dSize;
320 x1ar[1] = x1h;
321 y1ar[1] = y1h;
322 x1ar[2] = x1h + rSize*cosT - sinT*dSize;
323 y1ar[2] = y1h + rSize*sinT + cosT*dSize;
324 x1ar[3] = x1ar[0];
325 y1ar[3] = y1ar[0];
326
327 // NDC to user coordinates
328 for (Int_t i=0; i<4; i++) {
329 x1ar[i] = (1/rx)*(x1ar[i]-x1ndc)+rx1;
330 y1ar[i] = (1/ry)*(y1ar[i]-y1ndc)+ry1;
331 }
332 if (opt.Contains("<|")) {
333 if (gVirtualX) gVirtualX->SetLineStyle(1);
335 if (GetFillColor()) {
336 gPad->PaintFillArea(3,x1ar,y1ar);
337 gPad->PaintPolyLine(4,x1ar,y1ar);
338 } else {
339 gPad->PaintPolyLine(4,x1ar,y1ar);
340 }
341 } else {
342 gPad->PaintPolyLine(3,x1ar,y1ar);
343 }
344 }
345}
346
347////////////////////////////////////////////////////////////////////////////////
348/// Draw this arrow with new coordinates in NDC.
349
352{
353 if (!gPad) return;
354 PaintArrow(gPad->GetX1() + u1 * (gPad->GetX2() - gPad->GetX1()),
355 gPad->GetY1() + v1 * (gPad->GetY2() - gPad->GetY1()),
356 gPad->GetX1() + u2 * (gPad->GetX2() - gPad->GetX1()),
357 gPad->GetY1() + v2 * (gPad->GetY2() - gPad->GetY1()), arrowsize, option);
358}
359
360////////////////////////////////////////////////////////////////////////////////
361/// Save primitive as a C++ statement(s) on output stream out
362
363void TArrow::SavePrimitive(std::ostream &out, Option_t *option)
364{
365 SavePrimitiveConstructor(out, Class(), "arrow",
366 TString::Format("%g, %g, %g, %g, %g, \"%s\"", fX1, fY1, fX2, fY2, fArrowSize,
367 TString(GetDrawOption()).ReplaceSpecialCppChars().Data()));
368
369 SaveFillAttributes(out, "arrow", 0, 1);
370 SaveLineAttributes(out, "arrow", 1, 1, 1);
371
372 if (TestBit(kLineNDC))
373 out << " arrow->SetNDC();\n";
374
375 if (GetAngle() != 60)
376 out << " arrow->SetAngle(" << GetAngle() << ");\n";
377
378 SavePrimitiveDraw(out, "arrow", option);
379}
380
381////////////////////////////////////////////////////////////////////////////////
382/// Set default angle.
383
385{
386 fgDefaultAngle = Angle;
387}
388
389
390////////////////////////////////////////////////////////////////////////////////
391/// Set default arrow size.
392
397
398
399////////////////////////////////////////////////////////////////////////////////
400/// Set default option.
401
403{
404 fgDefaultOption = Option;
405}
406
407
408////////////////////////////////////////////////////////////////////////////////
409/// Get default angle.
410
415
416
417////////////////////////////////////////////////////////////////////////////////
418/// Get default arrow size.
419
424
425
426////////////////////////////////////////////////////////////////////////////////
427/// Get default option.
428
430{
431 return fgDefaultOption.Data();
432}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
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:411
Float_t fArrowSize
Arrow Size.
Definition TArrow.h:32
static void SetDefaultOption(Option_t *Option)
Set default option.
Definition TArrow.cxx:402
static TClass * Class()
static void SetDefaultArrowSize(Float_t ArrowSize)
Set default arrow size.
Definition TArrow.cxx:393
static void SetDefaultAngle(Float_t Angle)
Set default angle.
Definition TArrow.cxx:384
static Float_t fgDefaultAngle
Default Arrow opening angle (degrees)
Definition TArrow.h:35
static Option_t * GetDefaultOption()
Get default option.
Definition TArrow.cxx:429
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:133
void Paint(Option_t *option="") override
Paint this arrow with its current attributes.
Definition TArrow.cxx:155
TArrow()
Arrow default constructor.
Definition TArrow.cxx:45
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:363
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:350
void Draw(Option_t *option="") override
Draw this arrow with its current attributes.
Definition TArrow.cxx:118
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:173
~TArrow() override
Arrow default destructor.
Definition TArrow.cxx:89
static Float_t GetDefaultArrowSize()
Get default arrow size.
Definition TArrow.cxx:420
void Copy(TObject &arrow) const override
Copy this arrow to arrow.
Definition TArrow.cxx:106
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:206
virtual void Modify()
Change current fill area attributes if necessary.
Definition TAttFill.cxx:215
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:238
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:246
void Copy(TAttLine &attline) const
Copy this line attributes to a new TAttLine.
Definition TAttLine.cxx:176
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:274
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:75
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:202
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 SavePrimitiveDraw(std::ostream &out, const char *variable_name, Option_t *option=nullptr)
Save invocation of primitive Draw() method Skipped if option contains "nodraw" string.
Definition TObject.cxx:822
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:68
Basic string class.
Definition TString.h:138
Ssiz_t Length() const
Definition TString.h:425
void ToLower()
Change string to lower-case.
Definition TString.cxx:1189
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2250
const char * Data() const
Definition TString.h:384
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:631
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:2384
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:640
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:673
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:199
constexpr Double_t Pi()
Definition TMath.h:40
Double_t Tan(Double_t)
Returns the tangent of an angle of x radians.
Definition TMath.h:611