{ "cells": [ { "cell_type": "markdown", "id": "03d62f8c", "metadata": {}, "source": [ "# triangles\n", " \\preview Create small triangles at random positions on the canvas.\n", "Assign a unique ID to each triangle, and give each one a random color from the color palette.\n", "\n", "```cpp\n", "root > .x triangles.C\n", "```\n", "\n", "When a triangle is clicked, a message displaying its unique number and color will be printed.\n", "\n", "\n", "\n", "\n", "**Author:** Rene Brun \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:37 PM." ] }, { "cell_type": "markdown", "id": "496fafd5", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 1, "id": "9e8ea5f0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:36.661042Z", "iopub.status.busy": "2026-05-19T20:37:36.660922Z", "iopub.status.idle": "2026-05-19T20:37:36.667624Z", "shell.execute_reply": "2026-05-19T20:37:36.666879Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void TriangleClicked()\n", "{\n", " // this action function is called whenever you move the mouse\n", " // it just prints the id of the picked triangle\n", " // you can add graphics actions instead\n", " int event = gPad->GetEvent();\n", " if (event != 11)\n", " return; // may be comment this line\n", " TObject *select = gPad->GetSelected();\n", " if (!select)\n", " return;\n", " if (select->InheritsFrom(TPolyLine::Class())) {\n", " TPolyLine *pl = (TPolyLine *)select;\n", " printf(\"You have clicked triangle %d, color=%d\\n\", pl->GetUniqueID(), pl->GetFillColor());\n", " }\n", "}" ] }, { "cell_type": "markdown", "id": "9aa49ca8", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 2, "id": "744c3855", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:36.668994Z", "iopub.status.busy": "2026-05-19T20:37:36.668874Z", "iopub.status.idle": "2026-05-19T20:37:37.005030Z", "shell.execute_reply": "2026-05-19T20:37:37.003919Z" } }, "outputs": [], "source": [ "int ntriangles = 50;" ] }, { "cell_type": "code", "execution_count": 3, "id": "86f13690", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:37.006664Z", "iopub.status.busy": "2026-05-19T20:37:37.006532Z", "iopub.status.idle": "2026-05-19T20:37:37.221690Z", "shell.execute_reply": "2026-05-19T20:37:37.220519Z" } }, "outputs": [], "source": [ "auto c1 = new TCanvas(\"c1\", \"triangles\", 10, 10, 700, 700);\n", "gStyle->SetPalette(kCMYK);\n", "TRandom r;\n", "double dx = 0.2;\n", "double dy = 0.2;\n", "int ncolors = TColor::GetNumberOfColors();\n", "double x[4], y[4];\n", "for (int i = 0; i < ntriangles; i++) {\n", " x[0] = r.Uniform(.05, .95);\n", " y[0] = r.Uniform(.05, .95);\n", " x[1] = x[0] + dx * r.Rndm();\n", " y[1] = y[0] + dy * r.Rndm();\n", " x[2] = x[1] - dx * r.Rndm();\n", " y[2] = y[1] - dy * r.Rndm();\n", " x[3] = x[0];\n", " y[3] = y[0];\n", " auto pl = new TPolyLine(4, x, y);\n", " pl->SetUniqueID(i);\n", " int ci = ncolors * r.Rndm();\n", " TColor *c = gROOT->GetColor(TColor::GetColorPalette(ci));\n", " c->SetAlpha(r.Rndm());\n", " pl->SetFillColor(c->GetNumber());\n", " pl->Draw(\"f\");\n", "}\n", "c1->AddExec(\"ex\", \"TriangleClicked()\");" ] }, { "cell_type": "markdown", "id": "7b7ad1ab", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 4, "id": "9fe28689", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:37.223306Z", "iopub.status.busy": "2026-05-19T20:37:37.223182Z", "iopub.status.idle": "2026-05-19T20:37:37.435179Z", "shell.execute_reply": "2026-05-19T20:37:37.434193Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "gROOT->GetListOfCanvases()->Draw()" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }