triangles.C

Go to the documentation of this file.
00001 void triangles(Int_t ntriangles=50) {
00002   //generate small triangles randomly in the canvas.
00003   //Each triangle has a unique id and a random color in the color palette
00004   //root > .x triangles.C
00005   //then click on any triangle. A message showing the triangle number
00006   //and its color will be printed.
00007   //Author: Rene Brun
00008    
00009   TCanvas *c1 = new TCanvas("c1","triangles",10,10,700,700);
00010   TRandom r;
00011   Double_t dx = 0.2; Double_t dy = 0.2;
00012   Int_t ncolors = gStyle->GetNumberOfColors();
00013   Double_t x[4],y[4];
00014   for (Int_t i=0;i<ntriangles;i++) {
00015      x[0] = r.Uniform(.05,.95); y[0] = r.Uniform(.05,.95);
00016      x[1] = x[0] + dx*r.Rndm(); y[1] = y[0] + dy*r.Rndm();
00017      x[2] = x[1] - dx*r.Rndm(); y[2] = y[1] - dy*r.Rndm();
00018      x[3] = x[0];               y[3] = y[0];
00019      TPolyLine *pl = new TPolyLine(4,x,y);
00020      pl->SetUniqueID(i);
00021      pl->SetFillColor(ncolors*r.Rndm());
00022      pl->Draw("f");
00023   }
00024   c1->AddExec("ex","TriangleClicked()");
00025 }
00026 
00027 void TriangleClicked() {
00028    //this action function is called whenever you move the mouse
00029    //it just prints the id of the picked triangle
00030    //you can add graphics actions instead
00031    int event = gPad->GetEvent();
00032    if (event != 11) return; //may be comment this line 
00033    TObject *select = gPad->GetSelected();
00034    if (!select) return;
00035    if (select->InheritsFrom(TPolyLine::Class())) {
00036       TPolyLine *pl = (TPolyLine*)select;
00037       printf("You have clicked triangle %d, color=%d\n",
00038               pl->GetUniqueID(),pl->GetFillColor());
00039    }
00040 }

Generated on Tue Jul 5 15:44:22 2011 for ROOT_528-00b_version by  doxygen 1.5.1