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;
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];
pl->SetUniqueID(i);
int ci = ncolors*
r.Rndm();
pl->SetFillColor(
c->GetNumber());
pl->Draw("f");
}
c1->AddExec(
"ex",
"TriangleClicked()");
}
void TriangleClicked()
{
int event =
gPad->GetEvent();
if (event != 11) return;
if (!select) return;
printf("You have clicked triangle %d, color=%d\n",
}
}
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
R__EXTERN TStyle * gStyle
virtual Color_t GetFillColor() const
Return the fill area color.
The color creation and management class.
static Int_t GetColorPalette(Int_t i)
Static function returning the color number i in current palette.
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
static Int_t GetNumberOfColors()
Static function returning number of colors in the color palette.
Mother of all ROOT objects.
virtual UInt_t GetUniqueID() const
Return the unique object id.
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Defined by an array on N points in a 2-D space.
This is the base class for the ROOT Random number generators.
void SetPalette(Int_t ncolors=kBird, Int_t *colors=nullptr, Float_t alpha=1.)
See TColor::SetPalette.
- Author
- Rene Brun
Definition in file triangles.C.