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;
120 if (option && strlen(option)) opt = option;
121 else opt = (char*)GetOption();
122
123 AppendPad(opt);
124}
125
126////////////////////////////////////////////////////////////////////////////////
127/// Draw this arrow with new coordinates.
128///
129/// - if `arrowsize` is <= 0, `arrowsize` will be the current arrow size
130/// - if `option=""`, `option` will be the current arrow option
131
134{
135
137 if (size <= 0) size = fArrowSize;
138 if (size <= 0) size = 0.05;
139 const char* opt = option;
140 if (!opt || !opt[0]) opt = fOption.Data();
141 if (!opt || !opt[0]) opt = "|>";
142 TArrow *newarrow = new TArrow(x1,y1,x2,y2,size,opt);
143 newarrow->SetAngle(fAngle);
146 newarrow->SetBit(kCanDelete);
147 newarrow->AppendPad(opt);
148 return newarrow;
149}
150
151////////////////////////////////////////////////////////////////////////////////
152/// Paint this arrow with its current attributes.
153
155{
156 if (!gPad) return;
157 Option_t *opt;
158 if (option && strlen(option)) opt = option;
159 else opt = (char*)GetOption();
160 if (TestBit(kLineNDC))
161 PaintArrow(gPad->GetX1() + fX1 * (gPad->GetX2() - gPad->GetX1()),
162 gPad->GetY1() + fY1 * (gPad->GetY2() - gPad->GetY1()),
163 gPad->GetX1() + fX2 * (gPad->GetX2() - gPad->GetX1()),
164 gPad->GetY1() + fY2 * (gPad->GetY2() - gPad->GetY1()), fArrowSize, opt);
165 else
166 PaintArrow(gPad->XtoPad(fX1), gPad->YtoPad(fY1), gPad->XtoPad(fX2), gPad->YtoPad(fY2), fArrowSize, opt);
167}
168
169////////////////////////////////////////////////////////////////////////////////
170/// Draw this arrow with new coordinates.
171
174{
175 if (!gPad) return;
176 // Compute the gPad coordinates in TRUE normalized space (NDC)
177 Int_t iw = gPad->GetWw();
178 Int_t ih = gPad->GetWh();
180 gPad->GetPadPar(x1p,y1p,x2p,y2p);
181 Int_t ix1 = (Int_t)(iw*x1p);
182 Int_t iy1 = (Int_t)(ih*y1p);
183 Int_t ix2 = (Int_t)(iw*x2p);
184 Int_t iy2 = (Int_t)(ih*y2p);
185 if (ix1==ix2||iy1==iy2) return;
186
187 // Option and attributes
188 TString opt = option;
189 opt.ToLower();
192
193
202
203 // Ratios to convert user space in TRUE normalized space (NDC)
205 gPad->GetRange(rx1,ry1,rx2,ry2);
206 Double_t rx = (x2ndc-x1ndc)/(rx2-rx1);
207 Double_t ry = (y2ndc-y1ndc)/(ry2-ry1);
208
209 // Arrow position and arrow's middle in NDC space
210 Double_t x1n = rx*(x1-rx1)+x1ndc;
211 Double_t x2n = rx*(x2-rx1)+x1ndc;
212 Double_t y1n = ry*(y1-ry1)+y1ndc;
213 Double_t y2n = ry*(y2-ry1)+y1ndc;
214 Double_t xm = (x1n+x2n)/2;
215 Double_t ym = (y1n+y2n)/2;
216
217 // Arrow heads size
221 Double_t cosT = (length > 0) ? (x2n-x1n)/length : 1.;
222 Double_t sinT = (length > 0) ? (y2n-y1n)/length : 0.;
223
224 Double_t xarr[6], yarr[6];
225 Int_t cnt = 0;
226
227 // Draw the start and end bars if needed
228 if (opt.BeginsWith("|-")) {
229 xarr[0] = x1n-sinT*dSize;
230 yarr[0] = y1n+cosT*dSize;
231 xarr[1] = x1n+sinT*dSize;
232 yarr[1] = y1n-cosT*dSize;
233 cnt += 2;
234 opt(0) = ' ';
235 }
236 if (opt.EndsWith("-|")) {
237 xarr[cnt] = x2n-sinT*dSize;
238 yarr[cnt] = y2n+cosT*dSize;
239 xarr[cnt+1] = x2n+sinT*dSize;
240 yarr[cnt+1] = y2n-cosT*dSize;
241 cnt += 2;
242 opt(opt.Length()-1) = ' ';
243 }
244
245 // Move arrow head's position if needed
246 Double_t x1h = x1n;
247 Double_t y1h = y1n;
248 Double_t x2h = x2n;
249 Double_t y2h = y2n;
250 if (opt.Contains("->-") || opt.Contains("-|>-")) {
251 x2h = xm + cosT*rSize/2;
252 y2h = ym + sinT*rSize/2;
253 }
254 if (opt.Contains("-<-") || opt.Contains("-<|-")) {
255 x1h = xm - cosT*rSize/2;
256 y1h = ym - sinT*rSize/2;
257 }
258
259 // Paint Arrow body
260 if (opt.Contains("|>") && !opt.Contains("-|>-")) {
261 x2n -= cosT*rSize;
262 y2n -= sinT*rSize;
263 }
264 if (opt.Contains("<|") && !opt.Contains("-<|-")) {
265 x1n += cosT*rSize;
266 y1n += sinT*rSize;
267 }
268 xarr[cnt] = x1n; xarr[cnt+1] = x2n;
269 yarr[cnt] = y1n; yarr[cnt+1] = y2n;
270 cnt += 2;
271 // NDC to user coordinates
272 for (Int_t i=0; i<cnt; i++) {
273 xarr[i] = (1/rx)*(xarr[i]-x1ndc)+rx1;
274 yarr[i] = (1/ry)*(yarr[i]-y1ndc)+ry1;
275 }
276 // paint arrow main line with start/stop segment together
277 gPad->PaintSegments(cnt/2, xarr, yarr);
278
279 // Draw the arrow's head(s)
280 if (opt.Contains(">")) {
281 Double_t x2ar[4], y2ar[4];
282
283 x2ar[0] = x2h - rSize*cosT - sinT*dSize;
284 y2ar[0] = y2h - rSize*sinT + cosT*dSize;
285 x2ar[1] = x2h;
286 y2ar[1] = y2h;
287 x2ar[2] = x2h - rSize*cosT + sinT*dSize;
288 y2ar[2] = y2h - rSize*sinT - cosT*dSize;
289 x2ar[3] = x2ar[0];
290 y2ar[3] = y2ar[0];
291
292 // NDC to user coordinates
293 for (Int_t i=0; i<4; i++) {
294 x2ar[i] = (1/rx)*(x2ar[i]-x1ndc)+rx1;
295 y2ar[i] = (1/ry)*(y2ar[i]-y1ndc)+ry1;
296 }
297 if (opt.Contains("|>")) {
298 gPad->GetPainter()->SetLineStyle(1);
299 if (GetFillColor()) {
300 gPad->PaintFillArea(3,x2ar,y2ar);
301 gPad->PaintPolyLine(4,x2ar,y2ar);
302 } else {
303 gPad->PaintPolyLine(4,x2ar,y2ar);
304 }
305 } else {
306 gPad->PaintPolyLine(3,x2ar,y2ar);
307 }
308 }
309
310 if (opt.Contains("<")) {
311 Double_t x1ar[4], y1ar[4];
312 x1ar[0] = x1h + rSize*cosT + sinT*dSize;
313 y1ar[0] = y1h + rSize*sinT - cosT*dSize;
314 x1ar[1] = x1h;
315 y1ar[1] = y1h;
316 x1ar[2] = x1h + rSize*cosT - sinT*dSize;
317 y1ar[2] = y1h + rSize*sinT + cosT*dSize;
318 x1ar[3] = x1ar[0];
319 y1ar[3] = y1ar[0];
320
321 // NDC to user coordinates
322 for (Int_t i=0; i<4; i++) {
323 x1ar[i] = (1/rx)*(x1ar[i]-x1ndc)+rx1;
324 y1ar[i] = (1/ry)*(y1ar[i]-y1ndc)+ry1;
325 }
326 if (opt.Contains("<|")) {
327 gPad->GetPainter()->SetLineStyle(1);
328 if (GetFillColor()) {
329 gPad->PaintFillArea(3,x1ar,y1ar);
330 gPad->PaintPolyLine(4,x1ar,y1ar);
331 } else {
332 gPad->PaintPolyLine(4,x1ar,y1ar);
333 }
334 } else {
335 gPad->PaintPolyLine(3,x1ar,y1ar);
336 }
337 }
338}
339
340////////////////////////////////////////////////////////////////////////////////
341/// Draw this arrow with new coordinates in NDC.
342
345{
346 if (!gPad) return;
347 PaintArrow(gPad->GetX1() + u1 * (gPad->GetX2() - gPad->GetX1()),
348 gPad->GetY1() + v1 * (gPad->GetY2() - gPad->GetY1()),
349 gPad->GetX1() + u2 * (gPad->GetX2() - gPad->GetX1()),
350 gPad->GetY1() + v2 * (gPad->GetY2() - gPad->GetY1()), arrowsize, option);
351}
352
353////////////////////////////////////////////////////////////////////////////////
354/// Save primitive as a C++ statement(s) on output stream out
355
356void TArrow::SavePrimitive(std::ostream &out, Option_t *option)
357{
358 SavePrimitiveConstructor(out, Class(), "arrow",
359 TString::Format("%g, %g, %g, %g, %g, \"%s\"", fX1, fY1, fX2, fY2, fArrowSize,
360 TString(GetOption()).ReplaceSpecialCppChars().Data()));
361
362 SaveFillAttributes(out, "arrow", 0, 1001);
363 SaveLineAttributes(out, "arrow", 1, 1, 1);
364
365 if (TestBit(kLineNDC))
366 out << " arrow->SetNDC();\n";
367
368 if (GetAngle() != 60)
369 out << " arrow->SetAngle(" << GetAngle() << ");\n";
370
371 SavePrimitiveDraw(out, "arrow", option);
372}
373
374////////////////////////////////////////////////////////////////////////////////
375/// Set default angle.
376
378{
379 fgDefaultAngle = Angle;
380}
381
382
383////////////////////////////////////////////////////////////////////////////////
384/// Set default arrow size.
385
390
391
392////////////////////////////////////////////////////////////////////////////////
393/// Set default option.
394
396{
397 fgDefaultOption = Option;
398}
399
400
401////////////////////////////////////////////////////////////////////////////////
402/// Get default angle.
403
408
409
410////////////////////////////////////////////////////////////////////////////////
411/// Get default arrow size.
412
417
418
419////////////////////////////////////////////////////////////////////////////////
420/// Get default option.
421
423{
424 return fgDefaultOption.Data();
425}
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:404
Float_t fArrowSize
Arrow Size.
Definition TArrow.h:32
static void SetDefaultOption(Option_t *Option)
Set default option.
Definition TArrow.cxx:395
static TClass * Class()
static void SetDefaultArrowSize(Float_t ArrowSize)
Set default arrow size.
Definition TArrow.cxx:386
static void SetDefaultAngle(Float_t Angle)
Set default angle.
Definition TArrow.cxx:377
static Float_t fgDefaultAngle
Default Arrow opening angle (degrees)
Definition TArrow.h:35
static Option_t * GetDefaultOption()
Get default option.
Definition TArrow.cxx:422
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:132
void Paint(Option_t *option="") override
Paint this arrow with its current attributes.
Definition TArrow.cxx:154
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:356
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:343
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:172
~TArrow() override
Arrow default destructor.
Definition TArrow.cxx:88
static Float_t GetDefaultArrowSize()
Get default arrow size.
Definition TArrow.cxx:413
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: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:243
virtual Color_t GetLineColor() const
Return the line color.
Definition TAttLine.h:36
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: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: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: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
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