Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
triangles.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Create small triangles at random positions on the canvas.

Assign a unique ID to each triangle, and give each one a random color from the color palette.

When a triangle is clicked, a message displaying its unique number and color will be printed.

void triangles(int ntriangles = 50)
{
auto c1 = new TCanvas("c1", "triangles", 10, 10, 700, 700);
double dx = 0.2;
double dy = 0.2;
int ncolors = TColor::GetNumberOfColors();
double x[4], y[4];
for (int 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];
auto pl = new TPolyLine(4, x, y);
pl->SetUniqueID(i);
int ci = ncolors * r.Rndm();
c->SetAlpha(r.Rndm());
pl->SetFillColor(c->GetNumber());
pl->Draw("f");
}
c1->AddExec("ex", "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())) {
printf("You have clicked triangle %d, color=%d\n", pl->GetUniqueID(), pl->GetFillColor());
}
}
#define c(i)
Definition RSha256.hxx:101
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
@ kCMYK
Definition TColor.h:124
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:442
#define gPad
The Canvas class.
Definition TCanvas.h:23
The color creation and management class.
Definition TColor.h:22
static Int_t GetColorPalette(Int_t i)
Static function returning the color number i in current palette.
Definition TColor.cxx:1508
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:1924
static Int_t GetNumberOfColors()
Static function returning number of colors in the color palette.
Definition TColor.cxx:1528
Mother of all ROOT objects.
Definition TObject.h:41
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
Author
Rene Brun

Definition in file triangles.C.