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