The R Project SVN R

Rev

Rev 59282 | Rev 64656 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 r 1
/*
564 maechler 2
 *  R : A Computer Language for Statistical Data Analysis
2 r 3
 *  Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
59035 ripley 4
 *  Copyright (C) 1997--2012  The R Core Team
50323 maechler 5
 *  Copyright (C) 2002--2009  The R Foundation
2 r 6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
42307 ripley 17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, a copy is available at
19
 *  http://www.r-project.org/Licenses/
2 r 20
 */
21
 
5187 hornik 22
#ifdef HAVE_CONFIG_H
44031 ripley 23
# include <config.h>
5187 hornik 24
#endif
25
 
11499 ripley 26
#include <Defn.h>
51838 ripley 27
#include <float.h>  /* for DBL_MAX */
11499 ripley 28
#include <Graphics.h>
29
#include <Print.h>
2 r 30
 
41631 ripley 31
#define imax2(x, y) ((x < y) ? y : x)
32
 
59266 ripley 33
/* used in graphics and grid */
13046 maechler 34
SEXP CreateAtVector(double *axp, double *usr, int nint, Rboolean logflag)
658 ihaka 35
{
59266 ripley 36
/*	Create an  'at = ...' vector for  axis(.)
2666 maechler 37
 *	i.e., the vector of tick mark locations,
38
 *	when none has been specified (= default).
39
 *
21044 maechler 40
 *	axp[0:2] = (x1, x2, nInt), where x1..x2 are the extreme tick marks
50323 maechler 41
 *		   {unless in log case, where nInt \in {1,2,3 ; -1,-2,....}
42
 *		    and the `nint' argument is used *instead*.}
21044 maechler 43
 
2666 maechler 44
 *	The resulting REAL vector must have length >= 1, ideally >= 2
45
 */
987 maechler 46
    SEXP at = R_NilValue;/* -Wall*/
759 ihaka 47
    double umin, umax, dn, rng, small;
2666 maechler 48
    int i, n, ne;
21044 maechler 49
    if (!logflag || axp[2] < 0) { /* --- linear axis --- Only use axp[] arg. */
59173 ripley 50
	n = (int)(fabs(axp[2]) + 0.25);/* >= 0 */
8267 ripley 51
	dn = imax2(1, n);
658 ihaka 52
	rng = axp[1] - axp[0];
2666 maechler 53
	small = fabs(rng)/(100.*dn);
658 ihaka 54
	at = allocVector(REALSXP, n + 1);
5507 ihaka 55
	for (i = 0; i <= n; i++) {
658 ihaka 56
	    REAL(at)[i] = axp[0] + (i / dn) * rng;
759 ihaka 57
	    if (fabs(REAL(at)[i]) < small)
783 maechler 58
		REAL(at)[i] = 0;
8552 maechler 59
	}
658 ihaka 60
    }
2669 maechler 61
    else { /* ------ log axis ----- */
34502 maechler 62
	Rboolean reversed = FALSE;
63
 
59174 ripley 64
	n = (int)(axp[2] + 0.5);
33335 maechler 65
	/* {xy}axp[2] for 'log': GLpretty() [./graphics.c] sets
2666 maechler 66
	   n < 0: very small scale ==> linear axis, above, or
67
	   n = 1,2,3.  see switch() below */
658 ihaka 68
	umin = usr[0];
69
	umax = usr[1];
34502 maechler 70
	if (umin > umax) {
71
	    reversed = (axp[0] > axp[1]);
72
	    if (reversed) {
73
		/* have *reversed* log axis -- whereas
74
		 * the switch(n) { .. } below assumes *increasing* values
75
		 * --> reverse axis direction here, and reverse back at end */
76
		umin = usr[1];
77
		umax = usr[0];
78
		dn = axp[0]; axp[0] = axp[1]; axp[1] = dn;
79
	    }
80
	    else {
81
		/* can the following still happen... ? */
82
		warning("CreateAtVector \"log\"(from axis()): "
83
			"usr[0] = %g > %g = usr[1] !", umin, umax);
84
	    }
85
	}
57362 ripley 86
	/* allow a fuzz since we will do things like 0.2*dn >= umin */
87
	umin *= 1 - 1e-12;
88
	umax *= 1 + 1e-12;
34502 maechler 89
 
2690 maechler 90
	dn = axp[0];
33405 maechler 91
	if (dn < DBL_MIN) {/* was 1e-300; now seems too cautious */
4179 rgentlem 92
	    warning("CreateAtVector \"log\"(from axis()): axp[0] = %g !", dn);
33405 maechler 93
	    if (dn <= 0) /* real trouble (once for Solaris) later on */
94
		error("CreateAtVector [log-axis()]: axp[0] = %g < 0!", dn);
95
	}
2669 maechler 96
 
2666 maechler 97
	/* You get the 3 cases below by
5507 ihaka 98
	 *  for (y in 1e-5*c(1,2,8))  plot(y, log = "y")
2666 maechler 99
	 */
658 ihaka 100
	switch(n) {
2666 maechler 101
	case 1: /* large range:	1	 * 10^k */
59173 ripley 102
	    i = (int)(floor(log10(axp[1])) - ceil(log10(axp[0])) + 0.25);
2666 maechler 103
	    ne = i / nint + 1;
50323 maechler 104
#ifdef DEBUG_axis
105
	    REprintf("CreateAtVector [log-axis(), case 1]: (nint, ne) = (%d,%d)\n",
106
		     nint, ne);
107
#endif
5507 ihaka 108
	    if (ne < 1)
2690 maechler 109
		error("log - axis(), 'at' creation, _LARGE_ range: "
110
		      "ne = %d <= 0 !!\n"
5731 ripley 111
		      "\t axp[0:1]=(%g,%g) ==> i = %d;	nint = %d",
2690 maechler 112
		      ne, axp[0],axp[1], i, nint);
113
	    rng = pow(10., (double)ne);/* >= 10 */
658 ihaka 114
	    n = 0;
5507 ihaka 115
	    while (dn < umax) {
658 ihaka 116
		n++;
2016 pd 117
		dn *= rng;
658 ihaka 118
	    }
5507 ihaka 119
	    if (!n)
2666 maechler 120
		error("log - axis(), 'at' creation, _LARGE_ range: "
33125 ripley 121
		      "invalid {xy}axp or par; nint=%d\n"
5731 ripley 122
		      "	 axp[0:1]=(%g,%g), usr[0:1]=(%g,%g); i=%d, ni=%d",
2666 maechler 123
		      nint, axp[0],axp[1], umin,umax, i,ne);
658 ihaka 124
	    at = allocVector(REALSXP, n);
125
	    dn = axp[0];
126
	    n = 0;
5507 ihaka 127
	    while (dn < umax) {
658 ihaka 128
		REAL(at)[n++] = dn;
2016 pd 129
		dn *= rng;
658 ihaka 130
	    }
131
	    break;
2666 maechler 132
 
133
	case 2: /* medium range:  1, 5	  * 10^k */
658 ihaka 134
	    n = 0;
5507 ihaka 135
	    if (0.5 * dn >= umin) n++;
136
	    for (;;) {
137
		if (dn > umax) break;		n++;
138
		if (5 * dn > umax) break;	n++;
2666 maechler 139
		dn *= 10;
658 ihaka 140
	    }
5507 ihaka 141
	    if (!n)
2666 maechler 142
		error("log - axis(), 'at' creation, _MEDIUM_ range: "
33125 ripley 143
		      "invalid {xy}axp or par;\n"
5731 ripley 144
		      "	 axp[0]= %g, usr[0:1]=(%g,%g)",
2666 maechler 145
		      axp[0], umin,umax);
146
 
658 ihaka 147
	    at = allocVector(REALSXP, n);
148
	    dn = axp[0];
149
	    n = 0;
5507 ihaka 150
	    if (0.5 * dn >= umin) REAL(at)[n++] = 0.5 * dn;
151
	    for (;;) {
152
		if (dn > umax) break;		REAL(at)[n++] = dn;
153
		if (5 * dn > umax) break;	REAL(at)[n++] = 5 * dn;
2666 maechler 154
		dn *= 10;
658 ihaka 155
	    }
156
	    break;
2666 maechler 157
 
158
	case 3: /* small range:	 1,2,5,10 * 10^k */
658 ihaka 159
	    n = 0;
5507 ihaka 160
	    if (0.2 * dn >= umin) n++;
161
	    if (0.5 * dn >= umin) n++;
162
	    for (;;) {
163
		if (dn > umax) break;		n++;
164
		if (2 * dn > umax) break;	n++;
165
		if (5 * dn > umax) break;	n++;
2666 maechler 166
		dn *= 10;
658 ihaka 167
	    }
5507 ihaka 168
	    if (!n)
2666 maechler 169
		error("log - axis(), 'at' creation, _SMALL_ range: "
33125 ripley 170
		      "invalid {xy}axp or par;\n"
5731 ripley 171
		      "	 axp[0]= %g, usr[0:1]=(%g,%g)",
2666 maechler 172
		      axp[0], umin,umax);
658 ihaka 173
	    at = allocVector(REALSXP, n);
174
	    dn = axp[0];
175
	    n = 0;
5507 ihaka 176
	    if (0.2 * dn >= umin) REAL(at)[n++] = 0.2 * dn;
177
	    if (0.5 * dn >= umin) REAL(at)[n++] = 0.5 * dn;
178
	    for (;;) {
179
		if (dn > umax) break;		REAL(at)[n++] = dn;
180
		if (2 * dn > umax) break;	REAL(at)[n++] = 2 * dn;
181
		if (5 * dn > umax) break;	REAL(at)[n++] = 5 * dn;
2666 maechler 182
		dn *= 10;
658 ihaka 183
	    }
184
	    break;
2666 maechler 185
	default:
33125 ripley 186
	    error("log - axis(), 'at' creation: INVALID {xy}axp[3] = %g",
2666 maechler 187
		  axp[2]);
658 ihaka 188
	}
34502 maechler 189
 
190
	if (reversed) {/* reverse back again - last assignment was at[n++]= . */
191
	    for (i = 0; i < n/2; i++) { /* swap( at[i], at[n-i-1] ) : */
192
		dn = REAL(at)[i];
193
		REAL(at)[i] = REAL(at)[n-i-1];
194
		REAL(at)[n-i-1] = dn;
195
	    }
196
	}
197
    } /* linear / log */
658 ihaka 198
    return at;
199
}
59282 ripley 200