// @(#)root/graf:$Id$
// Author: Rene Brun   108/08/2002

/*************************************************************************
 * 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 "TROOT.h"
#include "TMath.h"
#include "TCrown.h"
#include "TVirtualPad.h"

ClassImp(TCrown)


//______________________________________________________________________________
/* Begin_Html
<center><h2>TCrown : to draw crown</h2></center>
A crown is specified with the position of its centre, its inner/outer radius
a minimum and maximum angle. The attributes of the outline line are given via
TAttLine. The attributes of the fill area are given via TAttFill.
<p> Example:
End_Html
Begin_Macro(source)
{
   TCanvas *c1 = new TCanvas("c1","c1",400,400);
   TCrown cr1(.5,.5,.3,.4);
   cr1.SetLineStyle(2);
   cr1.SetLineWidth(4);
   cr1.Draw();
   TCrown cr2(.5,.5,.2,.3,45,315);
   cr2.SetFillColor(38);
   cr2.SetFillStyle(3010);
   cr2.Draw();
   TCrown cr3(.5,.5,.2,.3,-45,45);
   cr3.SetFillColor(50);
   cr3.SetFillStyle(3025);
   cr3.Draw();
   TCrown cr4(.5,.5,.0,.2);
   cr4.SetFillColor(4);
   cr4.SetFillStyle(3008);
   cr4.Draw();
   return c1;
}
End_Macro */


//______________________________________________________________________________
TCrown::TCrown(): TEllipse()
{
   /* Begin_Html
   Crown default constructor.
   End_Html */

}


//______________________________________________________________________________
TCrown::TCrown(Double_t x1, Double_t y1,Double_t radin, Double_t radout,Double_t phimin,Double_t phimax)
      :TEllipse(x1,y1,radin,radout,phimin,phimax,0)
{
   /* Begin_Html
   Crown normal constructor.
   <ul>
   <li> x1,y1  : coordinates of centre of crown
   <li> radin  : inner crown radius
   <li> radout : outer crown radius
   <li> phimin : min angle in degrees (default is 0)
   <li> phimax : max angle in degrees (default is 360)
   </ul>
   When a crown sector only is drawn, the lines connecting the center
   of the crown to the edges are drawn by default. One can specify
   the drawing option "only" to not draw these lines.
   End_Html */

}


//______________________________________________________________________________
TCrown::TCrown(const TCrown &crown) : TEllipse(crown)
{
   /* Begin_Html
   Crown copy constructor.
   End_Html */

   ((TCrown&)crown).Copy(*this);
}


//______________________________________________________________________________
TCrown::~TCrown()
{
   /* Begin_Html
   Crown default destructor.
   End_Html */
}


//______________________________________________________________________________
void TCrown::Copy(TObject &crown) const
{
   /* Begin_Html
   Copy this crown to crown.
   End_Html */

   TEllipse::Copy(crown);
}


//______________________________________________________________________________
Int_t TCrown::DistancetoPrimitive(Int_t px, Int_t py)
{
   /* Begin_Html
   Compute distance from point px,py to a crown.
   <p>
   If crown is filled, return OK if we are inside
   otherwise, crown is found if near the crown edges.
   End_Html */

   const Double_t kPI = TMath::Pi();
   Double_t x = gPad->PadtoX(gPad->AbsPixeltoX(px)) - fX1;
   Double_t y = gPad->PadtoY(gPad->AbsPixeltoY(py)) - fY1;

   Double_t r1 = fR1;
   Double_t r2 = fR2;
   Double_t r  = TMath::Sqrt(x*x+y*y);

   if (r1>r2) {
      r1 = fR2;
      r2 = fR1;
   }

   Int_t dist = 9999;
   if (r > r2) return dist;
   if (r < r1) return dist;
   if (fPhimax-fPhimin < 360) {
      Double_t phi = 180*TMath::ACos(x/r)/kPI;
      if (y<0) phi = 360-phi;
      Double_t phi1 = fPhimin;
      Double_t phi2 = fPhimax;
      if (phi1<0) phi1=phi1+360;
      if (phi2<0) phi2=phi2+360;
      if (phi2<phi1) {
         if (phi < phi1 && phi > phi2) return dist;
      } else {
         if (phi < phi1) return dist;
         if (phi > phi2) return dist;
      }
   }

   if (GetFillColor() && GetFillStyle()) {
      return 0;
   } else {
      if (TMath::Abs(r2-r)/r2 < 0.02) return 0;
      if (TMath::Abs(r1-r)/r1 < 0.02) return 0;
   }
   return dist;
}


//______________________________________________________________________________
void TCrown::DrawCrown(Double_t x1, Double_t y1,Double_t radin,Double_t radout,Double_t phimin,Double_t phimax,Option_t *option)
{
   /* Begin_Html
   Draw this crown with new coordinates.
   End_Html */

   TCrown *newcrown = new TCrown(x1, y1, radin, radout, phimin, phimax);
   TAttLine::Copy(*newcrown);
   TAttFill::Copy(*newcrown);
   newcrown->SetBit(kCanDelete);
   newcrown->AppendPad(option);
}


//______________________________________________________________________________
void TCrown::ExecuteEvent(Int_t event, Int_t px, Int_t py)
{
   /* Begin_Html
   Execute action corresponding to one event
   <p>
   For the time being TEllipse::ExecuteEvent is used.
   End_Html */

   TEllipse::ExecuteEvent(event,px,py);
}


//______________________________________________________________________________
void TCrown::Paint(Option_t *)
{
   /* Begin_Html
   Paint this crown with its current attributes.
   End_Html */

   const Double_t kPI = TMath::Pi();
   const Int_t np = 40;
   static Double_t x[2*np+3], y[2*np+3];
   TAttLine::Modify();
   TAttFill::Modify();

   Double_t angle,dx,dy;
   Double_t dphi = (fPhimax-fPhimin)*kPI/(180*np);
   Double_t ct   = TMath::Cos(kPI*fTheta/180);
   Double_t st   = TMath::Sin(kPI*fTheta/180);
   Int_t i;
   //compute outer points
   for (i=0;i<=np;i++) {
      angle = fPhimin*kPI/180 + Double_t(i)*dphi;
      dx    = fR2*TMath::Cos(angle);
      dy    = fR2*TMath::Sin(angle);
      x[i]  = fX1 + dx*ct - dy*st;
      y[i]  = fY1 + dx*st + dy*ct;
   }
   //compute inner points
   for (i=0;i<=np;i++) {
      angle = fPhimin*kPI/180 + Double_t(i)*dphi;
      dx    = fR1*TMath::Cos(angle);
      dy    = fR1*TMath::Sin(angle);
      x[2*np-i+1]  = fX1 + dx*ct - dy*st;
      y[2*np-i+1]  = fY1 + dx*st + dy*ct;
   }
   x[2*np+2]  = x[0];
   y[2*np+2]  = y[0];
   if (fPhimax-fPhimin >= 360 ) {
      // a complete filled crown
      if (GetFillColor()  && GetFillStyle()) {
         gPad->PaintFillArea(2*np+2,x,y);
      }
      // a complete empty crown
      if (GetLineStyle()) {
         gPad->PaintPolyLine(np+1,x,y);
         gPad->PaintPolyLine(np+1,&x[np+1],&y[np+1]);
      }
   } else {
      //crown segment
      if (GetFillColor()  && GetFillStyle()) gPad->PaintFillArea(2*np+2,x,y);
      if (GetLineStyle()) gPad->PaintPolyLine(2*np+3,x,y);
   }
}


//______________________________________________________________________________
void TCrown::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
{
   /* Begin_Html
   Save primitive as a C++ statement(s) on output stream out.
   End_Html */

   out<<"   "<<std::endl;
   if (gROOT->ClassSaved(TCrown::Class())) {
      out<<"   ";
   } else {
      out<<"   TCrown *";
   }
   out<<"crown = new TCrown("<<fX1<<","<<fY1<<","<<fR1<<","<<fR2
      <<","<<fPhimin<<","<<fPhimax<<");"<<std::endl;

   SaveFillAttributes(out,"crown",0,1001);
   SaveLineAttributes(out,"crown",1,1,1);

   if (GetNoEdges()) out<<"   crown->SetNoEdges();"<<std::endl;

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