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