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