1 | #include <iostream> |
---|
2 | #include <math.h> |
---|
3 | #include <cpgplot.h> |
---|
4 | #include <duchamp.hh> |
---|
5 | #include <Utils/utils.hh> |
---|
6 | |
---|
7 | void plotLine(const float slope, const float intercept) |
---|
8 | { |
---|
9 | float x1, x2, y1, y2; |
---|
10 | cpgqwin(&x1,&x2,&y1,&y2); |
---|
11 | cpgmove(x1,slope*x1+intercept); |
---|
12 | cpgdraw(x2,slope*x2+intercept); |
---|
13 | } |
---|
14 | |
---|
15 | void lineOfEquality() |
---|
16 | { |
---|
17 | plotLine(1.,0.); |
---|
18 | } |
---|
19 | |
---|
20 | void lineOfBestFit(int size, float *x, float *y) |
---|
21 | { |
---|
22 | float a,b,r,erra,errb; |
---|
23 | linear_regression(size,x,y,0,size-1,a,erra,b,errb,r); |
---|
24 | plotLine(a,b); |
---|
25 | } |
---|
26 | |
---|
27 | void plotVertLine(const float xval, const int colour, const int style) |
---|
28 | { |
---|
29 | float x1, x2, y1, y2; |
---|
30 | cpgqwin(&x1,&x2,&y1,&y2); |
---|
31 | int currentColour,currentStyle; |
---|
32 | cpgqci(¤tColour); |
---|
33 | cpgqls(¤tStyle); |
---|
34 | if(currentColour!=colour) cpgsci(colour); |
---|
35 | if(currentStyle !=style) cpgsls(style); |
---|
36 | cpgmove(xval,y1); |
---|
37 | cpgdraw(xval,y2); |
---|
38 | if(currentColour!=colour) cpgsci(currentColour); |
---|
39 | if(currentStyle !=style) cpgsls(currentStyle); |
---|
40 | } |
---|
41 | |
---|
42 | void plotVertLine(const float xval) |
---|
43 | { |
---|
44 | int colour,style; |
---|
45 | cpgqci(&colour); |
---|
46 | cpgqls(&style); |
---|
47 | plotVertLine(xval,colour,style); |
---|
48 | } |
---|
49 | |
---|
50 | void plotVertLine(const float xval, const int colour) |
---|
51 | { |
---|
52 | int style; |
---|
53 | cpgqls(&style); |
---|
54 | plotVertLine(xval,colour,style); |
---|
55 | } |
---|
56 | |
---|
57 | void plotHorizLine(const float yval, const int colour, const int style) |
---|
58 | { |
---|
59 | float x1, x2, y1, y2; |
---|
60 | cpgqwin(&x1,&x2,&y1,&y2); |
---|
61 | int currentColour,currentStyle; |
---|
62 | cpgqci(¤tColour); |
---|
63 | cpgqls(¤tStyle); |
---|
64 | if(currentColour!=colour) cpgsci(colour); |
---|
65 | if(currentStyle !=style) cpgsls(style); |
---|
66 | cpgmove(x1,yval); |
---|
67 | cpgdraw(x2,yval); |
---|
68 | if(currentColour!=colour) cpgsci(currentColour); |
---|
69 | if(currentStyle !=style) cpgsls(currentStyle); |
---|
70 | } |
---|
71 | |
---|
72 | void plotHorizLine(const float yval) |
---|
73 | { |
---|
74 | int colour,style; |
---|
75 | cpgqci(&colour); |
---|
76 | cpgqls(&style); |
---|
77 | plotHorizLine(yval,colour,style); |
---|
78 | } |
---|
79 | |
---|
80 | void plotHorizLine(const float yval, const int colour) |
---|
81 | { |
---|
82 | int style; |
---|
83 | cpgqls(&style); |
---|
84 | plotHorizLine(yval,colour,style); |
---|
85 | } |
---|
86 | |
---|
87 | void lineOfBestFitPB(const int size, const float *x, const float *y) |
---|
88 | { |
---|
89 | int numSim = int(size * log(float(size)*log(float(size))) ); |
---|
90 | float slope=0, intercept=0; |
---|
91 | float *xboot = new float[size]; |
---|
92 | float *yboot = new float[size]; |
---|
93 | for(int sim=0;sim<numSim;sim++){ |
---|
94 | for(int i=0;i<size;i++){ |
---|
95 | int point = int((1.*rand())/(RAND_MAX+1.0) * size); |
---|
96 | xboot[i] = x[point]; |
---|
97 | yboot[i] = y[point]; |
---|
98 | } |
---|
99 | float a,b,r,erra,errb; |
---|
100 | linear_regression(size,xboot,yboot,0,size-1,a,erra,b,errb,r); |
---|
101 | slope += a; |
---|
102 | intercept += b; |
---|
103 | } |
---|
104 | delete [] xboot,yboot; |
---|
105 | slope /= float(numSim); |
---|
106 | intercept /= float(numSim); |
---|
107 | |
---|
108 | plotLine(slope,intercept); |
---|
109 | } |
---|
110 | |
---|
111 | void drawContours(const int size, const float *x, const float *y) |
---|
112 | { |
---|
113 | float x1, x2, y1, y2; |
---|
114 | cpgqwin(&x1,&x2,&y1,&y2); |
---|
115 | const int nbin = 30; |
---|
116 | // widths of bins in x and y directions |
---|
117 | float xbin = (x2 - x1) / float(nbin-2); // have one bin either side of extrema |
---|
118 | float ybin = (y2 - y1) / float(nbin-2); |
---|
119 | float *binnedarray = new float[nbin*nbin]; |
---|
120 | for(int i=0;i<nbin*nbin;i++) binnedarray[i] = 0.; |
---|
121 | for(int i=0;i<size;i++){ |
---|
122 | int xpos = int( (x[i] - (x1-xbin)) / xbin ); |
---|
123 | int ypos = int( (y[i] - (y1-ybin)) / ybin ); |
---|
124 | if((ypos*nbin+xpos) >= (nbin*nbin)) |
---|
125 | duchampError("drawContours","pixel position out of range"); |
---|
126 | binnedarray[ypos*nbin+xpos] += 1.; |
---|
127 | } |
---|
128 | float maxct = binnedarray[0]; |
---|
129 | for(int i=1;i<nbin*nbin;i++) if(binnedarray[i]>maxct) maxct=binnedarray[i]; |
---|
130 | float tr[6]={x1-3*xbin/2,xbin,0.,y1-3*ybin/2,0.,ybin}; |
---|
131 | const int ncont = 10; |
---|
132 | float *cont = new float[ncont]; |
---|
133 | for(int i=0;i<ncont;i++) cont[i] = maxct / pow(2,float(i)/2.); |
---|
134 | cpgcont(binnedarray,nbin,nbin,1,nbin,1,nbin,cont,ncont,tr); |
---|
135 | delete [] binnedarray; |
---|
136 | delete [] cont; |
---|
137 | } |
---|