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