Logo ROOT   6.08/07
Reference Guide
triangles.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_graphics
3 /// \notebook
4 /// Generate small triangles randomly in the canvas.
5 /// Each triangle has a unique id and a random color in the color palette
6 ///
7 /// ~~~{.cpp}
8 /// root > .x triangles.C
9 /// ~~~
10 ///
11 /// Then click on any triangle. A message showing the triangle number
12 /// and its color will be printed.
13 ///
14 /// \macro_image
15 /// \macro_code
16 ///
17 /// \author Rene Brun
18 
19 void triangles(Int_t ntriangles=50) {
20  TCanvas *c1 = new TCanvas("c1","triangles",10,10,700,700);
21  TRandom r;
22  Double_t dx = 0.2; Double_t dy = 0.2;
23  Int_t ncolors = gStyle->GetNumberOfColors();
24  Double_t x[4],y[4];
25  TColor *c;
26  Int_t ci;
27  for (Int_t i=0;i<ntriangles;i++) {
28  x[0] = r.Uniform(.05,.95); y[0] = r.Uniform(.05,.95);
29  x[1] = x[0] + dx*r.Rndm(); y[1] = y[0] + dy*r.Rndm();
30  x[2] = x[1] - dx*r.Rndm(); y[2] = y[1] - dy*r.Rndm();
31  x[3] = x[0]; y[3] = y[0];
32  TPolyLine *pl = new TPolyLine(4,x,y);
33  pl->SetUniqueID(i);
34  ci = ncolors*r.Rndm();
35  c = gROOT->GetColor(TColor::GetColorPalette(ci));
36  c->SetAlpha(r.Rndm());
37  pl->SetFillColor(ci);
38  pl->Draw("f");
39  }
40  c1->AddExec("ex","TriangleClicked()");
41 }
42 
43 void TriangleClicked() {
44  //this action function is called whenever you move the mouse
45  //it just prints the id of the picked triangle
46  //you can add graphics actions instead
47  int event = gPad->GetEvent();
48  if (event != 11) return; //may be comment this line
49  TObject *select = gPad->GetSelected();
50  if (!select) return;
51  if (select->InheritsFrom(TPolyLine::Class())) {
52  TPolyLine *pl = (TPolyLine*)select;
53  printf("You have clicked triangle %d, color=%d\n",
54  pl->GetUniqueID(),pl->GetFillColor());
55  }
56 }
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition: TObject.cxx:434
virtual void SetAlpha(Float_t a)
Definition: TColor.h:70
return c
return c1
Definition: legend1.C:41
R__EXTERN TStyle * gStyle
Definition: TStyle.h:418
#define gROOT
Definition: TROOT.h:364
int Int_t
Definition: RtypesCore.h:41
const char * Class
Definition: TXMLSetup.cxx:64
static Int_t GetColorPalette(Int_t i)
Static function returning the color number i in current palette.
Definition: TColor.cxx:1355
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:31
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition: TObject.cxx:750
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:512
virtual void AddExec(const char *name, const char *command)
Add a new TExec object to the list of Execs.
Definition: TPad.cxx:378
virtual void Draw(Option_t *option="")
Draw this polyline with its current attributes.
Definition: TPolyLine.cxx:232
TRandom2 r(17)
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:42
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:488
Int_t GetNumberOfColors() const
Return number of colors in the color palette.
Definition: TStyle.cxx:801
The Canvas class.
Definition: TCanvas.h:41
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
virtual Color_t GetFillColor() const
Return the fill area color.
Definition: TAttFill.h:35
The color creation and management class.
Definition: TColor.h:23
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
Mother of all ROOT objects.
Definition: TObject.h:37
Defined by an array on N points in a 2-D space.
Definition: TPolyLine.h:31
#define gPad
Definition: TVirtualPad.h:289