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