1 /* ======================================================= *
2  * Copyright 1998-2005 Stephen C. Grubb                    *
3  * http://ploticus.sourceforge.net                         *
4  * Covered by GPL; see the file ./Copyright for details.   *
5  * ======================================================= */
6 
7 /* small, lowlevel routines re: scaled units */
8 /* see also units.c which is a layer above this. */
9 
10 #include "plg.h"
11 
12 struct plgc PLG;
13 
14 int
PLG_set_early_defaults()15 PLG_set_early_defaults()
16 {
17 /* overall settings - these can be set by application program before Einit() */
18 strcpy( Estandard_font, "/Helvetica" );
19 Estandard_textsize = 10;
20 Estandard_lwscale = 1.0;
21 strcpy( Estandard_color, "black" );
22 strcpy( Estandard_bkcolor, "white" );
23 
24 /* current parameters.. */
25 strcpy( Ecurfont, "" );
26 Ecurtextsize = 0;
27 Ecurtextheight = 0.0;
28 Ecurtextwidth = 0.0;
29 Ecurtextdirection = 0;
30 Ecurpaper = -1;
31 Ecurlinewidth = -1.0;
32 Ecurlinetype = -1;
33 Ecurpatternfactor = 0.0;
34 strcpy( Ecurcolor, "" ); /* scg 6/18/04 */
35 
36 EScale_x = 1; EScale_y = 1;
37 Escaletype_x = E_LINEAR;
38 Escaletype_y = E_LINEAR;
39 
40 strcpy( Eprogname, "" );
41 Eflip = 0;
42 Eblacklines = 0;
43 Eflashdelay = 150000;
44 
45 return( 0 );
46 }
47 
48 /* ============================ */
49 /* SCALETYPE - select the scaling method */
50 
51 int
PLG_scaletype(typ,axis)52 PLG_scaletype( typ, axis )
53 char typ[];
54 char axis;
55 {
56 
57 if( strcmp( typ, "linear" )==0 ) {
58 	/* Esetunits( axis, "linear" ); */
59 	if( axis == 'x' ) Escaletype_x = E_LINEAR;
60 	else if( axis == 'y' ) Escaletype_y = E_LINEAR;
61 	return( 0 );
62 	}
63 
64 #ifndef NOSCALE
65 else if( strcmp( typ, "log" )==0 ) {
66 	/* Esetunits( axis, "linear" ); */ /* linear ok */
67 	if( axis == 'x' ) Escaletype_x = E_LOG;
68 	else if( axis == 'y' ) Escaletype_y = E_LOG;
69 	return( 0 );
70 	}
71 
72 else if( strcmp( typ, "log+1" )==0 ) {		/* log+1 added scg 11/29/00 */
73 	if( axis == 'x' ) Escaletype_x = E_LOGPLUS1;
74 	else if( axis == 'y' ) Escaletype_y = E_LOGPLUS1;
75 	return( 0 );
76 	}
77 
78 else	{
79 	/* stat = Esetunits( axis, typ );
80 	 * if( stat != 0 ) {
81 	 *	sprintf( buf, "Invalid scaling type for %c axis", axis );
82 	 *	return( Eerr( 101, buf, typ ) );
83 	 *	}
84 	 */
85 
86 	/* special units always use linear as the basic units.. */
87 	if( axis == 'x' ) Escaletype_x = E_LINEAR;
88 	else Escaletype_y = E_LINEAR;
89 	return( 0 );
90 	}
91 #endif
92 }
93 
94 #ifndef NOSCALE
95 /* =========================== */
96 /* SCALE_X - for setting up scaling in x */
97 
98 int
PLG_scale_x(xlow,xhi,datalow,datahi)99 PLG_scale_x( xlow, xhi, datalow, datahi )
100 double 	xlow, 	/* absolute x location of left side of the area */
101 	xhi, 	/* absolute x location of the right side of the area */
102 	datalow, /* data-units x at the left side */
103 	datahi;	 /* data-units x at the right side */
104 {
105 char msgbuf[100];
106 
107 EXlo = xlow;
108 EXhi = xhi;
109 EDXlo = datalow;
110 EDXhi = datahi;
111 
112 if( datahi-datalow <= 0 ) return( Eerr( 100, "x range is invalid .. likely culprits: xautorange, or invalid date format" , "" ) );
113 
114 if( xhi-xlow <= 0 ) {
115 	sprintf( msgbuf, "Error in x absolute plot area dimensions (%g and %g)", xlow, xhi);
116 	return( Eerr( 101, msgbuf, "" ) );
117 	}
118 
119 if( Escaletype_x == E_LINEAR ) EScale_x = (xhi-xlow) / (datahi-datalow) ;
120 else if( Escaletype_x == E_LOG ) {
121 	/* if( datalow <= 0.0 ) datalow = 0.01; */ /* this line commented out 9/26/03 per paul labbe */
122 	EScale_x = (xhi-xlow) / (log( datahi ) - log( datalow ));
123 	}
124 else if( Escaletype_x == E_LOGPLUS1 ) {
125 	if( (datalow) < 0.0 ) datalow = 0.0;
126 	EScale_x = (xhi-xlow) / (log( datahi+1.0 ) - log( datalow+1.0 ));
127 	}
128 return( 0 );
129 }
130 
131 /* =========================== */
132 /* SCALE_Y - for setting up scaling in y */
133 
134 int
PLG_scale_y(ylow,yhi,datalow,datahi)135 PLG_scale_y( ylow, yhi, datalow, datahi )
136 double 	ylow, 	/* absolute y location of low side of the area */
137 	yhi, 	/* absolute y location of high side of the area */
138 	datalow, /* data-units y at the low side */
139 	datahi;	 /* data-units y at the high side */
140 {
141 char msgbuf[100];
142 
143 EYlo = ylow;
144 EYhi = yhi;
145 EDYlo = datalow;
146 EDYhi = datahi;
147 
148 if( datahi-datalow <= 0 ) return( Eerr( 100, "y range is invalid .. likely culprit is yautorange or yrange", "" ) );
149 
150 if( yhi-ylow <= 0 ) {
151 	sprintf( msgbuf, "Error in y absolute plot area dimensions (%g and %g)", ylow, yhi);
152 	return( Eerr( 101, msgbuf, "" ) );
153 	}
154 
155 if( Escaletype_y == E_LINEAR ) EScale_y = (yhi-ylow) / (datahi-datalow) ;
156 
157 else if( Escaletype_y == E_LOG ) {
158 	/* if( datalow <= 0.0 ) datalow = 0.01; */ /* this line commented out scg 9/26/03 per paul labbe */
159 	EScale_y = (yhi-ylow) / (log( datahi ) - log( datalow ));
160 	}
161 else if( Escaletype_y == E_LOGPLUS1 ) {
162 	if( (datalow) < 0.0 ) datalow = 0.0;
163 	EScale_y = (yhi-ylow) / (log( datahi+1.0 ) - log( datalow+1.0 ));
164 	}
165 return( 0 );
166 }
167 
168 /* =========================== */
169 /* A -  Returns an absolute location from a data value in xory.
170    This is the preferred function to use because it handles flip. */
171 
172 double
PLG_a(xory,d)173 PLG_a( xory, d )
174 char xory;
175 double d;
176 {
177 if( Eflip ) {
178 	if( xory == 'x' ) return( Eay( d ) );
179 	else if( xory == 'y' ) return( Eax( d ) );
180 	}
181 else	{
182 	if( xory == 'x' ) return( Eax( d ) );
183 	else if( xory == 'y' ) return( Eay( d ) );
184 	}
185 return( Eerr( 15, "Ea: nonsensical parameters", "" ) );
186 }
187 
188 
189 /* =========================== */
190 /* AX - returns an absolute x location from a data value */
191 
192 double
PLG_ax(d)193 PLG_ax( d )
194 double d;
195 {
196 
197 if( Escaletype_x == E_LINEAR )
198 	return( EXlo + (( d - EDXlo ) * EScale_x ));
199 else if( Escaletype_x == E_LOG ) {
200 	if( d <= 0.0 ) return( EXlo );
201 	else if( EDXlo <= 0.0 ) return( EXlo + (( log( d ) - log( 1.0 ) ) * EScale_x ) );
202 	else return( EXlo + (( log( d ) - log( EDXlo ) ) * EScale_x ) );
203 	}
204 else if( Escaletype_x == E_LOGPLUS1 ) {
205 	if( d <= 0.0 ) return( EXlo );
206 	else if( EDXlo <= 0.0 ) return( EXlo + (( log( d+1.0 ) - log( 1.0 ) ) * EScale_x ) );
207 	else return( EXlo + (( log( d+1.0 ) - log( EDXlo ) ) * EScale_x ) );
208 	}
209 return(0.0);
210 }
211 
212 /* =========================== */
213 /* AY - returns an absolute y location from a data value */
214 double
PLG_ay(d)215 PLG_ay( d )
216 double d;
217 {
218 if( Escaletype_y == E_LINEAR ) return( EYlo + (( d - EDYlo ) * EScale_y ));
219 else if( Escaletype_y == E_LOG ) {
220 	if( d <= 0.0 ) return( EYlo );
221 	else if( EDYlo <= 0.0 ) return( EYlo + (( log( d ) - log( 1.0 ) ) * EScale_y ) );
222 	else return( EYlo + (( log( d ) - log( EDYlo ) ) * EScale_y ) );
223 	}
224 else if( Escaletype_y == E_LOGPLUS1 ) {
225 	if( d <= 0.0 ) return( EYlo );
226 	else if( EDYlo <= 0.0 ) return( EYlo + (( log( d+1.0 ) - log( 1.0 ) ) * EScale_y ) );
227 	else return( EYlo + (( log( d+1.0 ) - log( EDYlo ) ) * EScale_y ) );
228 	}
229 return(0.0);
230 }
231 
232 
233 
234 
235 /* =========================== */
236 /* DX - given an abs coord in X, returns a value in data space */
237 
238 double
PLG_dx(a)239 PLG_dx( a )
240 double a;
241 {
242 double h;
243 if( Escaletype_x == E_LINEAR ) {
244 	h = a - EXlo;
245 	return( EDXlo + ( h / EScale_x ) );
246 	}
247 else if( Escaletype_x == E_LOG ) {
248 	if( a < EXlo ) return( EDXlo );
249 	h = log( a ) - log( EDXlo );
250 	return( EDXlo + ( h / EScale_x ) );
251 	}
252 else if( Escaletype_x == E_LOGPLUS1 ) {
253 	if( a < EXlo ) return( EDXlo );
254 	h = log( a ) - log( EDXlo );
255 	return( (EDXlo + ( h / EScale_x ) ) - 1.0 ); /* ??? */
256 	}
257 return(0.0);
258 }
259 
260 /* =========================== */
261 /* DY - given an abs coord in Y, returns a value in data space */
262 
263 double
PLG_dy(a)264 PLG_dy( a )
265 double a;
266 {
267 double h;
268 if( Escaletype_y == E_LINEAR ) {
269 	h = a - EYlo;
270 	return( EDYlo + ( h / EScale_y ) );
271 	}
272 else if( Escaletype_y == E_LOG ) {
273 	if( a < EYlo ) return( EDYlo );
274 	h = log( a ) - log( EDYlo );
275 	return( EDYlo + ( h / EScale_y ) );
276 	}
277 else if( Escaletype_y == E_LOGPLUS1 ) {
278 	if( a < EYlo ) return( EDYlo );
279 	h = log( a ) - log( EDYlo );
280 	return( (EDYlo + ( h / EScale_y ) ) - 1.0 ); /* ??? */
281 	}
282 return(0.0);
283 }
284 
285 
286 /* ====================== */
287 /* LIMIT - Get minima or maxima of either axis,
288    in either absolute or scaled units.. */
289 
290 double
PLG_limit(axis,end,units)291 PLG_limit( axis, end, units )
292 char axis;
293 char end; /* either 'l' == lo  or 'h' == hi */
294 char units; /* either 'a' == absolute or 's' == scaled */
295 {
296 if( axis == 'x' ) {
297 	if( end == 'l' && units == 's' ) return( EDXlo );
298 	else if( end == 'h' && units == 's' ) return( EDXhi );
299 	if( end == 'l' && units == 'a' ) return( EXlo );
300 	else if( end == 'h' && units == 'a' ) return( EXhi );
301 	}
302 if( axis == 'y' ) {
303 	if( end == 'l' && units == 's' ) return( EDYlo );
304 	else if( end == 'h' && units == 's' ) return( EDYhi );
305 	if( end == 'l' && units == 'a' ) return( EYlo );
306 	else if( end == 'h' && units == 'a' ) return( EYhi );
307 	}
308 Eerr( 12015, "warning, bad values passed to Elimit", "" );
309 return( 0.0 );
310 }
311 #endif
312 
313 /* ======================================================= *
314  * Copyright 1998-2005 Stephen C. Grubb                    *
315  * http://ploticus.sourceforge.net                         *
316  * Covered by GPL; see the file ./Copyright for details.   *
317  * ======================================================= */
318