ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
flower.C
Go to the documentation of this file.
1 //Author: Timur Pocheptsov, 4/02/2014
2 
3 //A demo to show transparency with TMultiGraph
4 //(and a really interesting curve/equation). + point compression in
5 //TPadPainter :))
6 //You can see all three flowers ONLY with Cococa (transparency).
7 
8 //The equation by Paul Burke: http://paulbourke.net/geometry/
9 
10 //Point compression (before you resize):
11 //polygon 1 from 500000 to 61071 points;
12 //polygon 2 from 100000 to 44600 points.
13 //OpenGL died :)))
14 
15 #include <cassert>
16 #include <vector>
17 
18 #include "TMultiGraph.h"
19 #include "TVirtualX.h"
20 #include "TCanvas.h"
21 #include "TGraph.h"
22 #include "TError.h"
23 #include "TColor.h"
24 #include "TMath.h"
25 
26 #include "customcolor.h"
27 
28 namespace {
29 
30 typedef std::vector<Double_t> vector_type;
31 typedef vector_type::size_type size_type;
32 
33 //______________________________________________________________________
34 void create_flower(vector_type &xs, vector_type &ys, size_type nPoints, Double_t r)
35 {
36  assert(nPoints > 100 && "create_flower, number of points is too small");
37 
38  xs.resize(nPoints + 1);
39  ys.resize(nPoints + 1);
40 
41  const Double_t angle = 21. * TMath::Pi() / nPoints;
42 
43  for (size_type i = 0; i <= nPoints; ++i) {
44  const Double_t u = i * angle;
45  const Double_t p4 = TMath::Sin(17 * u / 3);
46  const Double_t p8 = TMath::Sin(2 * TMath::Cos(3 * u) - 28 * u);
47  const Double_t rr = r * (1 + TMath::Sin(11 * u / 5)) - 4 * p4 * p4 * p4 * p4 * p8 * p8 * p8 * p8 * p8 * p8 * p8 * p8;
48 
49  xs[i] = rr * TMath::Cos(u);
50  ys[i] = rr * TMath::Sin(u);
51  }
52 }
53 
54 }//unnamed namespace.
55 
56 void flower()
57 {
58  //0. Indices for custom colors.
59  Color_t indices[3] = {};
61  ::Error("flower", "failed to create custom colors");
62  return;
63  }
64 
65  //1. I have to create a canvas to initialize gVirtualX.
66  TCanvas * const cnv = new TCanvas("Chrysanthemum", "Chrysanthemum", 900, 900);
67  if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
68  ::Error("flower", "This macro requires OS X version of ROOT with cocoa enabled");
69  delete cnv;
70  return;
71  }
72 
73  cnv->cd();//Just to suppress a warning if compiled.
74 
75  vector_type xs, ys;
76 
77  //2. Create graphs and custom colors for each graph.
78  create_flower(xs, ys, 300, 6);
79  TGraph * const gr1 = new TGraph(Int_t(xs.size()), &xs[0], &ys[0]);
80  new TColor(indices[0], 0., 0., 0.5, "custom_blue", 0.7);
81  gr1->SetFillColor(indices[0]);
82  gr1->SetName("part1");
83  gr1->SetTitle("part1");
84 
85  create_flower(xs, ys, 500000, 8);
86  TGraph * const gr2 = new TGraph(Int_t(xs.size()), &xs[0], &ys[0]);
87  new TColor(indices[1], 0.5, 0., 0.5, "custom_purple", 0.5);
88  gr2->SetFillColor(indices[1]);
89  gr2->SetName("part2");
90  gr2->SetTitle("part2");
91 
92  create_flower(xs, ys, 100000, 10);
93  TGraph * const gr3 = new TGraph(Int_t(xs.size()), &xs[0], &ys[0]);
94 
95  //If you want to see the difference, change 0.2 to 1 in the next call:
96  new TColor(indices[2], 1., 0., 0.4, "custom_magenta", 0.2);
97  gr3->SetFillColor(indices[2]);
98  gr3->SetName("part3");
99  gr3->SetTitle("part3");
100 
101  //3. Create a final multigraph.
102 
103  //Otcveli, uzh davno ... nu ti ponEl.
104 
105  TMultiGraph * const flower = new TMultiGraph("Chrysanthemum", "Chrysanthemum");
106  flower->Add(gr1);
107  flower->Add(gr2);
108  flower->Add(gr3);
109 
110  flower->Draw("AFP");
111 }
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition: customcolor.h:44
#define assert(cond)
Definition: unittest.h:542
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
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:2153
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
void flower()
Definition: flower.C:56
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
#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:48
double Double_t
Definition: RtypesCore.h:55
The color creation and management class.
Definition: TColor.h:47
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.