Revision 1ff8cfc023263a593b0e186877352756557e2553 authored by Rene Brun on 19 November 2004, 06:46:19 UTC, committed by Rene Brun on 19 November 2004, 06:46:19 UTC
# Default 3d Viewer
# by default 3-D views are shown in the pad.
# if the next line is activated, the default viewer will be OpenGL
#Viewer3D.DefaultDrawOption:   ogl


git-svn-id: http://root.cern.ch/svn/root/trunk@10564 27541ba8-7e3a-0410-8455-c3a389f83636
1 parent 8524938
Raw File
triangles.C
void triangles(Int_t ntriangles=50) {
  //generate small triangles randomly in the canvas.
  //Each triangle has a unique id and a random color in the color palette
  //root > .x triangles.C
  //then click on any triangle. A message showing the triangle number
  //and its color will be printed.

  TCanvas *c1 = new TCanvas("c1","triangles",10,10,700,700);
  TRandom r;
  Double_t dx = 0.2; Double_t dy = 0.2;
  Int_t ncolors = gStyle->GetNumberOfColors();
  Double_t x[4],y[4];
  for (Int_t i=0;i<ntriangles;i++) {
     x[0] = r.Uniform(.05,.95); y[0] = r.Uniform(.05,.95);
     x[1] = x[0] + dx*r.Rndm(); y[1] = y[0] + dy*r.Rndm();
     x[2] = x[1] - dx*r.Rndm(); y[2] = y[1] - dy*r.Rndm();
     x[3] = x[0];               y[3] = y[0];
     TPolyLine *pl = new TPolyLine(4,x,y);
     pl->SetUniqueID(i);
     pl->SetFillColor(ncolors*r.Rndm());
     pl->Draw("f");
  }
  c1->AddExec("ex","TriangleClicked()");
}

void TriangleClicked() {
   //this action function is called whenever you move the mouse
   //it just prints the id of the picked triangle
   //you can add graphics actions instead
   int event = gPad->GetEvent();
   if (event != 11) return; //may be comment this line 
   TObject *select = gPad->GetSelected();
   if (!select) return;
   if (select->InheritsFrom("TPolyLine")) {
      TPolyLine *pl = (TPolyLine*)select;
      printf("You have clicked triangle %d, color=%d\n",
              pl->GetUniqueID(),pl->GetFillColor());
   }
}
back to top