Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
triangles.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphics
3/// \notebook
4/// Create small triangles at random positions on the canvas.
5/// Assign a unique ID to each triangle, and give each one a random color from the color palette.
6///
7/// ~~~{.cpp}
8/// root > .x triangles.C
9/// ~~~
10///
11/// When a triangle is clicked, a message displaying its unique number and color will be printed.
12///
13/// \macro_image
14/// \macro_code
15///
16/// \author Rene Brun
17
18void triangles(int ntriangles = 50)
19{
20 auto c1 = new TCanvas("c1", "triangles", 10, 10, 700, 700);
22 TRandom r;
23 double dx = 0.2;
24 double dy = 0.2;
25 int ncolors = TColor::GetNumberOfColors();
26 double x[4], y[4];
27 for (int i = 0; i < ntriangles; i++) {
28 x[0] = r.Uniform(.05, .95);
29 y[0] = r.Uniform(.05, .95);
30 x[1] = x[0] + dx * r.Rndm();
31 y[1] = y[0] + dy * r.Rndm();
32 x[2] = x[1] - dx * r.Rndm();
33 y[2] = y[1] - dy * r.Rndm();
34 x[3] = x[0];
35 y[3] = y[0];
36 auto pl = new TPolyLine(4, x, y);
37 pl->SetUniqueID(i);
38 int ci = ncolors * r.Rndm();
40 c->SetAlpha(r.Rndm());
41 pl->SetFillColor(c->GetNumber());
42 pl->Draw("f");
43 }
44 c1->AddExec("ex", "TriangleClicked()");
45}
46
47void TriangleClicked()
48{
49 // this action function is called whenever you move the mouse
50 // it just prints the id of the picked triangle
51 // you can add graphics actions instead
52 int event = gPad->GetEvent();
53 if (event != 11)
54 return; // may be comment this line
55 TObject *select = gPad->GetSelected();
56 if (!select)
57 return;
58 if (select->InheritsFrom(TPolyLine::Class())) {
59 TPolyLine *pl = (TPolyLine *)select;
60 printf("You have clicked triangle %d, color=%d\n", pl->GetUniqueID(), pl->GetFillColor());
61 }
62}
#define c(i)
Definition RSha256.hxx:101
@ kCMYK
Definition TColor.h:123
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
#define gROOT
Definition TROOT.h:406
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
#define gPad
virtual Color_t GetFillColor() const
Return the fill area color.
Definition TAttFill.h:30
The Canvas class.
Definition TCanvas.h:23
The color creation and management class.
Definition TColor.h:21
static Int_t GetColorPalette(Int_t i)
Static function returning the color number i in current palette.
Definition TColor.cxx:1505
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition TColor.cxx:1921
static Int_t GetNumberOfColors()
Static function returning number of colors in the color palette.
Definition TColor.cxx:1525
Mother of all ROOT objects.
Definition TObject.h:41
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition TObject.cxx:474
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:542
Defined by an array on N points in a 2-D space.
Definition TPolyLine.h:23
static TClass * Class()
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
void SetPalette(Int_t ncolors=kBird, Int_t *colors=nullptr, Float_t alpha=1.)
See TColor::SetPalette.
Definition TStyle.cxx:1888
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17