1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/*
*
* G N U P L O T -- graphics.c
*
* Copyright (C) 1986, 1987 Thomas Williams, Colin Kelley
*
* You may use this code as you wish if credit is given and this message
* is retained.
*
* Please e-mail any useful additions to vu-vlsi!plot so they may be
* included in later releases.
*
* This file should be edited with 4-column tabs! (:set ts=4 sw=4 in vi)
*/
#include <math.h>
#include "gnuplot.h"
char *strcpy(),*strncpy(),*strcat();
BOOLEAN log_x = 0, log_y = 0; /* set to false */
extern int term;
extern BOOLEAN term_init;
extern struct termentry term_tbl[];
#define max(a,b) ((a > b) ? a : b)
#define map_x(x) (int)((x-xmin)*xscale) /* maps floating point x to screen */
#define map_y(y) (int)((y-ymin)*yscale) /* same for y */
static double Raise(x,y)
double x;
int y;
{
int i;
double val;
val = 1.0;
for (i=0; i < abs(y); i++)
val *= x;
if (y < 0 ) return (1.0/val);
return(val);
}
double make_tics(tmin,tmax,logscale)
double tmin,tmax;
BOOLEAN logscale;
{
double xr,xnorm,tics,tic,l10;
xr = fabs(tmin-tmax);
l10 = log10(xr);
if (logscale) {
tic = Raise(10.0,(l10 >= 0.0 ) ? (int)l10 : ((int)l10-1));
if (tic < 1.0)
tic = 1.0;
} else {
xnorm = pow(10.0,l10-(double)((l10 >= 0.0 ) ? (int)l10 : ((int)l10-1)));
if (xnorm <= 2)
tics = 0.2;
else if (xnorm <= 5)
tics = 0.5;
else tics = 1.0;
tic = tics * Raise(10.0,(l10 >= 0.0 ) ? (int)l10 : ((int)l10-1));
}
return(tic);
}
char *idx(a,b)
char *a,b;
{
do {
if (*a == b)
return(a);
} while (*a++);
return(0);
}
num2str(num,str)
double num;
char str[];
{
static char temp[80];
double d;
char *a,*b;
if ((d = fabs(num)) > 9999.0 || d < 0.001 && d != 0.0)
(void) sprintf(temp,"%-.3e",num);
else
(void) sprintf(temp,"%-.3g",num);
if (b = idx(temp,'e')) {
a = b;
while ( *(--a) == '0') /* trailing zeros */
;
if ( *a == '.')
a--;
(void) strncpy(str,temp,(int)(a-temp)+1);
str[(int)(a-temp)+1] = '\0';
a = b+1; /* point to 1 after 'e' */
(void) strcat(str,"e");
if ( *a == '-')
(void) strcat(str,"-");
a++; /* advance a past '+' or '-' */
while ( *a == '0' && *(a+1) != '\0') /* leading zeroes */
a++;
(void) strcat(str,a); /* copy rest of string */
}
else
(void) strcpy(str,temp);
}
do_plot(plots, pcount, xmin, xmax, ymin, ymax)
struct curve_points *plots;
int pcount; /* count of plots in linked list */
double xmin, xmax;
double ymin, ymax;
{
int i, x;
struct termentry *t = &term_tbl[term];
BOOLEAN prev_undef;
int curve, xaxis_y, yaxis_x, dpcount;
struct curve_points *this_plot;
enum PLOT_TYPE p_type;
double xscale, yscale;
double ytic, xtic, least, most, ticplace;
int mms,mts;
static char xns[20],xms[20],yns[20],yms[20],xts[20],yts[20];
static char label[80];
if (ymin == HUGE || ymax == -HUGE)
xlfail("all points undefined!");
ytic = make_tics(ymin,ymax,log_y);
xtic = make_tics(xmin,xmax,log_x);
dpcount = 0;
if (ymin < ymax ) {
ymin = ytic * floor(ymin/ytic);
ymax = ytic * ceil(ymax/ytic);
}
else {
ymin = ytic * ceil(ymin/ytic);
ymax = ytic * floor(ymax/ytic);
}
if (xmin == xmax)
xlfail("xmin should not equal xmax!");
if (ymin == ymax)
xlfail("ymin should not equal ymax!");
(*t->init)();
if( (*t->graphics)() > 0) return(1);
yscale = (t->ymax - 2)/(ymax - ymin);
xscale = (t->xmax - 2)/(xmax - xmin);
(*t->linetype)(-2); /* border linetype */
/* draw plot border */
(*t->move)(0,0);
(*t->vector)(t->xmax-1,0);
(*t->vector)(t->xmax-1,t->ymax-1);
(*t->vector)(0,t->ymax-1);
(*t->vector)(0,0);
least = (ymin < ymax) ? ymin : ymax;
most = (ymin < ymax) ? ymax : ymin;
for (ticplace = ytic + least; ticplace < most ; ticplace += ytic) {
(*t->move)(0,map_y(ticplace));
(*t->vector)(t->h_tic,map_y(ticplace));
(*t->move)(t->xmax-1,map_y(ticplace));
(*t->vector)(t->xmax-1-t->h_tic,map_y(ticplace));
}
if (xmin < xmax ) {
least = xtic * floor(xmin/xtic);
most = xtic * ceil(xmax/xtic);
}
else {
least = xtic * ceil(xmin/xtic);
most = xtic * floor(xmax/xtic);
}
for (ticplace = xtic + least; ticplace < most ; ticplace += xtic) {
(*t->move)(map_x(ticplace),0);
(*t->vector)(map_x(ticplace),t->v_tic);
(*t->move)(map_x(ticplace),t->ymax-1);
(*t->vector)(map_x(ticplace),t->ymax-1-t->v_tic);
}
if (log_x) {
num2str(pow(10.0,xmin),xns);
num2str(pow(10.0,xmax),xms);
num2str(pow(10.0,xtic),xts);
}
else {
num2str(xmin,xns);
num2str(xmax,xms);
num2str(xtic,xts);
}
if (log_y) {
num2str(pow(10.0,ymin),yns);
num2str(pow(10.0,ymax),yms);
num2str(pow(10.0,ytic),yts);
} else {
num2str(ymin,yns);
num2str(ymax,yms);
num2str(ytic,yts);
}
mms = max(strlen(xms),strlen(yms));
mts = max(strlen(xts),strlen(yts));
(void) sprintf(label,"%s < y < %-*s inc = %-*s",yns,mms,yms,mts,yts);
(*t->lrput_text)(0, label);
(void) sprintf(label,"%s < x < %-*s inc = %-*s",xns,mms,xms,mts,xts);
(*t->lrput_text)(1, label);
/* DRAW AXES */
(*t->linetype)(-1); /* axis line type */
xaxis_y = map_y(0.0);
yaxis_x = map_x(0.0);
if (xaxis_y < 0)
xaxis_y = 0; /* save for impulse plotting */
else if (xaxis_y >= t->ymax)
xaxis_y = t->ymax - 1;
else if (!log_y) {
(*t->move)(0,xaxis_y);
(*t->vector)((t->xmax-1),xaxis_y);
}
if (!log_x && yaxis_x >= 0 && yaxis_x < t->xmax) {
(*t->move)(yaxis_x,0);
(*t->vector)(yaxis_x,(t->ymax-1));
}
/* DRAW CURVES */
this_plot = plots;
for (curve = 0; curve < pcount; this_plot = this_plot->next_cp, curve++) {
(*t->linetype)(curve);
(*t->ulput_text)(curve, this_plot->title);
(*t->linetype)(curve);
p_type = this_plot->plot_type;
switch(this_plot->plot_style) {
case IMPULSES:
for (i = 0; i < this_plot->p_count; i++) {
if (!this_plot->points[i].undefined) {
x = map_x(this_plot->points[i].x);
(*t->move)(x,xaxis_y);
(*t->vector)(x,map_y(this_plot->points[i].y));
}
}
break;
case LINES:
prev_undef = TRUE;
for (i = 0; i < this_plot->p_count; i++) {
if (!this_plot->points[i].undefined) {
x = map_x(this_plot->points[i].x);
if (prev_undef)
(*t->move)(x,
map_y(this_plot->points[i].y));
(*t->vector)(x,
map_y(this_plot->points[i].y));
}
prev_undef = this_plot->points[i].undefined;
}
break;
case POINTS:
for (i = 0; i < this_plot->p_count; i++) {
if (!this_plot->points[i].undefined) {
x = map_x(this_plot->points[i].x);
(*t->point)(x,map_y(this_plot->points[i].y),dpcount);
}
}
dpcount++;
break;
}
}
(*t->text)();
return(0);
}
|