00001 class HexLatice {
00002 public:
00003
00004 TVector2 U,V;
00005 TVector2 bU,bV;
00006 double uv,w0,pitch;
00007
00008 HexLatice(double pitchX,double phi1) {
00009 const double pi=2*acos(0);
00010 pitch=pitchX;
00011 bU.SetMagPhi(pitch,phi1);
00012 bV=bU.Rotate(pi/3);
00013 U=bU.Unit();
00014 V=bV.Unit();
00015 uv=U*V, w0=(1-uv*uv)*pitch;
00016 printf("Hex Grid base: U(%.3f,%3f), V(%.3f,%3f) U*V=%.3f\n",U.X(),V.Y(),U.X(),V.Y(),uv);
00017 }
00018 TVector2 snap(TVector2 &P, int &ku,int &kv){
00019
00020 double a=P*U, b=P*V;
00021 double u=(a-b*uv)/w0, v=(b-a*uv)/w0;
00022 double iu=floor(u), iv=floor(v);
00023
00024 int iN;
00025 double maxD2=999999;
00026
00027 TVector2 Psnap=TVector2(99999,99999);
00028 for(iN=0;iN<4;iN++) {
00029 int ju=iu+iN%2;
00030 int jv=iv+iN/2;
00031 TVector2 Q=ju*bU + jv*bV;
00032 double dist2=(Q-P).Mod2();
00033
00034 if(maxD2<dist2) continue;
00035 maxD2=dist2;
00036 Psnap=Q;
00037
00038 ku=ju;
00039 kv=jv;
00040 }
00041
00042
00043
00044 return Psnap;
00045 }
00046 };
00047
00048
00049
00050 hexSnap(int np=1) {
00051 const double pi=2*acos(0);
00052 double phi1=pi/4;
00053 double pitch=10;
00054 double Rmax=40;
00055 gStyle->SetOptStat(0);
00056 HexLatice hexLat(pitch,phi1);
00057 TRandom3 *rnd=new TRandom3;
00058 c=new TCanvas("bb","bb",600,600);
00059 h=new TH2F("aa","aa",10,0,Rmax,10,0,Rmax); h->Draw();
00060 drawHoles(38,hexLat);
00061
00062 int i;
00063 int j=4;
00064 for(j=0;j<10;j++)
00065 for(i=0;i<10;i++){
00066
00067 TVector2 P(3+j*.7,3+i*.7);
00068 int kU,kV;
00069 hexLat.snap(P,kU,kV);
00070 kU+=1000;
00071 kV+=1000;
00072
00073 tm=new TMarker(P.X(),P.Y(),5);
00074 int off=kU%2 +2*(kV%2);
00075
00076 tm->Draw(); tm->SetMarkerColor(1+off);
00077 }
00078
00079
00080 return;
00081 }
00082
00083
00084 drawHoles(double r2,HexLatice &HL) {
00085 int i=1,j=1;
00086
00087 for(j=-5; j<10;j++)
00088 for(i=1; i<10;i++) {
00089 TVector2 r=i*HL.bU + j*HL.bV;
00090
00091 if(r.X()<0 || r.Y()<0) continue;
00092 if(r.X()>r2 || r.Y()>r2) continue;
00093 el=new TEllipse(r.X(),r.Y(),0.5);
00094 el->Draw();
00095 int kU=i+1000;
00096 int kV=j+1000;
00097 int off=kU%2 +2*(kV%2);
00098 el->SetFillColor(1+off);
00099 }
00100 }
00101