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
20
21
22/** \class TArrow
23\ingroup BasicGraphics
24
25Draw all kinds of Arrows.
26
27The different arrow's formats are explained in TArrow::TArrow.
28The picture below gives some examples.
29
30Once an arrow is drawn on the screen:
31
32- One can click on one of the edges and move this edge.
33- One can click on any other arrow part to move the entire arrow.
34
35Begin_Macro(source)
36../../../tutorials/visualisation/graphics/arrows.C
37End_Macro
38*/
39
40////////////////////////////////////////////////////////////////////////////////
41/// Arrow default constructor.
42
48
49////////////////////////////////////////////////////////////////////////////////
50/// Arrow normal constructor.
51///
52/// Define an arrow between points x1,y1 and x2,y2
53/// the `arrowsize` is in percentage of the pad height
54/// Opening angle between the two sides of the arrow is fAngle (60 degrees)
55/// ~~~ {.cpp}
56/// option = ">" -------->
57/// option = "|->" |------->
58/// option = "<" <--------
59/// option = "<-|" <-------|
60/// option = "->-" ---->----
61/// option = "-<-" ----<----
62/// option = "-|>-" ---|>----
63/// option = "<>" <------->
64/// option = "<|>" <|-----|> arrow defined by a triangle
65/// ~~~
66/// Note:
67///
68/// - If FillColor == 0 an open triangle is drawn, otherwise a full triangle is
69/// drawn with the fill color. The default is filled with LineColor
70/// - The "Begin" and "end" bars options can be combined with any other options.
71/// - The 9 options described above cannot be mixed.
72
83
84////////////////////////////////////////////////////////////////////////////////
85/// Arrow default destructor.
86
90
91////////////////////////////////////////////////////////////////////////////////
92/// Copy constructor.
93
95{
97 fArrowSize = 0.;
98 arrow.TArrow::Copy(*this);
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// Copy this arrow to arrow
103
104void TArrow::Copy(TObject &obj) const
105{
106 TLine::Copy(obj);
107 TAttFill::Copy(((TArrow&)obj));
108 ((TArrow&)obj).fAngle = fAngle;
109 ((TArrow&)obj).fArrowSize = fArrowSize;
110 ((TArrow&)obj).fOption = fOption;
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Draw this arrow with its current attributes.
115
117{
118 Option_t *opt = option && *option ? option : GetOption();
119
120 AppendPad(opt);
121}
122
123////////////////////////////////////////////////////////////////////////////////
124/// Draw this arrow with new coordinates.
125///
126/// - if `arrowsize` is <= 0, `arrowsize` will be the current arrow size
127/// - if `option=""`, `option` will be the current arrow option
128
131{
132
134 if (size <= 0) size = fArrowSize;
135 if (size <= 0) size = 0.05;
136 Option_t* opt = option;
137 if (!opt || !*opt) opt = GetOption();
138 if (!opt || !*opt) opt = "|>";
139 TArrow *newarrow = new TArrow(x1,y1,x2,y2,size,opt);
140 newarrow->SetAngle(fAngle);
143 newarrow->SetBit(kCanDelete);
144 newarrow->AppendPad(opt);
145 return newarrow;
146}
147
148////////////////////////////////////////////////////////////////////////////////
149/// Paint this arrow with its current attributes.
150
152{
153 if (!gPad) return;
154 Option_t *opt = option && *option ? option : GetOption();
155 if (TestBit(kLineNDC))
157 else
158 PaintArrow(gPad->XtoPad(fX1), gPad->YtoPad(fY1), gPad->XtoPad(fX2), gPad->YtoPad(fY2), fArrowSize, opt);
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Draw this arrow with new coordinates.
163
166{
167 if (!gPad) return;
168 auto &parent = *gPad;
169 // Compute the gPad coordinates in TRUE normalized space (NDC)
170 Int_t iw = parent.GetWw();
171 Int_t ih = parent.GetWh();
173 parent.GetPadPar(x1p,y1p,x2p,y2p);
174 Int_t ix1 = (Int_t)(iw*x1p);
175 Int_t iy1 = (Int_t)(ih*y1p);
176 Int_t ix2 = (Int_t)(iw*x2p);
177 Int_t iy2 = (Int_t)(ih*y2p);
178 if (ix1==ix2||iy1==iy2) return;
179
180 // Option and attributes
181 TString opt = option;
182 opt.ToLower();
183 TAttLine::ModifyOn(parent);
184 TAttFill::ModifyOn(parent);
186
195
196 // Ratios to convert user space in TRUE normalized space (NDC)
198 parent.GetRange(rx1,ry1,rx2,ry2);
199 Double_t rx = (x2ndc-x1ndc)/(rx2-rx1);
200 Double_t ry = (y2ndc-y1ndc)/(ry2-ry1);
201
202 // Arrow position and arrow's middle in NDC space
203 Double_t x1n = rx*(x1-rx1)+x1ndc;
204 Double_t x2n = rx*(x2-rx1)+x1ndc;
205 Double_t y1n = ry*(y1-ry1)+y1ndc;
206 Double_t y2n = ry*(y2-ry1)+y1ndc;
207 Double_t xm = (x1n+x2n)/2;
208 Double_t ym = (y1n+y2n)/2;
209
210 // Arrow heads size
214 Double_t cosT = (length > 0) ? (x2n-x1n)/length : 1.;
215 Double_t sinT = (length > 0) ? (y2n-y1n)/length : 0.;
216
217 Double_t xarr[6], yarr[6];
218 Int_t cnt = 0;
219
220 // Draw the start and end bars if needed
221 if (opt.BeginsWith("|-")) {
222 xarr[0] = x1n-sinT*dSize;
223 yarr[0] = y1n+cosT*dSize;
224 xarr[1] = x1n+sinT*dSize;
225 yarr[1] = y1n-cosT*dSize;
226 cnt += 2;
227 opt(0) = ' ';
228 }
229 if (opt.EndsWith("-|")) {
230 xarr[cnt] = x2n-sinT*dSize;
231 yarr[cnt] = y2n+cosT*dSize;
232 xarr[cnt+1] = x2n+sinT*dSize;
233 yarr[cnt+1] = y2n-cosT*dSize;
234 cnt += 2;
235 opt(opt.Length()-1) = ' ';
236 }
237
238 // Move arrow head's position if needed
239 Double_t x1h = x1n;
240 Double_t y1h = y1n;
241 Double_t x2h = x2n;
242 Double_t y2h = y2n;
243 if (opt.Contains("->-") || opt.Contains("-|>-")) {
244 x2h = xm + cosT*rSize/2;
245 y2h = ym + sinT*rSize/2;
246 }
247 if (opt.Contains("-<-") || opt.Contains("-<|-")) {
248 x1h = xm - cosT*rSize/2;
249 y1h = ym - sinT*rSize/2;
250 }
251
252 // Paint Arrow body
253 if (opt.Contains("|>") && !opt.Contains("-|>-")) {
254 x2n -= cosT*rSize;
255 y2n -= sinT*rSize;
256 }
257 if (opt.Contains("<|") && !opt.Contains("-<|-")) {
258 x1n += cosT*rSize;
259 y1n += sinT*rSize;
260 }
261 xarr[cnt] = x1n; xarr[cnt+1] = x2n;
262 yarr[cnt] = y1n; yarr[cnt+1] = y2n;
263 cnt += 2;
264 // NDC to user coordinates
265 for (Int_t i=0; i<cnt; i++) {
266 xarr[i] = (1/rx)*(xarr[i]-x1ndc)+rx1;
267 yarr[i] = (1/ry)*(yarr[i]-y1ndc)+ry1;
268 }
269 // paint arrow main line with start/stop segment together
270 parent.PaintSegments(cnt/2, xarr, yarr);
271
272 // Draw the arrow's head(s)
273 if (opt.Contains(">")) {
274 Double_t x2ar[4], y2ar[4];
275
276 x2ar[0] = x2h - rSize*cosT - sinT*dSize;
277 y2ar[0] = y2h - rSize*sinT + cosT*dSize;
278 x2ar[1] = x2h;
279 y2ar[1] = y2h;
280 x2ar[2] = x2h - rSize*cosT + sinT*dSize;
281 y2ar[2] = y2h - rSize*sinT - cosT*dSize;
282 x2ar[3] = x2ar[0];
283 y2ar[3] = y2ar[0];
284
285 // NDC to user coordinates
286 for (Int_t i=0; i<4; i++) {
287 x2ar[i] = (1/rx)*(x2ar[i]-x1ndc)+rx1;
288 y2ar[i] = (1/ry)*(y2ar[i]-y1ndc)+ry1;
289 }
290 if (opt.Contains("|>")) {
291 attarrow.ModifyOn(parent);
292 if (GetFillColor()) {
293 parent.PaintFillArea(3,x2ar,y2ar);
294 parent.PaintPolyLine(4,x2ar,y2ar);
295 } else {
296 parent.PaintPolyLine(4,x2ar,y2ar);
297 }
298 } else {
299 parent.PaintPolyLine(3,x2ar,y2ar);
300 }
301 }
302
303 if (opt.Contains("<")) {
304 Double_t x1ar[4], y1ar[4];
305 x1ar[0] = x1h + rSize*cosT + sinT*dSize;
306 y1ar[0] = y1h + rSize*sinT - cosT*dSize;
307 x1ar[1] = x1h;
308 y1ar[1] = y1h;
309 x1ar[2] = x1h + rSize*cosT - sinT*dSize;
310 y1ar[2] = y1h + rSize*sinT + cosT*dSize;
311 x1ar[3] = x1ar[0];
312 y1ar[3] = y1ar[0];
313
314 // NDC to user coordinates
315 for (Int_t i=0; i<4; i++) {
316 x1ar[i] = (1/rx)*(x1ar[i]-x1ndc)+rx1;
317 y1ar[i] = (1/ry)*(y1ar[i]-y1ndc)+ry1;
318 }
319 if (opt.Contains("<|")) {
320 attarrow.ModifyOn(parent);
321 if (GetFillColor()) {
322 parent.PaintFillArea(3,x1ar,y1ar);
323 parent.PaintPolyLine(4,x1ar,y1ar);
324 } else {
325 parent.PaintPolyLine(4,x1ar,y1ar);
326 }
327 } else {
328 parent.PaintPolyLine(3,x1ar,y1ar);
329 }
330 }
331}
332
333////////////////////////////////////////////////////////////////////////////////
334/// Draw this arrow with new coordinates in NDC.
335
338{
339 if (!gPad) return;
340 PaintArrow(gPad->GetX1() + u1 * (gPad->GetX2() - gPad->GetX1()),
341 gPad->GetY1() + v1 * (gPad->GetY2() - gPad->GetY1()),
342 gPad->GetX1() + u2 * (gPad->GetX2() - gPad->GetX1()),
343 gPad->GetY1() + v2 * (gPad->GetY2() - gPad->GetY1()), arrowsize, option);
344}
345
346////////////////////////////////////////////////////////////////////////////////
347/// Save primitive as a C++ statement(s) on output stream out
348
349void TArrow::SavePrimitive(std::ostream &out, Option_t *option)
350{
351 SavePrimitiveConstructor(out, Class(), "arrow",
352 TString::Format("%g, %g, %g, %g, %g, \"%s\"", fX1, fY1, fX2, fY2, fArrowSize,
353 TString(GetOption()).ReplaceSpecialCppChars().Data()));
354
355 SaveFillAttributes(out, "arrow", 0, 1001);
356 SaveLineAttributes(out, "arrow", 1, 1, 1);
357
358 if (TestBit(kLineNDC))
359 out << " arrow->SetNDC();\n";
360
361 if (GetAngle() != 60)
362 out << " arrow->SetAngle(" << GetAngle() << ");\n";
363
364 SavePrimitiveDraw(out, "arrow", option);
365}
366
367////////////////////////////////////////////////////////////////////////////////
368/// Set default angle.
369
371{
372 fgDefaultAngle = Angle;
373}
374
375
376////////////////////////////////////////////////////////////////////////////////
377/// Set default arrow size.
378
383
384
385////////////////////////////////////////////////////////////////////////////////
386/// Set default option.
387
389{
390 fgDefaultOption = Option;
391}
392
393
394////////////////////////////////////////////////////////////////////////////////
395/// Get default angle.
396
401
402
403////////////////////////////////////////////////////////////////////////////////
404/// Get default arrow size.
405
410
411
412////////////////////////////////////////////////////////////////////////////////
413/// Get default option.
414
416{
417 return fgDefaultOption.Data();
418}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
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
#define gPad
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:397
Float_t fArrowSize
Arrow Size.
Definition TArrow.h:32
static void SetDefaultOption(Option_t *Option)
Set default option.
Definition TArrow.cxx:388
static TClass * Class()
static void SetDefaultArrowSize(Float_t ArrowSize)
Set default arrow size.
Definition TArrow.cxx:379
static void SetDefaultAngle(Float_t Angle)
Set default angle.
Definition TArrow.cxx:370
static Float_t fgDefaultAngle
Default Arrow opening angle (degrees)
Definition TArrow.h:35
static Option_t * GetDefaultOption()
Get default option.
Definition TArrow.cxx:415
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:129
void Paint(Option_t *option="") override
Paint this arrow with its current attributes.
Definition TArrow.cxx:151
TArrow()
Arrow default constructor.
Definition TArrow.cxx:43
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:349
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:336
void Draw(Option_t *option="") override
Draw this arrow with its current attributes.
Definition TArrow.cxx:116
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:164
~TArrow() override
Arrow default destructor.
Definition TArrow.cxx:87
static Float_t GetDefaultArrowSize()
Get default arrow size.
Definition TArrow.cxx:406
void Copy(TObject &arrow) const override
Copy this arrow to arrow.
Definition TArrow.cxx:104
Fill Area Attributes class.
Definition TAttFill.h:21
virtual Color_t GetFillColor() const
Return the fill area color.
Definition TAttFill.h:32
void Copy(TAttFill &attfill) const
Copy this fill attributes to a new TAttFill.
Definition TAttFill.cxx:203
virtual void ModifyOn(TVirtualPad &pad)
Change current fill area attributes on speicifed pad.
Definition TAttFill.cxx:221
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:240
Line Attributes class.
Definition TAttLine.h:21
virtual Color_t GetLineColor() const
Return the line color.
Definition TAttLine.h:36
virtual Width_t GetLineWidth() const
Return the line width.
Definition TAttLine.h:38
virtual void ModifyOn(TVirtualPad &pad)
Change current line attributes on specified pad.
Definition TAttLine.cxx:255
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:289
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
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
@ kLineNDC
Use NDC coordinates.
Definition TLine.h:33
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:844
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:776
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:71
Basic string class.
Definition TString.h:137
Ssiz_t Length() const
Definition TString.h:426
void ToLower()
Change string to lower-case.
Definition TString.cxx:1190
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2325
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:633
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:2460
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:642
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:675
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:613