From $ROOTSYS/tutorials/graphics/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.
  //Author: Rene Brun

  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];
  TColor *c;
  Int_t ci;
  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);
     ci = ncolors*r.Rndm();
     c  = gROOT->GetColor(ci);
     c->SetAlpha(r.Rndm());
     pl->SetFillColor(ci);
     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::Class())) {
      TPolyLine *pl = (TPolyLine*)select;
      printf("You have clicked triangle %d, color=%d\n",
              pl->GetUniqueID(),pl->GetFillColor());
   }
}
 triangles.C:1
 triangles.C:2
 triangles.C:3
 triangles.C:4
 triangles.C:5
 triangles.C:6
 triangles.C:7
 triangles.C:8
 triangles.C:9
 triangles.C:10
 triangles.C:11
 triangles.C:12
 triangles.C:13
 triangles.C:14
 triangles.C:15
 triangles.C:16
 triangles.C:17
 triangles.C:18
 triangles.C:19
 triangles.C:20
 triangles.C:21
 triangles.C:22
 triangles.C:23
 triangles.C:24
 triangles.C:25
 triangles.C:26
 triangles.C:27
 triangles.C:28
 triangles.C:29
 triangles.C:30
 triangles.C:31
 triangles.C:32
 triangles.C:33
 triangles.C:34
 triangles.C:35
 triangles.C:36
 triangles.C:37
 triangles.C:38
 triangles.C:39
 triangles.C:40
 triangles.C:41
 triangles.C:42
 triangles.C:43
 triangles.C:44
 triangles.C:45
 triangles.C:46