xref: /petsc/src/sys/classes/draw/utils/axis.c (revision 2fa5cd679192b9b390e47ae2d0650965e6b1d9fa)
1 
2 #include <../src/sys/classes/draw/utils/axisimpl.h>
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "PetscDrawAxisSetLimits"
6 /*@
7     PetscDrawAxisSetLimits -  Sets the limits (in user coords) of the axis
8 
9     Not Collective (ignored on all processors except processor 0 of PetscDrawAxis)
10 
11     Input Parameters:
12 +   axis - the axis
13 .   xmin,xmax - limits in x
14 -   ymin,ymax - limits in y
15 
16     Options Database:
17 .   -drawaxis_hold - hold the initial set of axis limits for future plotting
18 
19     Level: advanced
20 
21 .seealso:  PetscDrawAxisSetHoldLimits()
22 
23 @*/
24 PetscErrorCode  PetscDrawAxisSetLimits(PetscDrawAxis axis,PetscReal xmin,PetscReal xmax,PetscReal ymin,PetscReal ymax)
25 {
26   PetscErrorCode ierr;
27 
28   PetscFunctionBegin;
29   if (!axis) PetscFunctionReturn(0);
30   if (axis->hold) PetscFunctionReturn(0);
31   axis->xlow = xmin;
32   axis->xhigh= xmax;
33   axis->ylow = ymin;
34   axis->yhigh= ymax;
35 
36   ierr = PetscOptionsHasName(((PetscObject)axis)->prefix,"-drawaxis_hold",&axis->hold);CHKERRQ(ierr);
37   PetscFunctionReturn(0);
38 }
39 
40 #undef __FUNCT__
41 #define __FUNCT__ "PetscDrawAxisGetLimits"
42 /*@
43     PetscDrawAxisGetLimits -  Gets the limits (in user coords) of the axis
44 
45     Not Collective (ignored on all processors except processor 0 of PetscDrawAxis)
46 
47     Input Parameters:
48 +   axis - the axis
49 .   xmin,xmax - limits in x
50 -   ymin,ymax - limits in y
51 
52     Level: advanced
53 
54 .seealso:  PetscDrawAxisSetLimits()
55 
56 @*/
57 PetscErrorCode  PetscDrawAxisGetLimits(PetscDrawAxis axis,PetscReal *xmin,PetscReal *xmax,PetscReal *ymin,PetscReal *ymax)
58 {
59   PetscFunctionBegin;
60   if (!axis) PetscFunctionReturn(0);
61   if (axis->hold) PetscFunctionReturn(0);
62   *xmin = axis->xlow;
63   *xmax = axis->xhigh;
64   *ymin = axis->ylow;
65   *ymax = axis->yhigh;
66   PetscFunctionReturn(0);
67 }
68 
69 #undef __FUNCT__
70 #define __FUNCT__ "PetscADefLabel"
71 /*
72    val is the label value.  sep is the separation to the next (or previous)
73    label; this is useful in determining how many significant figures to
74    keep.
75  */
76 PetscErrorCode PetscADefLabel(PetscReal val,PetscReal sep,char **p)
77 {
78   static char    buf[40];
79   PetscErrorCode ierr;
80 
81   PetscFunctionBegin;
82   /* Find the string */
83   if (PetscAbsReal(val)/sep <  1.e-4) {
84     buf[0] = '0'; buf[1] = 0;
85   } else {
86     sprintf(buf,"%0.1e",(double)val);
87     ierr = PetscStripZerosPlus(buf);CHKERRQ(ierr);
88     ierr = PetscStripe0(buf);CHKERRQ(ierr);
89     ierr = PetscStripInitialZero(buf);CHKERRQ(ierr);
90     ierr = PetscStripAllZeros(buf);CHKERRQ(ierr);
91     ierr = PetscStripTrailingZeros(buf);CHKERRQ(ierr);
92   }
93   *p =buf;
94   PetscFunctionReturn(0);
95 }
96 
97 #undef __FUNCT__
98 #define __FUNCT__ "PetscADefTicks"
99 /* Finds "nice" locations for the ticks */
100 PetscErrorCode PetscADefTicks(PetscReal low,PetscReal high,int num,int *ntick,PetscReal * tickloc,int maxtick)
101 {
102   PetscErrorCode ierr;
103   int            i,power;
104   PetscReal      x = 0.0,base=0.0;
105 
106   PetscFunctionBegin;
107   ierr = PetscAGetBase(low,high,num,&base,&power);CHKERRQ(ierr);
108   ierr = PetscAGetNice(low,base,-1,&x);CHKERRQ(ierr);
109 
110   /* Values are of the form j * base */
111   /* Find the starting value */
112   if (x < low) x += base;
113 
114   i = 0;
115   while (i < maxtick && x <= high) {
116     tickloc[i++] = x;
117     x           += base;
118   }
119   *ntick = i;
120 
121   if (i < 2 && num < 10) {
122     ierr = PetscADefTicks(low,high,num+1,ntick,tickloc,maxtick);CHKERRQ(ierr);
123   }
124   PetscFunctionReturn(0);
125 }
126 
127 #define EPS 1.e-6
128 
129 #undef __FUNCT__
130 #define __FUNCT__ "PetscExp10"
131 PetscErrorCode PetscExp10(PetscReal d,PetscReal *result)
132 {
133   PetscFunctionBegin;
134   *result = pow((PetscReal)10.0,d);
135   PetscFunctionReturn(0);
136 }
137 
138 #undef __FUNCT__
139 #define __FUNCT__ "PetscMod"
140 PetscErrorCode PetscMod(PetscReal x,PetscReal y,PetscReal *result)
141 {
142   int i;
143 
144   PetscFunctionBegin;
145   if (y == 1) {
146     *result = 0.0;
147     PetscFunctionReturn(0);
148   }
149   i = ((int)x) / ((int)y);
150   x = x - i * y;
151   while (x > y) x -= y;
152   *result = x;
153   PetscFunctionReturn(0);
154 }
155 
156 #undef __FUNCT__
157 #define __FUNCT__ "PetscCopysign"
158 PetscErrorCode PetscCopysign(PetscReal a,PetscReal b,PetscReal *result)
159 {
160   PetscFunctionBegin;
161   if (b >= 0) *result = a;
162   else        *result = -a;
163   PetscFunctionReturn(0);
164 }
165 
166 #undef __FUNCT__
167 #define __FUNCT__ "PetscAGetNice"
168 /*
169     Given a value "in" and a "base", return a nice value.
170     based on "sign", extend up (+1) or down (-1)
171  */
172 PetscErrorCode PetscAGetNice(PetscReal in,PetscReal base,int sign,PetscReal *result)
173 {
174   PetscReal      etmp,s,s2,m;
175   PetscErrorCode ierr;
176 
177   PetscFunctionBegin;
178   ierr    = PetscCopysign (0.5,(double)sign,&s);CHKERRQ(ierr);
179   etmp    = in / base + 0.5 + s;
180   ierr    = PetscCopysign (0.5,etmp,&s);CHKERRQ(ierr);
181   ierr    = PetscCopysign (EPS * etmp,(double)sign,&s2);CHKERRQ(ierr);
182   etmp    = etmp - 0.5 + s - s2;
183   ierr    = PetscMod(etmp,1.0,&m);CHKERRQ(ierr);
184   etmp    = base * (etmp -  m);
185   *result = etmp;
186   PetscFunctionReturn(0);
187 }
188 
189 #undef __FUNCT__
190 #define __FUNCT__ "PetscAGetBase"
191 PetscErrorCode PetscAGetBase(PetscReal vmin,PetscReal vmax,int num,PetscReal *Base,int *power)
192 {
193   PetscReal        base,ftemp,e10;
194   static PetscReal base_try[5] = {10.0,5.0,2.0,1.0,0.5};
195   PetscErrorCode   ierr;
196   int              i;
197 
198   PetscFunctionBegin;
199   /* labels of the form n * BASE */
200   /* get an approximate value for BASE */
201   base = (vmax - vmin) / (double)(num + 1);
202 
203   /* make it of form   m x 10^power,  m in [1.0, 10) */
204   if (base <= 0.0) {
205     base = PetscAbsReal(vmin);
206     if (base < 1.0) base = 1.0;
207   }
208   ftemp = log10((1.0 + EPS) * base);
209   if (ftemp < 0.0) ftemp -= 1.0;
210   *power = (int)ftemp;
211   ierr   = PetscExp10((double)-*power,&e10);CHKERRQ(ierr);
212   base   = base * e10;
213   if (base < 1.0) base = 1.0;
214   /* now reduce it to one of 1, 2, or 5 */
215   for (i=1; i<5; i++) {
216     if (base >= base_try[i]) {
217       ierr = PetscExp10((double)*power,&e10);CHKERRQ(ierr);
218       base = base_try[i-1] * e10;
219       if (i == 1) *power = *power + 1;
220       break;
221     }
222   }
223   *Base = base;
224   PetscFunctionReturn(0);
225 }
226 
227 
228 
229 
230 
231 
232