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