{ "cells": [ { "cell_type": "markdown", "id": "2682bdd8", "metadata": {}, "source": [ "# mandelbrot\n", "\n", "Pressing the keys 'z' and 'u' will zoom and unzoom the picture\n", "near the mouse location, 'r' will reset to the default view.\n", "\n", "Try it (in compiled mode!) with: `root mandelbrot.C+`\n", "\n", "\n", "### Details\n", "\n", " when a mouse event occurs the myexec() function is called (by\n", " using AddExec). Depending on the pressed key, the mygenerate()\n", " function is called, with the proper arguments. Note the\n", " last_x and last_y variables that are used in myexec() to store\n", " the last pointer coordinates (px is not a pointer position in\n", " kKeyPress events).\n", "\n", "\n", "\n", "\n", "**Author:** Luigi Bardelli \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": "code", "execution_count": 1, "id": "f309f1a0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:16.467434Z", "iopub.status.busy": "2026-05-19T20:37:16.467273Z", "iopub.status.idle": "2026-05-19T20:37:16.498314Z", "shell.execute_reply": "2026-05-19T20:37:16.495411Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "TH2F *last_histo = nullptr;" ] }, { "cell_type": "markdown", "id": "e0f4b741", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "e7ae3615", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:16.520338Z", "iopub.status.busy": "2026-05-19T20:37:16.520192Z", "iopub.status.idle": "2026-05-19T20:37:16.553487Z", "shell.execute_reply": "2026-05-19T20:37:16.546380Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void mygenerate(double factor, double cen_x, double cen_y)\n", "{\n", " printf(\"Regenerating...\\n\");\n", " // resize histo:\n", " if (factor > 0) {\n", " double dx = last_histo->GetXaxis()->GetXmax() - last_histo->GetXaxis()->GetXmin();\n", " double dy = last_histo->GetYaxis()->GetXmax() - last_histo->GetYaxis()->GetXmin();\n", " last_histo->SetBins(last_histo->GetNbinsX(), cen_x - factor * dx / 2, cen_x + factor * dx / 2,\n", " last_histo->GetNbinsY(), cen_y - factor * dy / 2, cen_y + factor * dy / 2);\n", " last_histo->Reset();\n", " } else {\n", " if (last_histo)\n", " delete last_histo;\n", " // allocate first view...\n", " last_histo = new TH2F(\"h2\", \"Mandelbrot [move mouse and press z to zoom, u to unzoom, r to reset]\", 200, -2, 2,\n", " 200, -2, 2);\n", " last_histo->SetStats(false);\n", " }\n", " const int max_iter = 50;\n", " for (int bx = 1; bx <= last_histo->GetNbinsX(); bx++)\n", " for (int by = 1; by <= last_histo->GetNbinsY(); by++) {\n", " double x = last_histo->GetXaxis()->GetBinCenter(bx);\n", " double y = last_histo->GetYaxis()->GetBinCenter(by);\n", " TComplex point(x, y);\n", " TComplex z = point;\n", " int iter = 0;\n", " while (z.Rho() < 2) {\n", " z = z * z + point;\n", " last_histo->Fill(x, y);\n", " iter++;\n", " if (iter > max_iter)\n", " break;\n", " }\n", " }\n", " last_histo->SetContour(99);\n", " last_histo->Draw(\"colz\");\n", " gPad->Modified();\n", " gPad->Update();\n", " printf(\"Done.\\n\");\n", "}" ] }, { "cell_type": "markdown", "id": "dab5d7f6", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 3, "id": "b4e22163", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:16.554922Z", "iopub.status.busy": "2026-05-19T20:37:16.554801Z", "iopub.status.idle": "2026-05-19T20:37:16.559654Z", "shell.execute_reply": "2026-05-19T20:37:16.558988Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void myexec()\n", "{\n", " // get event information\n", " int event = gPad->GetEvent();\n", " int px = gPad->GetEventX();\n", " int py = gPad->GetEventY();\n", "\n", " // some magic to get the coordinates...\n", " double xd = gPad->AbsPixeltoX(px);\n", " double yd = gPad->AbsPixeltoY(py);\n", " float x = gPad->PadtoX(xd);\n", " float y = gPad->PadtoY(yd);\n", "\n", " static float last_x;\n", " static float last_y;\n", "\n", " if (event != kKeyPress) {\n", " last_x = x;\n", " last_y = y;\n", " return;\n", " }\n", "\n", " const double Z = 2.;\n", " switch (px) {\n", " case 'z': // ZOOM\n", " mygenerate(1. / Z, last_x, last_y);\n", " break;\n", " case 'u': // UNZOOM\n", " mygenerate(Z, last_x, last_y);\n", " break;\n", " case 'r': // RESET\n", " mygenerate(-1, last_x, last_y);\n", " break;\n", " };\n", "}" ] }, { "cell_type": "markdown", "id": "aa1f3876", "metadata": {}, "source": [ "cosmetics..." ] }, { "cell_type": "code", "execution_count": 4, "id": "62c24830", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:16.560868Z", "iopub.status.busy": "2026-05-19T20:37:16.560754Z", "iopub.status.idle": "2026-05-19T20:37:17.049880Z", "shell.execute_reply": "2026-05-19T20:37:17.048904Z" } }, "outputs": [], "source": [ "gStyle->SetPadGridX(kTRUE);\n", "gStyle->SetPadGridY(kTRUE);\n", "new TCanvas(\"canvas\", \"View Mandelbrot set\");" ] }, { "cell_type": "markdown", "id": "f1a2dbad", "metadata": {}, "source": [ "this generates and draws the first view..." ] }, { "cell_type": "code", "execution_count": 5, "id": "78b9b121", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:17.051277Z", "iopub.status.busy": "2026-05-19T20:37:17.051157Z", "iopub.status.idle": "2026-05-19T20:37:17.395416Z", "shell.execute_reply": "2026-05-19T20:37:17.394959Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Regenerating...\n", "Done.\n" ] } ], "source": [ "mygenerate(-1, 0, 0);" ] }, { "cell_type": "markdown", "id": "8ef6b80a", "metadata": {}, "source": [ "add exec" ] }, { "cell_type": "code", "execution_count": 6, "id": "1a438172", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:17.396916Z", "iopub.status.busy": "2026-05-19T20:37:17.396798Z", "iopub.status.idle": "2026-05-19T20:37:17.603320Z", "shell.execute_reply": "2026-05-19T20:37:17.602322Z" } }, "outputs": [], "source": [ "gPad->AddExec(\"myexec\", \"myexec()\");" ] }, { "cell_type": "markdown", "id": "d507b60c", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 7, "id": "dfb41a67", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:17.604756Z", "iopub.status.busy": "2026-05-19T20:37:17.604625Z", "iopub.status.idle": "2026-05-19T20:37:17.830970Z", "shell.execute_reply": "2026-05-19T20:37:17.822717Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%jsroot on\n", "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 }