Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
21using std::sqrt;
22
23/** \class TAttLine
24\ingroup Base
25\ingroup GraphicsAtt
26
27Line Attributes class.
28
29This class is used (in general by secondary inheritance)
30by many other classes (graphics, histograms). It holds all the line attributes.
31
32## Line attributes
33Line attributes are:
34
35 - [Line Color](\ref ATTLINE1)
36 - [Line Width](\ref ATTLINE2)
37 - [Line Style](\ref ATTLINE3)
38
39\anchor ATTLINE1
40## 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.
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 an opacity of 35% (i.e. a transparency of 65%).
60(The color `kBlue` itself is internally stored as fully opaque.)
61
62~~~ {.cpp}
63histo->SetLineColorAlpha(kBlue, 0.35);
64~~~
65
66The transparency is available on all platforms when the flag `OpenGL.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, TeX ... but not PostScript.
69
70Alternatively, you can call at the top of your script `gSytle->SetCanvasPreferGL();`.
71Or if you prefer to activate GL for a single canvas `c`, then use `c->SetSupportGL(true);`.
72
73\anchor ATTLINE2
74## Line Width
75The line width is expressed in pixel units.
76The line width of any class inheriting from `TAttLine` can
77be changed using the method `SetLineWidth` and retrieved using the
78method `GetLineWidth`.
79The following picture shows the line widths from 1 to 10 pixels.
80
81Begin_Macro
82{
83 TCanvas *Lw = new TCanvas("Lw","test",500,200);
84 TText t;
85 t.SetTextAlign(32);
86 t.SetTextSize(0.08);
87 Int_t i=1;
88 for (float s=0.1; s<1.0 ; s+=0.092) {
89 TLine *lh = new TLine(0.15,s,.85,s);
90 lh->SetLineWidth(i);
91 t.DrawText(0.1,s,Form("%d",i++));
92 lh->Draw();
93 }
94}
95End_Macro
96
97\anchor ATTLINE3
98## Line Style
99Line styles are identified via integer numbers. The line style of any class
100inheriting from `TAttLine` can be changed using the method
101`SetLineStyle` and retrieved using the method `GetLineStyle`.
102
103The first 10 line styles are predefined as shown on the following picture:
104
105Begin_Macro
106{
107 TCanvas *Ls = new TCanvas("Ls","test",500,200);
108 TText t;
109 t.SetTextAlign(32);
110 t.SetTextSize(0.08);
111 Int_t i=1;
112 for (float s=0.1; s<1.0 ; s+=0.092) {
113 TLine *lh = new TLine(0.15,s,.85,s);
114 lh->SetLineStyle(i);
115 lh->SetLineWidth(3);
116 t.DrawText(0.1,s,Form("%d",i++));
117 lh->Draw();
118 }
119}
120End_Macro
121
122Some line styles can be accessed via the following enum:
123
124~~~ {.cpp}
125 kSolid = 1
126 kDashed = 2
127 kDotted = 3
128 kDashDotted = 4
129~~~
130
131Additional line styles can be defined using `TStyle::SetLineStyleString`.
132For example the line style number 11 can be defined as follow:
133~~~ {.cpp}
134 gStyle->SetLineStyleString(11,"400 200");
135~~~
136Existing line styles (1 to 10) can be redefined using the same method.
137 */
138
139////////////////////////////////////////////////////////////////////////////////
140/// AttLine default constructor.
141
149
150////////////////////////////////////////////////////////////////////////////////
151/// AttLine normal constructor.
152/// Line attributes are taking from the argument list
153///
154/// - color : must be one of the valid color index
155/// - style : 1=solid, 2=dash, 3=dash-dot, 4=dot-dot. New styles can be
156/// defined using TStyle::SetLineStyleString.
157/// - width : expressed in pixel units
158
165
166////////////////////////////////////////////////////////////////////////////////
167/// AttLine destructor.
168
172
173////////////////////////////////////////////////////////////////////////////////
174/// Copy this line attributes to a new TAttLine.
175
177{
178 attline.fLineColor = fLineColor;
179 attline.fLineStyle = fLineStyle;
180 attline.fLineWidth = fLineWidth;
181}
182
183////////////////////////////////////////////////////////////////////////////////
184/// Compute distance from point px,py to a line.
185/// Compute the closest distance of approach from point px,py to this line.
186/// The distance is computed in pixels units.
187///
188/// Algorithm:
189///~~~ {.cpp}
190/// A(x1,y1) P B(x2,y2)
191/// -----------------+------------------------------
192/// |
193/// |
194/// |
195/// |
196/// M(x,y)
197///
198/// Let us call a = distance AM A=a**2
199/// b = distance BM B=b**2
200/// c = distance AB C=c**2
201/// d = distance PM D=d**2
202/// u = distance AP U=u**2
203/// v = distance BP V=v**2 c = u + v
204///
205/// D = A - U
206/// D = B - V = B -(c-u)**2
207/// ==> u = (A -B +C)/2c
208///~~~
209
211{
212 Double_t xl, xt, yl, yt;
213 Double_t x = px;
214 Double_t y = py;
215 Double_t x1 = gPad->XtoAbsPixel(xp1);
216 Double_t y1 = gPad->YtoAbsPixel(yp1);
217 Double_t x2 = gPad->XtoAbsPixel(xp2);
218 Double_t y2 = gPad->YtoAbsPixel(yp2);
219 if (x1 < x2) {xl = x1; xt = x2;}
220 else {xl = x2; xt = x1;}
221 if (y1 < y2) {yl = y1; yt = y2;}
222 else {yl = y2; yt = y1;}
223 if (x < xl-2 || x> xt+2) return 9999; //following algorithm only valid in the box
224 if (y < yl-2 || y> yt+2) return 9999; //surrounding the line
225 Double_t xx1 = x - x1;
226 Double_t xx2 = x - x2;
227 Double_t x1x2 = x1 - x2;
228 Double_t yy1 = y - y1;
229 Double_t yy2 = y - y2;
230 Double_t y1y2 = y1 - y2;
231 Double_t a = xx1*xx1 + yy1*yy1;
232 Double_t b = xx2*xx2 + yy2*yy2;
234 if (c <= 0) return 9999;
235 Double_t v = sqrt(c);
236 Double_t u = (a - b + c)/(2*v);
237 Double_t d = std::abs(a - u*u);
238 if (d < 0) return 9999;
239
240 return Int_t(sqrt(d) - 0.5*Double_t(fLineWidth));
241}
242
243////////////////////////////////////////////////////////////////////////////////
244/// Change current line attributes if necessary.
245
247{
248 if (!gPad) return;
249 Int_t lineWidth = std::abs(fLineWidth%100);
250 if (!gPad->IsBatch()) {
251 gVirtualX->SetLineColor(fLineColor);
252 if (fLineStyle > 0 && fLineStyle < 30) gVirtualX->SetLineStyle(fLineStyle);
253 else gVirtualX->SetLineStyle(1);
254 gVirtualX->SetLineWidth(lineWidth);
255 }
256
257 if (fLineStyle > 0 && fLineStyle < 30) gPad->SetAttLinePS(fLineColor,fLineStyle,lineWidth);
258 else gPad->SetAttLinePS(fLineColor,1,lineWidth);
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// Reset this line attributes to default values.
263
265{
266 fLineColor = 1;
267 fLineStyle = 1;
268 fLineWidth = 1;
269}
270
271////////////////////////////////////////////////////////////////////////////////
272/// Save line attributes as C++ statement(s) on output stream out.
273
274void TAttLine::SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef, Int_t stydef, Int_t widdef)
275{
276 if (fLineColor != coldef)
277 out << " " << name << "->SetLineColor(" << TColor::SavePrimitiveColor(fLineColor) << ");\n";
278 if (fLineStyle != stydef)
279 out << " " << name << "->SetLineStyle(" << fLineStyle << ");\n";
280 if (fLineWidth != widdef)
281 out << " " << name << "->SetLineWidth(" << fLineWidth << ");\n";
282}
283
284////////////////////////////////////////////////////////////////////////////////
285/// Invoke the DialogCanvas Line attributes.
286
291
292////////////////////////////////////////////////////////////////////////////////
293/// Set a transparent line color.
294/// \param lcolor defines the line color
295/// \param lalpha defines the percentage of opacity from 0. (fully transparent) to 1. (fully opaque).
296/// \note lalpha is ignored (treated as 1) if the TCanvas has no GL support activated.
297
302
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
short Style_t
Style number (short)
Definition RtypesCore.h:96
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
short Color_t
Color number (short)
Definition RtypesCore.h:99
short Width_t
Line width (short)
Definition RtypesCore.h:98
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 SetLineColor
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 width
Option_t Option_t style
Option_t Option_t TPoint TPoint const char y1
char name[80]
Definition TGX11.cxx:110
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
#define gPad
#define gVirtualX
Definition TVirtualX.h:337
Line Attributes class.
Definition TAttLine.h:20
virtual Color_t GetLineColor() const
Return the line color.
Definition TAttLine.h:35
virtual Width_t GetLineWidth() const
Return the line width.
Definition TAttLine.h:37
virtual void SetLineAttributes()
Invoke the DialogCanvas Line attributes.
Definition TAttLine.cxx:287
virtual void SetLineColorAlpha(Color_t lcolor, Float_t lalpha)
Set a transparent line color.
Definition TAttLine.cxx:298
Width_t fLineWidth
Line width.
Definition TAttLine.h:25
virtual void ResetAttLine(Option_t *option="")
Reset this line attributes to default values.
Definition TAttLine.cxx:264
virtual ~TAttLine()
AttLine destructor.
Definition TAttLine.cxx:169
TAttLine()
AttLine default constructor.
Definition TAttLine.cxx:142
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:42
virtual Style_t GetLineStyle() const
Return the line style.
Definition TAttLine.h:36
virtual void Modify()
Change current line attributes if necessary.
Definition TAttLine.cxx:246
Style_t fLineStyle
Line style.
Definition TAttLine.h:24
void Copy(TAttLine &attline) const
Copy this line attributes to a new TAttLine.
Definition TAttLine.cxx:176
Color_t fLineColor
Line color.
Definition TAttLine.h:23
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:210
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:274
static TString SavePrimitiveColor(Int_t ci)
Convert color in C++ statement which can be used in SetColor directives Produced statement either inc...
Definition TColor.cxx:2556
static Int_t GetColorTransparent(Int_t color, Float_t a)
Static function: Returns the transparent color number corresponding to n.
Definition TColor.cxx:2182
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