ROOT logo
// @(#)root/base:$Id$
// Author: Rene Brun   28/11/94

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Riostream.h"
#include "TAttLine.h"
#include "TVirtualPad.h"
#include "TStyle.h"
#include "TVirtualX.h"
#include "TVirtualPadEditor.h"
#include "TColor.h"
#include <cmath>


ClassImp(TAttLine)


//______________________________________________________________________________
/* Begin_Html
<center><h2>Line Attributes class</h2></center>

This class is used (in general by secondary inheritance)
by many other classes (graphics, histograms). It holds all the line attributes.

<h3>Line attributes</h3>
Line attributes are:
<ul>
<li><a href="#L1">Line Color.</a></li>
<li><a href="#L2">Line Width.</a></li>
<li><a href="#L3">Line Style.</a></li>
</ul>

<a name="L1"></a><h3>Line Color</h3>
The line color is a color index (integer) pointing in the ROOT
color table.
The line color of any class inheriting from <tt>TAttLine</tt> can
be changed using the method <tt>SetLineColor</tt> and retrieved using the
method <tt>GetLineColor</tt>.
The following table shows the first 50 default colors.
End_Html
Begin_Macro(source)
{
   TCanvas *c = new TCanvas("c","Line colors",0,0,500,200);
   c.DrawColorTable();
   return c;
}
End_Macro

Begin_Html

<h4>Color transparency</h4>
<tt>SetLineColorAlpha()</tt>, allows to set a transparent color.
In the following example the line color of the histogram <tt>histo</tt>
is set to blue with a transparency of 35%. The color <tt>kBlue</tt>
itself remains fully opaque. 
<p>
<pre>
histo->SetLineColorAlpha(kBlue, 0.35);
</pre>
<p>  
The transparency is available on all platforms when the <tt>flagOpenGL.CanvasPreferGL</tt> is set to <tt>1</tt>
in <tt>$ROOTSYS/etc/system.rootrc</tt>, or on Mac with the Cocoa backend. On the file output
it is visible with PDF, PNG, Gif, JPEG, SVG ... but not PostScript.


<a name="L2"></a><h3>Line Width</h3>
The line width is expressed in pixel units.
The line width of any class inheriting from <tt>TAttLine</tt> can
be changed using the method <tt>SetLineWidth</tt> and retrieved using the
method <tt>GetLineWidth</tt>.
The following picture shows the line widths from 1 to 10 pixels.
End_Html
Begin_Macro(source)
{
   TCanvas *Lw = new TCanvas("Lw","test",500,200);
   TText  t;
   t.SetTextAlign(32);
   t.SetTextSize(0.08);
   Int_t i=1;
   for (float s=0.1; s<1.0 ; s+=0.092) {
      TLine *lh = new TLine(0.15,s,.85,s);
      lh->SetLineWidth(i);
      t.DrawText(0.1,s,Form("%d",i++));
      lh->Draw();
   }
   return Lw;
}
End_Macro

Begin_Html
<a name="L3"></a><h3>Line Style</h3>
Line styles are identified via integer numbers. The line style of any class
inheriting from <tt>TAttLine</tt> can be changed using the method
<tt>SetLineStyle</tt> and retrieved using the method <tt>GetLineStyle</tt>.
<br>
The first 10 line styles are predefined as shown on the following picture:
End_Html
Begin_Macro(source)
{
   TCanvas *Ls = new TCanvas("Ls","test",500,200);
   TText  t;
   t.SetTextAlign(32);
   t.SetTextSize(0.08);
   Int_t i=1;
   for (float s=0.1; s<1.0 ; s+=0.092) {
      TLine *lh = new TLine(0.15,s,.85,s);
      lh->SetLineStyle(i);
      t.DrawText(0.1,s,Form("%d",i++));
      lh->Draw();
   }
   return Ls;
}
End_Macro

Begin_Html
Additional line styles can be defined using <tt>TStyle::SetLineStyleString</tt>.
<br>For example the line style number 11 can be defined as follow:
<pre>
   gStyle->SetLineStyleString(11,"400 200");
</pre>
Existing line styles (1 to 10) can be redefined using the same method.
End_Html */


//______________________________________________________________________________
TAttLine::TAttLine()
{
   // AttLine default constructor.

   if (!gStyle) {fLineColor=1; fLineWidth=1; fLineStyle=1; return;}
   fLineColor = gStyle->GetLineColor();
   fLineWidth = gStyle->GetLineWidth();
   fLineStyle = gStyle->GetLineStyle();
}


//______________________________________________________________________________
TAttLine::TAttLine(Color_t color, Style_t style, Width_t width)
{
   // AttLine normal constructor.
   // Line attributes are taking from the argument list
   //   color : must be one of the valid color index
   //   style : 1=solid, 2=dash, 3=dash-dot, 4=dot-dot. New styles can be
   //           defined using TStyle::SetLineStyleString.
   //   width : expressed in pixel units

   fLineColor = color;
   fLineWidth = width;
   fLineStyle = style;
}


//______________________________________________________________________________
TAttLine::~TAttLine()
{
   // AttLine destructor.
}


//______________________________________________________________________________
void TAttLine::Copy(TAttLine &attline) const
{
   // Copy this line attributes to a new TAttLine.

   attline.fLineColor  = fLineColor;
   attline.fLineStyle  = fLineStyle;
   attline.fLineWidth  = fLineWidth;
}


//______________________________________________________________________________
Int_t TAttLine::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.
   // Compute the closest distance of approach from point px,py to this line.
   // The distance is computed in pixels units.
   //
   // Algorithm:
   //
   //   A(x1,y1)         P                             B(x2,y2)
   //   -----------------+------------------------------
   //                    |
   //                    |
   //                    |
   //                    |
   //                   M(x,y)
   //
   // Let us call  a = distance AM     A=a**2
   //              b = distance BM     B=b**2
   //              c = distance AB     C=c**2
   //              d = distance PM     D=d**2
   //              u = distance AP     U=u**2
   //              v = distance BP     V=v**2     c = u + v
   //
   // D = A - U
   // D = B - V  = B -(c-u)**2
   //    ==> u = (A -B +C)/2c

   Double_t xl, xt, yl, yt;
   Double_t x     = px;
   Double_t y     = py;
   Double_t x1    = gPad->XtoAbsPixel(xp1);
   Double_t y1    = gPad->YtoAbsPixel(yp1);
   Double_t x2    = gPad->XtoAbsPixel(xp2);
   Double_t y2    = gPad->YtoAbsPixel(yp2);
   if (x1 < x2) {xl = x1; xt = x2;}
   else         {xl = x2; xt = x1;}
   if (y1 < y2) {yl = y1; yt = y2;}
   else         {yl = y2; yt = y1;}
   if (x < xl-2 || x> xt+2) return 9999;  //following algorithm only valid in the box
   if (y < yl-2 || y> yt+2) return 9999;  //surrounding the line
   Double_t xx1   = x  - x1;
   Double_t xx2   = x  - x2;
   Double_t x1x2  = x1 - x2;
   Double_t yy1   = y  - y1;
   Double_t yy2   = y  - y2;
   Double_t y1y2  = y1 - y2;
   Double_t a     = xx1*xx1   + yy1*yy1;
   Double_t b     = xx2*xx2   + yy2*yy2;
   Double_t c     = x1x2*x1x2 + y1y2*y1y2;
   if (c <= 0)  return 9999;
   Double_t v     = sqrt(c);
   Double_t u     = (a - b + c)/(2*v);
   Double_t d     = TMath::Abs(a - u*u);
   if (d < 0)   return 9999;

   return Int_t(sqrt(d) - 0.5*Double_t(fLineWidth));
}


//______________________________________________________________________________
void TAttLine::Modify()
{
   // Change current line attributes if necessary.

   if (!gPad) return;
   Int_t lineWidth = TMath::Abs(fLineWidth%100);
   if (!gPad->IsBatch()) {
      gVirtualX->SetLineColor(fLineColor);
      if (fLineStyle > 0 && fLineStyle < 30) gVirtualX->SetLineStyle(fLineStyle);
      else                                   gVirtualX->SetLineStyle(1);
      gVirtualX->SetLineWidth(lineWidth);
   }

   if (fLineStyle > 0 && fLineStyle < 30) gPad->SetAttLinePS(fLineColor,fLineStyle,lineWidth);
   else                                   gPad->SetAttLinePS(fLineColor,1,lineWidth);
}


//______________________________________________________________________________
void TAttLine::ResetAttLine(Option_t *)
{
   // Reset this line attributes to default values.

   fLineColor  = 1;
   fLineStyle  = 1;
   fLineWidth  = 1;
}


//______________________________________________________________________________
void TAttLine::SaveLineAttributes(ostream &out, const char *name, Int_t coldef, Int_t stydef, Int_t widdef)
{
   // Save line attributes as C++ statement(s) on output stream out.

   if (fLineColor != coldef) {
      if (fLineColor > 228) {
         TColor::SaveColor(out, fLineColor);
         out<<"   "<<name<<"->SetLineColor(ci);" << endl;
      } else
         out<<"   "<<name<<"->SetLineColor("<<fLineColor<<");"<<endl;
   }
   if (fLineStyle != stydef) {
      out<<"   "<<name<<"->SetLineStyle("<<fLineStyle<<");"<<endl;
   }
   if (fLineWidth != widdef) {
      out<<"   "<<name<<"->SetLineWidth("<<fLineWidth<<");"<<endl;
   }
}


//______________________________________________________________________________
void TAttLine::SetLineAttributes()
{
   // Invoke the DialogCanvas Line attributes.

   TVirtualPadEditor::UpdateLineAttributes(fLineColor,fLineStyle,fLineWidth);
}


//______________________________________________________________________________
void TAttLine::SetLineColorAlpha(Color_t lcolor, Float_t lalpha)
{
   // Set a transparent line color. lalpha defines the percentage of
   // the color opacity from 0. (fully transparent) to 1. (fully opaque).

   fLineColor = TColor::GetColorTransparent(lcolor, lalpha);
 TAttLine.cxx:1
 TAttLine.cxx:2
 TAttLine.cxx:3
 TAttLine.cxx:4
 TAttLine.cxx:5
 TAttLine.cxx:6
 TAttLine.cxx:7
 TAttLine.cxx:8
 TAttLine.cxx:9
 TAttLine.cxx:10
 TAttLine.cxx:11
 TAttLine.cxx:12
 TAttLine.cxx:13
 TAttLine.cxx:14
 TAttLine.cxx:15
 TAttLine.cxx:16
 TAttLine.cxx:17
 TAttLine.cxx:18
 TAttLine.cxx:19
 TAttLine.cxx:20
 TAttLine.cxx:21
 TAttLine.cxx:22
 TAttLine.cxx:23
 TAttLine.cxx:24
 TAttLine.cxx:25
 TAttLine.cxx:26
 TAttLine.cxx:27
 TAttLine.cxx:28
 TAttLine.cxx:29
 TAttLine.cxx:30
 TAttLine.cxx:31
 TAttLine.cxx:32
 TAttLine.cxx:33
 TAttLine.cxx:34
 TAttLine.cxx:35
 TAttLine.cxx:36
 TAttLine.cxx:37
 TAttLine.cxx:38
 TAttLine.cxx:39
 TAttLine.cxx:40
 TAttLine.cxx:41
 TAttLine.cxx:42
 TAttLine.cxx:43
 TAttLine.cxx:44
 TAttLine.cxx:45
 TAttLine.cxx:46
 TAttLine.cxx:47
 TAttLine.cxx:48
 TAttLine.cxx:49
 TAttLine.cxx:50
 TAttLine.cxx:51
 TAttLine.cxx:52
 TAttLine.cxx:53
 TAttLine.cxx:54
 TAttLine.cxx:55
 TAttLine.cxx:56
 TAttLine.cxx:57
 TAttLine.cxx:58
 TAttLine.cxx:59
 TAttLine.cxx:60
 TAttLine.cxx:61
 TAttLine.cxx:62
 TAttLine.cxx:63
 TAttLine.cxx:64
 TAttLine.cxx:65
 TAttLine.cxx:66
 TAttLine.cxx:67
 TAttLine.cxx:68
 TAttLine.cxx:69
 TAttLine.cxx:70
 TAttLine.cxx:71
 TAttLine.cxx:72
 TAttLine.cxx:73
 TAttLine.cxx:74
 TAttLine.cxx:75
 TAttLine.cxx:76
 TAttLine.cxx:77
 TAttLine.cxx:78
 TAttLine.cxx:79
 TAttLine.cxx:80
 TAttLine.cxx:81
 TAttLine.cxx:82
 TAttLine.cxx:83
 TAttLine.cxx:84
 TAttLine.cxx:85
 TAttLine.cxx:86
 TAttLine.cxx:87
 TAttLine.cxx:88
 TAttLine.cxx:89
 TAttLine.cxx:90
 TAttLine.cxx:91
 TAttLine.cxx:92
 TAttLine.cxx:93
 TAttLine.cxx:94
 TAttLine.cxx:95
 TAttLine.cxx:96
 TAttLine.cxx:97
 TAttLine.cxx:98
 TAttLine.cxx:99
 TAttLine.cxx:100
 TAttLine.cxx:101
 TAttLine.cxx:102
 TAttLine.cxx:103
 TAttLine.cxx:104
 TAttLine.cxx:105
 TAttLine.cxx:106
 TAttLine.cxx:107
 TAttLine.cxx:108
 TAttLine.cxx:109
 TAttLine.cxx:110
 TAttLine.cxx:111
 TAttLine.cxx:112
 TAttLine.cxx:113
 TAttLine.cxx:114
 TAttLine.cxx:115
 TAttLine.cxx:116
 TAttLine.cxx:117
 TAttLine.cxx:118
 TAttLine.cxx:119
 TAttLine.cxx:120
 TAttLine.cxx:121
 TAttLine.cxx:122
 TAttLine.cxx:123
 TAttLine.cxx:124
 TAttLine.cxx:125
 TAttLine.cxx:126
 TAttLine.cxx:127
 TAttLine.cxx:128
 TAttLine.cxx:129
 TAttLine.cxx:130
 TAttLine.cxx:131
 TAttLine.cxx:132
 TAttLine.cxx:133
 TAttLine.cxx:134
 TAttLine.cxx:135
 TAttLine.cxx:136
 TAttLine.cxx:137
 TAttLine.cxx:138
 TAttLine.cxx:139
 TAttLine.cxx:140
 TAttLine.cxx:141
 TAttLine.cxx:142
 TAttLine.cxx:143
 TAttLine.cxx:144
 TAttLine.cxx:145
 TAttLine.cxx:146
 TAttLine.cxx:147
 TAttLine.cxx:148
 TAttLine.cxx:149
 TAttLine.cxx:150
 TAttLine.cxx:151
 TAttLine.cxx:152
 TAttLine.cxx:153
 TAttLine.cxx:154
 TAttLine.cxx:155
 TAttLine.cxx:156
 TAttLine.cxx:157
 TAttLine.cxx:158
 TAttLine.cxx:159
 TAttLine.cxx:160
 TAttLine.cxx:161
 TAttLine.cxx:162
 TAttLine.cxx:163
 TAttLine.cxx:164
 TAttLine.cxx:165
 TAttLine.cxx:166
 TAttLine.cxx:167
 TAttLine.cxx:168
 TAttLine.cxx:169
 TAttLine.cxx:170
 TAttLine.cxx:171
 TAttLine.cxx:172
 TAttLine.cxx:173
 TAttLine.cxx:174
 TAttLine.cxx:175
 TAttLine.cxx:176
 TAttLine.cxx:177
 TAttLine.cxx:178
 TAttLine.cxx:179
 TAttLine.cxx:180
 TAttLine.cxx:181
 TAttLine.cxx:182
 TAttLine.cxx:183
 TAttLine.cxx:184
 TAttLine.cxx:185
 TAttLine.cxx:186
 TAttLine.cxx:187
 TAttLine.cxx:188
 TAttLine.cxx:189
 TAttLine.cxx:190
 TAttLine.cxx:191
 TAttLine.cxx:192
 TAttLine.cxx:193
 TAttLine.cxx:194
 TAttLine.cxx:195
 TAttLine.cxx:196
 TAttLine.cxx:197
 TAttLine.cxx:198
 TAttLine.cxx:199
 TAttLine.cxx:200
 TAttLine.cxx:201
 TAttLine.cxx:202
 TAttLine.cxx:203
 TAttLine.cxx:204
 TAttLine.cxx:205
 TAttLine.cxx:206
 TAttLine.cxx:207
 TAttLine.cxx:208
 TAttLine.cxx:209
 TAttLine.cxx:210
 TAttLine.cxx:211
 TAttLine.cxx:212
 TAttLine.cxx:213
 TAttLine.cxx:214
 TAttLine.cxx:215
 TAttLine.cxx:216
 TAttLine.cxx:217
 TAttLine.cxx:218
 TAttLine.cxx:219
 TAttLine.cxx:220
 TAttLine.cxx:221
 TAttLine.cxx:222
 TAttLine.cxx:223
 TAttLine.cxx:224
 TAttLine.cxx:225
 TAttLine.cxx:226
 TAttLine.cxx:227
 TAttLine.cxx:228
 TAttLine.cxx:229
 TAttLine.cxx:230
 TAttLine.cxx:231
 TAttLine.cxx:232
 TAttLine.cxx:233
 TAttLine.cxx:234
 TAttLine.cxx:235
 TAttLine.cxx:236
 TAttLine.cxx:237
 TAttLine.cxx:238
 TAttLine.cxx:239
 TAttLine.cxx:240
 TAttLine.cxx:241
 TAttLine.cxx:242
 TAttLine.cxx:243
 TAttLine.cxx:244
 TAttLine.cxx:245
 TAttLine.cxx:246
 TAttLine.cxx:247
 TAttLine.cxx:248
 TAttLine.cxx:249
 TAttLine.cxx:250
 TAttLine.cxx:251
 TAttLine.cxx:252
 TAttLine.cxx:253
 TAttLine.cxx:254
 TAttLine.cxx:255
 TAttLine.cxx:256
 TAttLine.cxx:257
 TAttLine.cxx:258
 TAttLine.cxx:259
 TAttLine.cxx:260
 TAttLine.cxx:261
 TAttLine.cxx:262
 TAttLine.cxx:263
 TAttLine.cxx:264
 TAttLine.cxx:265
 TAttLine.cxx:266
 TAttLine.cxx:267
 TAttLine.cxx:268
 TAttLine.cxx:269
 TAttLine.cxx:270
 TAttLine.cxx:271
 TAttLine.cxx:272
 TAttLine.cxx:273
 TAttLine.cxx:274
 TAttLine.cxx:275
 TAttLine.cxx:276
 TAttLine.cxx:277
 TAttLine.cxx:278
 TAttLine.cxx:279
 TAttLine.cxx:280
 TAttLine.cxx:281
 TAttLine.cxx:282
 TAttLine.cxx:283
 TAttLine.cxx:284
 TAttLine.cxx:285
 TAttLine.cxx:286
 TAttLine.cxx:287
 TAttLine.cxx:288
 TAttLine.cxx:289
 TAttLine.cxx:290
 TAttLine.cxx:291
 TAttLine.cxx:292
 TAttLine.cxx:293
 TAttLine.cxx:294
 TAttLine.cxx:295
 TAttLine.cxx:296
 TAttLine.cxx:297
 TAttLine.cxx:298
 TAttLine.cxx:299
 TAttLine.cxx:300
 TAttLine.cxx:301
 TAttLine.cxx:302
 TAttLine.cxx:303
 TAttLine.cxx:304