File: scale_plot.c

package info (click to toggle)
quickplot 0.4.3-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 352 kB
  • ctags: 276
  • sloc: ansic: 4,124; makefile: 124; sh: 12
file content (159 lines) | stat: -rw-r--r-- 4,643 bytes parent folder | download | duplicates (2)
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
/***************************************************************
LanceMan's quickplot --- a fast interactive 2D plotter

Copyright (C) 1998, 1999  Lance Arsenault

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

Or look at http://www.gnu.org/copyleft/gpl.html .

**********************************************************************/
/* $Id: scale_plot.c,v 1.4 1999/01/15 02:19:14 lance Exp $ */
#include <stdio.h>
#include <string.h>

#include "data.h"
#include "xwin.h"

/* setup in Plot struct plot parts listed below
   double **scale;
   double **shift;
   struct Data ***start;
   struct Data ***end;
   int zoom_count;
   int num_zoom;
   double **z_scale;
   double **z_shift;
 */

int get_window_size(Display *display, Window window,
				 unsigned int *width,
				 unsigned int *height)
{
  Window root_return;
  int x_return, y_return;
  unsigned int border_width_return;
  unsigned int depth_return;
  static unsigned int w = 0, h = 0;

  XGetGeometry(display, window,
	       &root_return, &x_return, &y_return,
	       width, height, &border_width_return,
	       &depth_return);

  if(w != *width || h != *height)
    {
      w = *width;
      h = *height;
      return 1;
    }
  return 0;
}

void scale_plot(PlotXwins *win, Plot *plot, unsigned int *w, unsigned int *h)
{
  unsigned int height,width;
  static unsigned int oldheight = 0, oldwidth = 0;
  int i,j;
  double max[2], min[2];

  get_window_size(win->display,win->plotwin,&width,&height);
  *w = width; *h = height;
  if(width == oldwidth && height == oldheight)/* no size change so no scaling needed */
    return;
  if(SAME_SCALE & plot->flag)
    {
      max[X] = plot->max[plot->list[0][X]];
      max[Y] = plot->max[plot->list[0][Y]];
      min[X] = plot->min[plot->list[0][X]];
      min[Y] = plot->min[plot->list[0][Y]];         
      for(i=1;i<plot->num_plots;i++)
	{
	  for(j=0;j<2;j++) /* X = 0, Y = 1 in data.h */
	    {
	      if(max[j] < plot->max[plot->list[i][j]])
		max[j] = plot->max[plot->list[i][j]];
	      if(min[j] > plot->min[plot->list[i][j]])
		min[j] = plot->min[plot->list[i][j]];
	    }
	}
    }
  for(i=0;i<plot->num_plots;i++)
    {
      if(SAME_SCALE & plot->flag)
	{
	  if(max[X] == min[X])
	    max[X] = min[X] + 1.0;
	  if(max[Y] == min[Y])
	    max[Y] = min[Y] + 1.0;
	  plot->scale[i][X] = width/(max[X] - min[X]);
	  plot->scale[i][Y] = height/(min[Y] - max[Y]);
	  plot->shift[i][X] = -plot->scale[i][X]*min[X];
	  plot->shift[i][Y] = -plot->scale[i][Y]*max[Y];
	}
      else 
	{
	  if(plot->max[plot->list[i][X]] == plot->min[plot->list[i][X]])
	    {
	      plot->scale[i][X] = width/(
		(plot->max[plot->list[i][X]] +(i+1)) - (plot->min[plot->list[i][X]]-(plot->num_plots-i))
				     );
	      plot->shift[i][X] = -plot->scale[i][X]*(plot->min[plot->list[i][X]]-(plot->num_plots-i));
	    }
	  else
	    {
	      
	      plot->scale[i][X] = width/(
		plot->max[plot->list[i][X]] - plot->min[plot->list[i][X]]
				     );
	      plot->shift[i][X] = -plot->scale[i][X]*plot->min[plot->list[i][X]];
	    }
	  if(plot->min[plot->list[i][Y]] == plot->max[plot->list[i][Y]])
	    {
	      plot->scale[i][Y] = height/(
		 (plot->min[plot->list[i][Y]] -(plot->num_plots-i)) - (plot->max[plot->list[i][Y]]+(i+1))
		 );
	      plot->shift[i][Y] = -plot->scale[i][Y]*(plot->max[plot->list[i][Y]]+(i+1));
	    }
	  else
	    {
	      plot->scale[i][Y] = height/(
		 plot->min[plot->list[i][Y]] - plot->max[plot->list[i][Y]]
		 );
	      plot->shift[i][Y] = -plot->scale[i][Y]*plot->max[plot->list[i][Y]];

	    }
	  
	}
    }
  plot->z_scale[0][X] = FRACTION_WINDOW_USE;
  plot->z_scale[0][Y] = FRACTION_WINDOW_USE; 
  plot->z_shift[0][X] = (1.0 - FRACTION_WINDOW_USE)*width/2.0;
  plot->z_shift[0][Y] = (1.0 - FRACTION_WINDOW_USE)*height/2.0;
  if(oldwidth != 0 && oldheight != 0)
   {
     double fh,fw;
     
     fh = 1.0/oldheight; fh *= height;
     fw = 1.0/oldwidth;  fw *= width;
     for(i=1;i<=plot->zoom_count;i++)
      {
	plot->z_shift[i][X] *= fw;
	plot->z_shift[i][Y] *= fh;
      }
   }
  oldwidth = width;
  oldheight = height;
}