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 Double_t xarr[6], yarr[6];
226 Int_t cnt = 0;
227
228 // Draw the start and end bars if needed
229 if (opt.BeginsWith("|-")) {
230 xarr[0] = x1n-sinT*dSize;
231 yarr[0] = y1n+cosT*dSize;
232 xarr[1] = x1n+sinT*dSize;
233 yarr[1] = y1n-cosT*dSize;
234 cnt += 2;
235 opt(0) = ' ';
236 }
237 if (opt.EndsWith("-|")) {
238 xarr[cnt] = x2n-sinT*dSize;
239 yarr[cnt] = y2n+cosT*dSize;
240 xarr[cnt+1] = x2n+sinT*dSize;
241 yarr[cnt+1] = y2n-cosT*dSize;
242 cnt += 2;
243 opt(opt.Length()-1) = ' ';
244 }
245
246 // Move arrow head's position if needed
247 Double_t x1h = x1n;
248 Double_t y1h = y1n;
249 Double_t x2h = x2n;
250 Double_t y2h = y2n;
251 if (opt.Contains("->-") || opt.Contains("-|>-")) {
252 x2h = xm + cosT*rSize/2;
253 y2h = ym + sinT*rSize/2;
254 }
255 if (opt.Contains("-<-") || opt.Contains("-<|-")) {
256 x1h = xm - cosT*rSize/2;
257 y1h = ym - sinT*rSize/2;
258 }
259
260 // Paint Arrow body
261 if (opt.Contains("|>") && !opt.Contains("-|>-")) {
262 x2n -= cosT*rSize;
263 y2n -= sinT*rSize;
264 }
265 if (opt.Contains("<|") && !opt.Contains("-<|-")) {
266 x1n += cosT*rSize;
267 y1n += sinT*rSize;
268 }
269 xarr[cnt] = x1n; xarr[cnt+1] = x2n;
270 yarr[cnt] = y1n; yarr[cnt+1] = y2n;
271 cnt += 2;
272 // NDC to user coordinates
273 for (Int_t i=0; i<cnt; i++) {
274 xarr[i] = (1/rx)*(xarr[i]-x1ndc)+rx1;
275 yarr[i] = (1/ry)*(yarr[i]-y1ndc)+ry1;
276 }
277 // paint arrow main line with start/stop segment together
278 gPad->PaintSegments(cnt/2, xarr, yarr);
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/// Draw this arrow with new coordinates in NDC.
345
348{
349 if (!gPad) return;
350 PaintArrow(gPad->GetX1() + u1 * (gPad->GetX2() - gPad->GetX1()),
351 gPad->GetY1() + v1 * (gPad->GetY2() - gPad->GetY1()),
352 gPad->GetX1() + u2 * (gPad->GetX2() - gPad->GetX1()),
353 gPad->GetY1() + v2 * (gPad->GetY2() - gPad->GetY1()), arrowsize, option);
354}
355
356////////////////////////////////////////////////////////////////////////////////
357/// Save primitive as a C++ statement(s) on output stream out
358
359void TArrow::SavePrimitive(std::ostream &out, Option_t *option)
360{
361 SavePrimitiveConstructor(out, Class(), "arrow",
362 TString::Format("%g, %g, %g, %g, %g, \"%s\"", fX1, fY1, fX2, fY2, fArrowSize,
363 TString(GetOption()).ReplaceSpecialCppChars().Data()));
364
365 SaveFillAttributes(out, "arrow", 0, 1001);
366 SaveLineAttributes(out, "arrow", 1, 1, 1);
367
368 if (TestBit(kLineNDC))
369 out << " arrow->SetNDC();\n";
370
371 if (GetAngle() != 60)
372 out << " arrow->SetAngle(" << GetAngle() << ");\n";
373
374 SavePrimitiveDraw(out, "arrow", option);
375}
376
377////////////////////////////////////////////////////////////////////////////////
378/// Set default angle.
379
381{
382 fgDefaultAngle = Angle;
383}
384
385
386////////////////////////////////////////////////////////////////////////////////
387/// Set default arrow size.
388
393
394
395////////////////////////////////////////////////////////////////////////////////
396/// Set default option.
397
399{
400 fgDefaultOption = Option;
401}
402
403
404////////////////////////////////////////////////////////////////////////////////
405/// Get default angle.
406
411
412
413////////////////////////////////////////////////////////////////////////////////
414/// Get default arrow size.
415
420
421
422////////////////////////////////////////////////////////////////////////////////
423/// Get default option.
424
426{
427 return fgDefaultOption.Data();
428}
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:84
#define gPad
#define gVirtualX
Definition TVirtualX.h:338
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:407
Float_t fArrowSize
Arrow Size.
Definition TArrow.h:32
static void SetDefaultOption(Option_t *Option)
Set default option.
Definition TArrow.cxx:398
static TClass * Class()
static void SetDefaultArrowSize(Float_t ArrowSize)
Set default arrow size.
Definition TArrow.cxx:389
static void SetDefaultAngle(Float_t Angle)
Set default angle.
Definition TArrow.cxx:380
static Float_t fgDefaultAngle
Default Arrow opening angle (degrees)
Definition TArrow.h:35
static Option_t * GetDefaultOption()
Get default option.
Definition TArrow.cxx:425
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:359
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:346
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:416
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:42
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
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:840
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:772
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:70
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:632
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:641
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:197
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