Logo ROOT  
Reference Guide
triangles.C File Reference

Detailed Description

View in nbviewer Open in SWAN 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.

void triangles(Int_t ntriangles=50) {
TCanvas *c1 = new TCanvas("c1","triangles",10,10,700,700);
Double_t dx = 0.2; Double_t dy = 0.2;
Double_t x[4],y[4];
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(TColor::GetColorPalette(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());
}
}
Author
Rene Brun

Definition in file triangles.C.

c
#define c(i)
Definition: RSha256.hxx:119
TPolyLine
Definition: TPolyLine.h:23
r
ROOT::R::TRInterface & r
Definition: Object.C:4
TStyle::GetNumberOfColors
Int_t GetNumberOfColors() const
Return number of colors in the color palette.
Definition: TStyle.cxx:1124
Int_t
int Int_t
Definition: RtypesCore.h:45
TObject::GetUniqueID
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition: TObject.cxx:377
x
Double_t x[n]
Definition: legend1.C:17
TColor::GetColorPalette
static Int_t GetColorPalette(Int_t i)
Static function returning the color number i in current palette.
Definition: TColor.cxx:1390
TObject::InheritsFrom
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:445
TPolyLine::Draw
virtual void Draw(Option_t *option="")
Draw this polyline with its current attributes.
Definition: TPolyLine.cxx:228
TRandom
Definition: TRandom.h:27
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TColor
Definition: TColor.h:19
y
Double_t y[n]
Definition: legend1.C:17
Double_t
double Double_t
Definition: RtypesCore.h:59
TCanvas
Definition: TCanvas.h:23
TObject
Definition: TObject.h:37
gPad
#define gPad
Definition: TVirtualPad.h:287
Class
void Class()
Definition: Class.C:29
TObject::SetUniqueID
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition: TObject.cxx:707
gROOT
#define gROOT
Definition: TROOT.h:406
c1
return c1
Definition: legend1.C:41