Logo ROOT  
Reference Guide
TAttLine.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Rene Brun 28/11/94
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 "TAttLine.h"
13 #include "TVirtualPad.h"
14 #include "TStyle.h"
15 #include "TVirtualX.h"
16 #include "TVirtualPadEditor.h"
17 #include "TColor.h"
18 #include <cmath>
19 #include <iostream>
20 
22 using std::sqrt;
23 
24 /** \class TAttLine
25 \ingroup Base
26 \ingroup GraphicsAtt
27 
28 Line Attributes class.
29 
30 This class is used (in general by secondary inheritance)
31 by many other classes (graphics, histograms). It holds all the line attributes.
32 
33 ## Line attributes
34 Line attributes are:
35 
36  - [Line Color](\ref L1)
37  - [Line Width](\ref L2)
38  - [Line Style](\ref L3)
39 
40 \anchor L1
41 ## Line Color
42 The line color is a color index (integer) pointing in the ROOT
43 color table.
44 The line color of any class inheriting from `TAttLine` can
45 be changed using the method `SetLineColor` and retrieved using the
46 method `GetLineColor`.
47 The following table shows the first 50 default colors.
48 
49 Begin_Macro
50 {
51  TCanvas *c = new TCanvas("c","Fill Area colors",0,0,500,200);
52  c->DrawColorTable();
53  return c;
54 }
55 End_Macro
56 
57 ### Color transparency
58 `SetLineColorAlpha()`, allows to set a transparent color.
59 In the following example the line color of the histogram `histo`
60 is set to blue with a transparency of 35%. The color `kBlue`
61 itself remains fully opaque.
62 
63 ~~~ {.cpp}
64 histo->SetLineColorAlpha(kBlue, 0.35);
65 ~~~
66 
67 The transparency is available on all platforms when the flag `OpenGL.CanvasPreferGL` is set to `1`
68 in `$ROOTSYS/etc/system.rootrc`, or on Mac with the Cocoa backend. On the file output
69 it is visible with PDF, PNG, Gif, JPEG, SVG, TeX ... but not PostScript.
70 
71 
72 \anchor L2
73 ## Line Width
74 The line width is expressed in pixel units.
75 The line width of any class inheriting from `TAttLine` can
76 be changed using the method `SetLineWidth` and retrieved using the
77 method `GetLineWidth`.
78 The following picture shows the line widths from 1 to 10 pixels.
79 
80 Begin_Macro
81 {
82  TCanvas *Lw = new TCanvas("Lw","test",500,200);
83  TText t;
84  t.SetTextAlign(32);
85  t.SetTextSize(0.08);
86  Int_t i=1;
87  for (float s=0.1; s<1.0 ; s+=0.092) {
88  TLine *lh = new TLine(0.15,s,.85,s);
89  lh->SetLineWidth(i);
90  t.DrawText(0.1,s,Form("%d",i++));
91  lh->Draw();
92  }
93 }
94 End_Macro
95 
96 \anchor L3
97 ## Line Style
98 Line styles are identified via integer numbers. The line style of any class
99 inheriting from `TAttLine` can be changed using the method
100 `SetLineStyle` and retrieved using the method `GetLineStyle`.
101 
102 The first 10 line styles are predefined as shown on the following picture:
103 
104 Begin_Macro
105 {
106  TCanvas *Ls = new TCanvas("Ls","test",500,200);
107  TText t;
108  t.SetTextAlign(32);
109  t.SetTextSize(0.08);
110  Int_t i=1;
111  for (float s=0.1; s<1.0 ; s+=0.092) {
112  TLine *lh = new TLine(0.15,s,.85,s);
113  lh->SetLineStyle(i);
114  lh->SetLineWidth(3);
115  t.DrawText(0.1,s,Form("%d",i++));
116  lh->Draw();
117  }
118 }
119 End_Macro
120 
121 Some line styles can be accessed via the following enum:
122 
123 ~~~ {.cpp}
124  kSolid = 1
125  kDashed = 2
126  kDotted = 3
127  kDashDotted = 4
128 ~~~
129 
130 Additional line styles can be defined using `TStyle::SetLineStyleString`.
131 For example the line style number 11 can be defined as follow:
132 ~~~ {.cpp}
133  gStyle->SetLineStyleString(11,"400 200");
134 ~~~
135 Existing line styles (1 to 10) can be redefined using the same method.
136  */
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// AttLine default constructor.
140 
142 {
143  if (!gStyle) {fLineColor=1; fLineWidth=1; fLineStyle=1; return;}
147 }
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 /// AttLine normal constructor.
151 /// Line attributes are taking from the argument list
152 ///
153 /// - color : must be one of the valid color index
154 /// - style : 1=solid, 2=dash, 3=dash-dot, 4=dot-dot. New styles can be
155 /// defined using TStyle::SetLineStyleString.
156 /// - width : expressed in pixel units
157 
159 {
160  fLineColor = color;
161  fLineWidth = width;
162  fLineStyle = style;
163 }
164 
165 ////////////////////////////////////////////////////////////////////////////////
166 /// AttLine destructor.
167 
169 {
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Copy this line attributes to a new TAttLine.
174 
175 void TAttLine::Copy(TAttLine &attline) const
176 {
177  attline.fLineColor = fLineColor;
178  attline.fLineStyle = fLineStyle;
179  attline.fLineWidth = fLineWidth;
180 }
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 /// Compute distance from point px,py to a line.
184 /// Compute the closest distance of approach from point px,py to this line.
185 /// The distance is computed in pixels units.
186 ///
187 /// Algorithm:
188 ///~~~ {.cpp}
189 /// A(x1,y1) P B(x2,y2)
190 /// -----------------+------------------------------
191 /// |
192 /// |
193 /// |
194 /// |
195 /// M(x,y)
196 ///
197 /// Let us call a = distance AM A=a**2
198 /// b = distance BM B=b**2
199 /// c = distance AB C=c**2
200 /// d = distance PM D=d**2
201 /// u = distance AP U=u**2
202 /// v = distance BP V=v**2 c = u + v
203 ///
204 /// D = A - U
205 /// D = B - V = B -(c-u)**2
206 /// ==> u = (A -B +C)/2c
207 ///~~~
208 
210 {
211  Double_t xl, xt, yl, yt;
212  Double_t x = px;
213  Double_t y = py;
214  Double_t x1 = gPad->XtoAbsPixel(xp1);
215  Double_t y1 = gPad->YtoAbsPixel(yp1);
216  Double_t x2 = gPad->XtoAbsPixel(xp2);
217  Double_t y2 = gPad->YtoAbsPixel(yp2);
218  if (x1 < x2) {xl = x1; xt = x2;}
219  else {xl = x2; xt = x1;}
220  if (y1 < y2) {yl = y1; yt = y2;}
221  else {yl = y2; yt = y1;}
222  if (x < xl-2 || x> xt+2) return 9999; //following algorithm only valid in the box
223  if (y < yl-2 || y> yt+2) return 9999; //surrounding the line
224  Double_t xx1 = x - x1;
225  Double_t xx2 = x - x2;
226  Double_t x1x2 = x1 - x2;
227  Double_t yy1 = y - y1;
228  Double_t yy2 = y - y2;
229  Double_t y1y2 = y1 - y2;
230  Double_t a = xx1*xx1 + yy1*yy1;
231  Double_t b = xx2*xx2 + yy2*yy2;
232  Double_t c = x1x2*x1x2 + y1y2*y1y2;
233  if (c <= 0) return 9999;
234  Double_t v = sqrt(c);
235  Double_t u = (a - b + c)/(2*v);
236  Double_t d = TMath::Abs(a - u*u);
237  if (d < 0) return 9999;
238 
239  return Int_t(sqrt(d) - 0.5*Double_t(fLineWidth));
240 }
241 
242 ////////////////////////////////////////////////////////////////////////////////
243 /// Change current line attributes if necessary.
244 
246 {
247  if (!gPad) return;
248  Int_t lineWidth = TMath::Abs(fLineWidth%100);
249  if (!gPad->IsBatch()) {
250  gVirtualX->SetLineColor(fLineColor);
251  if (fLineStyle > 0 && fLineStyle < 30) gVirtualX->SetLineStyle(fLineStyle);
252  else gVirtualX->SetLineStyle(1);
253  gVirtualX->SetLineWidth(lineWidth);
254  }
255 
256  if (fLineStyle > 0 && fLineStyle < 30) gPad->SetAttLinePS(fLineColor,fLineStyle,lineWidth);
257  else gPad->SetAttLinePS(fLineColor,1,lineWidth);
258 }
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// Reset this line attributes to default values.
262 
264 {
265  fLineColor = 1;
266  fLineStyle = 1;
267  fLineWidth = 1;
268 }
269 
270 ////////////////////////////////////////////////////////////////////////////////
271 /// Save line attributes as C++ statement(s) on output stream out.
272 
273 void TAttLine::SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef, Int_t stydef, Int_t widdef)
274 {
275  if (fLineColor != coldef) {
276  if (fLineColor > 228) {
278  out<<" "<<name<<"->SetLineColor(ci);" << std::endl;
279  } else
280  out<<" "<<name<<"->SetLineColor("<<fLineColor<<");"<<std::endl;
281  }
282  if (fLineStyle != stydef) {
283  out<<" "<<name<<"->SetLineStyle("<<fLineStyle<<");"<<std::endl;
284  }
285  if (fLineWidth != widdef) {
286  out<<" "<<name<<"->SetLineWidth("<<fLineWidth<<");"<<std::endl;
287  }
288 }
289 
290 ////////////////////////////////////////////////////////////////////////////////
291 /// Invoke the DialogCanvas Line attributes.
292 
294 {
296 }
297 
298 ////////////////////////////////////////////////////////////////////////////////
299 /// Set a transparent line color. lalpha defines the percentage of
300 /// the color opacity from 0. (fully transparent) to 1. (fully opaque).
301 
303 {
304  fLineColor = TColor::GetColorTransparent(lcolor, lalpha);
305 }
c
#define c(i)
Definition: RSha256.hxx:101
TAttLine::fLineColor
Color_t fLineColor
Line color.
Definition: TAttLine.h:21
Style_t
short Style_t
Definition: RtypesCore.h:89
Option_t
const char Option_t
Definition: RtypesCore.h:66
gVirtualX
#define gVirtualX
Definition: TVirtualX.h:338
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
Float_t
float Float_t
Definition: RtypesCore.h:57
width
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
TAttLine::SetLineColorAlpha
virtual void SetLineColorAlpha(Color_t lcolor, Float_t lalpha)
Set a transparent line color.
Definition: TAttLine.cxx:302
TStyle.h
Int_t
int Int_t
Definition: RtypesCore.h:45
TAttLine::SaveLineAttributes
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:273
x
Double_t x[n]
Definition: legend1.C:17
TColor.h
TVirtualX.h
TMath::Abs
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
TAttLine::fLineWidth
Width_t fLineWidth
Line width.
Definition: TAttLine.h:23
TColor::SaveColor
static void SaveColor(std::ostream &out, Int_t ci)
Save a color with index > 228 as a C++ statement(s) on output stream out.
Definition: TColor.cxx:2127
Color_t
short Color_t
Definition: RtypesCore.h:92
v
@ v
Definition: rootcling_impl.cxx:3664
b
#define b(i)
Definition: RSha256.hxx:100
x1
static const double x1[5]
Definition: RooGaussKronrodIntegrator1D.cxx:346
TAttLine::Modify
virtual void Modify()
Change current line attributes if necessary.
Definition: TAttLine.cxx:245
TAttLine.h
style
TCanvas * style()
Definition: style.C:1
TAttLine::SetLineAttributes
virtual void SetLineAttributes()
Invoke the DialogCanvas Line attributes.
Definition: TAttLine.cxx:293
TAttLine::Copy
void Copy(TAttLine &attline) const
Copy this line attributes to a new TAttLine.
Definition: TAttLine.cxx:175
TVirtualPadEditor.h
TAttLine
Line Attributes class.
Definition: TAttLine.h:18
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TAttLine::GetLineColor
virtual Color_t GetLineColor() const
Return the line color.
Definition: TAttLine.h:33
a
auto * a
Definition: textangle.C:12
TAttLine::GetLineStyle
virtual Style_t GetLineStyle() const
Return the line style.
Definition: TAttLine.h:34
Width_t
short Width_t
Definition: RtypesCore.h:91
TAttLine::ResetAttLine
virtual void ResetAttLine(Option_t *option="")
Reset this line attributes to default values.
Definition: TAttLine.cxx:263
TVirtualPad.h
y
Double_t y[n]
Definition: legend1.C:17
sqrt
double sqrt(double)
TAttLine::~TAttLine
virtual ~TAttLine()
AttLine destructor.
Definition: TAttLine.cxx:168
TVirtualPadEditor::UpdateLineAttributes
static void UpdateLineAttributes(Int_t col, Int_t sty, Int_t width)
Update line attributes via the pad editor.
Definition: TVirtualPadEditor.cxx:152
Double_t
double Double_t
Definition: RtypesCore.h:59
TColor::GetColorTransparent
static Int_t GetColorTransparent(Int_t color, Float_t a)
Static function: Returns the transparent color number corresponding to n.
Definition: TColor.cxx:1985
TAttLine::TAttLine
TAttLine()
AttLine default constructor.
Definition: TAttLine.cxx:141
name
char name[80]
Definition: TGX11.cxx:110
d
#define d(i)
Definition: RSha256.hxx:102
x2
static const double x2[5]
Definition: RooGaussKronrodIntegrator1D.cxx:364
gPad
#define gPad
Definition: TVirtualPad.h:287
TAttLine::DistancetoLine
Int_t DistancetoLine(Int_t px, Int_t py, Double_t xp1, Double_t yp1, Double_t xp2, Double_t yp2)
Compute distance from point px,py to a line.
Definition: TAttLine.cxx:209
TAttLine::GetLineWidth
virtual Width_t GetLineWidth() const
Return the line width.
Definition: TAttLine.h:35
TAttLine::fLineStyle
Style_t fLineStyle
Line style.
Definition: TAttLine.h:22
int