Logo ROOT   6.10/09
Reference Guide
flower.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_cocoa
3 /// A demo to show transparency with TMultiGraph
4 /// (and a really interesting curve/equation). Point compression in TPadPainter.
5 /// You can see all three flowers ONLY with Cocoa (transparency).
6 ///
7 /// The equation by Paul Burke: http://paulbourke.net/geometry/
8 ///
9 /// \macro_code
10 ///
11 /// \author Timur Pocheptsov
12 
13 #include <cassert>
14 #include <vector>
15 
16 #include "TMultiGraph.h"
17 #include "TVirtualX.h"
18 #include "TCanvas.h"
19 #include "TGraph.h"
20 #include "TError.h"
21 #include "TColor.h"
22 #include "TMath.h"
23 
24 #include "customcolor.h"
25 
26 namespace {
27 
28 typedef std::vector<Double_t> vector_type;
29 typedef vector_type::size_type size_type;
30 
31 //______________________________________________________________________
32 void create_flower(vector_type &xs, vector_type &ys, size_type nPoints, Double_t r)
33 {
34  assert(nPoints > 100 && "create_flower, number of points is too small");
35 
36  xs.resize(nPoints + 1);
37  ys.resize(nPoints + 1);
38 
39  const Double_t angle = 21. * TMath::Pi() / nPoints;
40 
41  for (size_type i = 0; i <= nPoints; ++i) {
42  const Double_t u = i * angle;
43  const Double_t p4 = TMath::Sin(17 * u / 3);
44  const Double_t p8 = TMath::Sin(2 * TMath::Cos(3 * u) - 28 * u);
45  const Double_t rr = r * (1 + TMath::Sin(11 * u / 5)) - 4 * p4 * p4 * p4 * p4 * p8 * p8 * p8 * p8 * p8 * p8 * p8 * p8;
46 
47  xs[i] = rr * TMath::Cos(u);
48  ys[i] = rr * TMath::Sin(u);
49  }
50 }
51 
52 }//unnamed namespace.
53 
54 void flower()
55 {
56  //0. Indices for custom colors.
57  Color_t indices[3] = {};
59  ::Error("flower", "failed to create custom colors");
60  return;
61  }
62 
63  //1. I have to create a canvas to initialize gVirtualX.
64  TCanvas * const cnv = new TCanvas("Chrysanthemum", "Chrysanthemum", 900, 900);
65  if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
66  ::Error("flower", "This macro requires OS X version of ROOT with cocoa enabled");
67  delete cnv;
68  return;
69  }
70 
71  cnv->cd();//Just to suppress a warning if compiled.
72 
73  vector_type xs, ys;
74 
75  //2. Create graphs and custom colors for each graph.
76  create_flower(xs, ys, 300, 6);
77  TGraph * const gr1 = new TGraph(Int_t(xs.size()), &xs[0], &ys[0]);
78  new TColor(indices[0], 0., 0., 0.5, "custom_blue", 0.7);
79  gr1->SetFillColor(indices[0]);
80  gr1->SetName("part1");
81  gr1->SetTitle("part1");
82 
83  create_flower(xs, ys, 500000, 8);
84  TGraph * const gr2 = new TGraph(Int_t(xs.size()), &xs[0], &ys[0]);
85  new TColor(indices[1], 0.5, 0., 0.5, "custom_purple", 0.5);
86  gr2->SetFillColor(indices[1]);
87  gr2->SetName("part2");
88  gr2->SetTitle("part2");
89 
90  create_flower(xs, ys, 100000, 10);
91  TGraph * const gr3 = new TGraph(Int_t(xs.size()), &xs[0], &ys[0]);
92 
93  //If you want to see the difference, change 0.2 to 1 in the next call:
94  new TColor(indices[2], 1., 0., 0.4, "custom_magenta", 0.2);
95  gr3->SetFillColor(indices[2]);
96  gr3->SetName("part3");
97  gr3->SetTitle("part3");
98 
99  //3. Create a final multigraph.
100 
101  //Otcveli, uzh davno ... nu ti ponEl.
102 
103  TMultiGraph * const flower = new TMultiGraph("Chrysanthemum", "Chrysanthemum");
104  flower->Add(gr1);
105  flower->Add(gr2);
106  flower->Add(gr3);
107 
108  flower->Draw("AFP");
109 }
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition: customcolor.h:38
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:131
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition: TMultiGraph.h:35
int Int_t
Definition: RtypesCore.h:41
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2180
virtual void Draw(Option_t *chopt="")
Draw this multigraph with its current attributes.
constexpr Double_t Pi()
Definition: TMath.h:40
void Error(const char *location, const char *msgfmt,...)
short Color_t
Definition: RtypesCore.h:79
TRandom2 r(17)
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
#define gVirtualX
Definition: TVirtualX.h:350
Double_t Cos(Double_t)
Definition: TMath.h:551
The Canvas class.
Definition: TCanvas.h:31
double Double_t
Definition: RtypesCore.h:55
The color creation and management class.
Definition: TColor.h:19
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
Double_t Sin(Double_t)
Definition: TMath.h:548
virtual void Add(TGraph *graph, Option_t *chopt="")
Add a new graph to the list of graphs.