File: grap_draw.cc

package info (click to toggle)
grap 1.46-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 912 kB
  • sloc: cpp: 3,260; yacc: 1,115; lex: 1,056; sh: 941; makefile: 42
file content (189 lines) | stat: -rw-r--r-- 4,992 bytes parent folder | download | duplicates (6)
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdexcept>
#include "grap.h"
#include "grap_data.h"
#include "grap_draw.h"

// This file is (c) 1998-2001 Ted Faber (faber@lunabase.org) see COPYRIGHT
// for the full copyright and limitations of liabilities.

void coord::newx(double x) {
// Specific code to add the x value.  If the axis is being autoscaled,
// see if this point expands it.  Otherwise, sanity check it.
    if ( xautoscale == 2) {
	xmax = (x > xmax ) ? x : xmax;
	xmin = (x < xmin ) ? x : xmin;
    }

    if ( xautoscale == 1 ) {
	// first point

	xmax = xmin = x;
	xautoscale = 2;
    }
    if ( (logscale & x_axis) ) {
	if ( xmin  < min_double ) {
	    cerr << "Logscale with non-positive value (within precision)"
		 << endl;
	    xmin = min_double;
	}
	if ( xmax  < min_double ) {
	    cerr << "Logscale with non-positive value (within precision)"
		 << endl;
	    xmax = min_double;
	}
    }
}

void coord::newy(double y) {
// Specific code to add the x value.  If the axis is being autoscaled,
// see if this point expands it.  Otherwise, sanity check it.
    if ( yautoscale == 2) {
	ymax = (y > ymax ) ? y : ymax;
	ymin = (y < ymin ) ? y : ymin;
    }

    if ( yautoscale == 1 ) {
	// first point

	ymax = ymin = y;
	yautoscale = 2;
    }
    if ( (logscale & y_axis) ) {
	if ( ymin  < min_double ) {
	    cerr << "Logscale with non-positive value (within precision)"
		 << endl;
	    ymin = min_double;
	}
	if ( ymax  < min_double ) {
	    cerr << "Logscale with non-positive value (within precision)"
		 << endl;
	    ymax = min_double;
	}
    }
}

void coord::addmargin(double mf) {
// Add a margin to the coordinate system to center the plot better.
// The margin factor(mf) is given as a multiplier to the current size
// (0.07 is 7% on either size).  If the axis is logarithmic, we have
// to work in that space.
    
    double range;	// The size of the axis we're working on

    // Log sale must be positive
    
    if ( (logscale & x_axis) ) {
	if ( xmin  < min_double ) xmin = min_double;
	if ( xmax  < min_double ) xmax = min_double;
    }
    if ( (logscale & y_axis) ) {
	if ( ymin  < min_double ) ymin = min_double;
	if ( ymax  < min_double ) ymax = min_double;
    }
    
    if ( xautoscale ) {

	if ( logscale & x_axis) {
	    double b, t;	// bottom and top of the logscale range

	    // Solving the log mapping equation for 1+mf and -mf
	    // Isn't math cool?
	    
	    b = pow(xmax,-mf) / pow(xmin,(-mf-1));
	    t = pow(xmax,1+mf) / pow(xmin,mf);
	    xmin = b; xmax = t;
	} else {
	    range = xmax - xmin;
	    xmin = xmin - mf * range;
	    xmax = xmax + mf * range;
	}
    }
    
    if ( yautoscale ) {

	if ( logscale & y_axis) {
	    double b, t; 	// bottom and top of the logscale range

	    // Solving the log mapping equation for 1+mf and -mf
	    // Isn't math cool?
	    
	    b = pow(ymax,-mf) / pow(ymin,(-mf-1));
	    t = pow(ymax,1+mf) / pow(ymin,mf);
	    ymin = b; ymax = t;
	} else {
	    range = ymax - ymin;
	    ymin = ymin - mf * range;
	    ymax = ymax + mf * range;
	}
    }

    // If they're too close together, just punt.  (Actually this is less a punt
    // than it looks like.  Adding 1 works if no points have been added that
    // would define a grid - xmin == xmax == 0 and if you do almost nothing you
    // get a (0,1) x (0,1) frame).
    
    if ( xmax == xmin) xmax += 1.0;
    if ( ymax == ymin) ymax += 1.0;
}


double coord::map(double v, axis ax ) {
// map the coordinate from data space to [0,1]. 1 is the top of the axis,
// 0 the bottom.  Do it right for logscale or cartesian coordinates
    switch ( ax ) {
	case x_axis:
	    if (logscale & x_axis ) {
		if ( v > min_double )
		    return ((log(v) -log(xmin)) / (log(xmax)-log(xmin)));
		else
		    throw range_error("Negative or zero logscale coordinate");
	    }
	    else return ( (v - xmin) / (xmax - xmin ) );
	    break;
	case y_axis:
	    if (logscale & y_axis ) {
		if ( v > min_double ) 
		    return ((log(v) -log(ymin)) / (log(ymax)-log(ymin)));
		else 
		    throw range_error("Negative or zero logscale coordinate");
	    }
	    else return ( (v - ymin) / (ymax - ymin ) );
	    break;
	default:
	    return -1;
	    break;
    }
}

// Linesegment constructor. Too long for a header, but
// straightforward.  Connect this segment to the previous point in the
// line.
linesegment::linesegment(double xx, double yy, coord* cc, line *ll,
			 DisplayString *s /* =0 */, linedesc *l /* =0 */,
			 bool a /* =false */)
    : to(xx,yy,cc), from(0) {
    point *p;	// The last point on line ll.
    
    // If a null linedesc is passed in, use the one in the line
    if ( l ) desc = *l;
    else desc = ll->desc;

    if ( s ) plotstr = new DisplayString(*s);
    else {
	if ( ll->plotstr ) plotstr = new DisplayString(*ll->plotstr);
	else plotstr = 0;
    }
    arrow = a;

    if ((p = ll->lastplotted())) 
	from = new point(p);
    
    ll->lastplotted(&to);
}