00001 // @(#)root/base:$Id: TAttLine.cxx 34859 2010-08-19 10:56:34Z brun $ 00002 // Author: Rene Brun 28/11/94 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 #include "Riostream.h" 00013 #include "TAttLine.h" 00014 #include "TVirtualPad.h" 00015 #include "TStyle.h" 00016 #include "TVirtualX.h" 00017 #include "TVirtualPadEditor.h" 00018 #include "TColor.h" 00019 #include <cmath> 00020 00021 00022 ClassImp(TAttLine) 00023 00024 00025 //______________________________________________________________________________ 00026 /* Begin_Html 00027 <center><h2>Line Attributes class</h2></center> 00028 00029 This class is used (in general by secondary inheritance) 00030 by many other classes (graphics, histograms). It holds all the line attributes. 00031 00032 <h3>Line attributes</h3> 00033 Line attributes are: 00034 <ul> 00035 <li><a href="#L1">Line Color.</a></li> 00036 <li><a href="#L2">Line Width.</a></li> 00037 <li><a href="#L3">Line Style.</a></li> 00038 </ul> 00039 00040 <a name="L1"></a><h3>Line Color</h3> 00041 The line color is a color index (integer) pointing in the ROOT 00042 color table. 00043 The line color of any class inheriting from <tt>TAttLine</tt> can 00044 be changed using the method <tt>SetLineColor</tt> and retrieved using the 00045 method <tt>GetLineColor</tt>. 00046 The following table shows the first 50 default colors. 00047 End_Html 00048 Begin_Macro(source) 00049 { 00050 TCanvas *c = new TCanvas("c","Line colors",0,0,500,200); 00051 c.DrawColorTable(); 00052 return c; 00053 } 00054 End_Macro 00055 00056 Begin_Html 00057 <a name="L2"></a><h3>Line Width</h3> 00058 The line width is expressed in pixel units. 00059 The line width of any class inheriting from <tt>TAttLine</tt> can 00060 be changed using the method <tt>SetLineWidth</tt> and retrieved using the 00061 method <tt>GetLineWidth</tt>. 00062 The following picture shows the line widths from 1 to 10 pixels. 00063 End_Html 00064 Begin_Macro(source) 00065 { 00066 TCanvas *Lw = new TCanvas("Lw","test",500,200); 00067 TText t; 00068 t.SetTextAlign(32); 00069 t.SetTextSize(0.08); 00070 Int_t i=1; 00071 for (float s=0.1; s<1.0 ; s+=0.092) { 00072 TLine *lh = new TLine(0.15,s,.85,s); 00073 lh->SetLineWidth(i); 00074 t.DrawText(0.1,s,Form("%d",i++)); 00075 lh->Draw(); 00076 } 00077 return Lw; 00078 } 00079 End_Macro 00080 00081 Begin_Html 00082 <a name="L3"></a><h3>Line Style</h3> 00083 Line styles are identified via integer numbers. The line style of any class 00084 inheriting from <tt>TAttLine</tt> can be changed using the method 00085 <tt>SetLineStyle</tt> and retrieved using the method <tt>GetLineStyle</tt>. 00086 <br> 00087 The first 10 line styles are predefined as shown on the following picture: 00088 End_Html 00089 Begin_Macro(source) 00090 { 00091 TCanvas *Ls = new TCanvas("Ls","test",500,200); 00092 TText t; 00093 t.SetTextAlign(32); 00094 t.SetTextSize(0.08); 00095 Int_t i=1; 00096 for (float s=0.1; s<1.0 ; s+=0.092) { 00097 TLine *lh = new TLine(0.15,s,.85,s); 00098 lh->SetLineStyle(i); 00099 t.DrawText(0.1,s,Form("%d",i++)); 00100 lh->Draw(); 00101 } 00102 return Ls; 00103 } 00104 End_Macro 00105 00106 Begin_Html 00107 Additional line styles can be defined using <tt>TStyle::SetLineStyleString</tt>. 00108 <br>For example the line style number 11 can be defined as follow: 00109 <pre> 00110 gStyle->SetLineStyleString(11,"400 200"); 00111 </pre> 00112 Existing line styles (1 to 10) can be redefined using the same method. 00113 End_Html */ 00114 00115 00116 //______________________________________________________________________________ 00117 TAttLine::TAttLine() 00118 { 00119 // AttLine default constructor. 00120 00121 if (!gStyle) {fLineColor=1; fLineWidth=1; fLineStyle=1; return;} 00122 fLineColor = gStyle->GetLineColor(); 00123 fLineWidth = gStyle->GetLineWidth(); 00124 fLineStyle = gStyle->GetLineStyle(); 00125 } 00126 00127 00128 //______________________________________________________________________________ 00129 TAttLine::TAttLine(Color_t color, Style_t style, Width_t width) 00130 { 00131 // AttLine normal constructor. 00132 // Line attributes are taking from the argument list 00133 // color : must be one of the valid color index 00134 // style : 1=solid, 2=dash, 3=dash-dot, 4=dot-dot. New styles can be 00135 // defined using TStyle::SetLineStyleString. 00136 // width : expressed in pixel units 00137 00138 fLineColor = color; 00139 fLineWidth = width; 00140 fLineStyle = style; 00141 } 00142 00143 00144 //______________________________________________________________________________ 00145 TAttLine::~TAttLine() 00146 { 00147 // AttLine destructor. 00148 } 00149 00150 00151 //______________________________________________________________________________ 00152 void TAttLine::Copy(TAttLine &attline) const 00153 { 00154 // Copy this line attributes to a new TAttLine. 00155 00156 attline.fLineColor = fLineColor; 00157 attline.fLineStyle = fLineStyle; 00158 attline.fLineWidth = fLineWidth; 00159 } 00160 00161 00162 //______________________________________________________________________________ 00163 Int_t TAttLine::DistancetoLine(Int_t px, Int_t py, Double_t xp1, Double_t yp1, Double_t xp2, Double_t yp2 ) 00164 { 00165 // Compute distance from point px,py to a line. 00166 // Compute the closest distance of approach from point px,py to this line. 00167 // The distance is computed in pixels units. 00168 // 00169 // Algorithm: 00170 // 00171 // A(x1,y1) P B(x2,y2) 00172 // -----------------+------------------------------ 00173 // | 00174 // | 00175 // | 00176 // | 00177 // M(x,y) 00178 // 00179 // Let us call a = distance AM A=a**2 00180 // b = distance BM B=b**2 00181 // c = distance AB C=c**2 00182 // d = distance PM D=d**2 00183 // u = distance AP U=u**2 00184 // v = distance BP V=v**2 c = u + v 00185 // 00186 // D = A - U 00187 // D = B - V = B -(c-u)**2 00188 // ==> u = (A -B +C)/2c 00189 00190 Double_t xl, xt, yl, yt; 00191 Double_t x = px; 00192 Double_t y = py; 00193 Double_t x1 = gPad->XtoAbsPixel(xp1); 00194 Double_t y1 = gPad->YtoAbsPixel(yp1); 00195 Double_t x2 = gPad->XtoAbsPixel(xp2); 00196 Double_t y2 = gPad->YtoAbsPixel(yp2); 00197 if (x1 < x2) {xl = x1; xt = x2;} 00198 else {xl = x2; xt = x1;} 00199 if (y1 < y2) {yl = y1; yt = y2;} 00200 else {yl = y2; yt = y1;} 00201 if (x < xl-2 || x> xt+2) return 9999; //following algorithm only valid in the box 00202 if (y < yl-2 || y> yt+2) return 9999; //surrounding the line 00203 Double_t xx1 = x - x1; 00204 Double_t xx2 = x - x2; 00205 Double_t x1x2 = x1 - x2; 00206 Double_t yy1 = y - y1; 00207 Double_t yy2 = y - y2; 00208 Double_t y1y2 = y1 - y2; 00209 Double_t a = xx1*xx1 + yy1*yy1; 00210 Double_t b = xx2*xx2 + yy2*yy2; 00211 Double_t c = x1x2*x1x2 + y1y2*y1y2; 00212 if (c <= 0) return 9999; 00213 Double_t v = sqrt(c); 00214 Double_t u = (a - b + c)/(2*v); 00215 Double_t d = TMath::Abs(a - u*u); 00216 if (d < 0) return 9999; 00217 00218 return Int_t(sqrt(d) - 0.5*Double_t(fLineWidth)); 00219 } 00220 00221 00222 //______________________________________________________________________________ 00223 void TAttLine::Modify() 00224 { 00225 // Change current line attributes if necessary. 00226 00227 if (!gPad) return; 00228 Int_t lineWidth = TMath::Abs(fLineWidth%100); 00229 if (!gPad->IsBatch()) { 00230 gVirtualX->SetLineColor(fLineColor); 00231 if (fLineStyle > 0 && fLineStyle < 30) gVirtualX->SetLineStyle(fLineStyle); 00232 else gVirtualX->SetLineStyle(1); 00233 gVirtualX->SetLineWidth(lineWidth); 00234 } 00235 00236 if (fLineStyle > 0 && fLineStyle < 30) gPad->SetAttLinePS(fLineColor,fLineStyle,lineWidth); 00237 else gPad->SetAttLinePS(fLineColor,1,lineWidth); 00238 } 00239 00240 00241 //______________________________________________________________________________ 00242 void TAttLine::ResetAttLine(Option_t *) 00243 { 00244 // Reset this line attributes to default values. 00245 00246 fLineColor = 1; 00247 fLineStyle = 1; 00248 fLineWidth = 1; 00249 } 00250 00251 00252 //______________________________________________________________________________ 00253 void TAttLine::SaveLineAttributes(ostream &out, const char *name, Int_t coldef, Int_t stydef, Int_t widdef) 00254 { 00255 // Save line attributes as C++ statement(s) on output stream out. 00256 00257 if (fLineColor != coldef) { 00258 if (fLineColor > 228) { 00259 TColor::SaveColor(out, fLineColor); 00260 out<<" "<<name<<"->SetLineColor(ci);" << endl; 00261 } else 00262 out<<" "<<name<<"->SetLineColor("<<fLineColor<<");"<<endl; 00263 } 00264 if (fLineStyle != stydef) { 00265 out<<" "<<name<<"->SetLineStyle("<<fLineStyle<<");"<<endl; 00266 } 00267 if (fLineWidth != widdef) { 00268 out<<" "<<name<<"->SetLineWidth("<<fLineWidth<<");"<<endl; 00269 } 00270 } 00271 00272 00273 //______________________________________________________________________________ 00274 void TAttLine::SetLineAttributes() 00275 { 00276 // Invoke the DialogCanvas Line attributes. 00277 00278 TVirtualPadEditor::UpdateLineAttributes(fLineColor,fLineStyle,fLineWidth); 00279 }