Imported Upstream version 4.6.0
[debian/atlc] / src / non_gui / convert_create_bmp_for_rect_in_circ_dimensions_to_integers.c
1 /* atlc - arbitrary transmission line calculator, for the analysis of
2 transmission lines are directional couplers. 
3
4 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either package_version 2
9 of the License, or (at your option) any later package_version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
19 USA.
20
21 Dr. David Kirkby, e-mail drkirkby at ntlworld.com 
22
23 */
24 #include "config.h"
25
26
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #endif
30
31 #ifdef HAVE_STRINGS_H
32 #include <strings.h>
33 #endif
34
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #include "definitions.h"
39
40 /* names, colours and Ers are all arrays of 10. It would be better they were 
41 in a structure as they are all linked closely, but they are not and I
42 can't be bothered to change it */
43
44 extern double DD, ww, hh, xx, yy, Er1;
45 extern int D, w, h, x, y;
46 extern int lowest;
47 extern int highest, verbose;
48
49 void convert_create_bmp_for_rect_in_circ_dimensions_to_integers(int accuracy_level) 
50 {
51    int best_D=-1, best_w=-1, best_h=-1, best_x=-1, best_y=-1;
52    int min=-1, max=-1;
53    int i;
54    double error, error_min=VERY_LARGE, gridsize, best_grid_size=-1;
55
56    /* scale grid size,  according to a command line option */
57    lowest*=(int) 0.5+pow(2.0,(double) accuracy_level);
58    highest*=(int) 0.5+pow(2.0,(double) accuracy_level);
59
60    for(i=min;i<=max;++i)
61    {
62       /* Try various combinations for  D, w, h, x, & y etc */
63       gridsize=DD/i;
64       D=(int)(DD/gridsize + 0.5);
65       w=(int)(ww/gridsize + 0.5);
66       h=(int)(hh/gridsize + 0.5);
67       x=(int)(xx/gridsize + 0.5);
68       y=(int)(yy/gridsize + 0.5);
69       error=0.0;
70       error+=pow((DD-D*gridsize)/DD,2.0); /* relative error in D */
71       error+=pow((ww-w*gridsize)/ww,2.0);
72       error+=pow((hh-h*gridsize)/hh,2.0);
73       if(x>0)
74          error+=pow((xx-x*gridsize)/xx,2.0);
75       if(y>0)
76           error+=pow((yy-y*gridsize)/yy,2.0);
77       if(error < error_min-TINY)
78       {
79           error_min=error;
80           best_D=D;
81           best_w=w;
82           best_h=h;
83           best_x=x;
84           best_y=y;
85           best_grid_size=gridsize;
86       }
87    }
88    D=best_D;
89    w=best_w;
90    h=best_h;
91    x=best_x;
92    y=best_y;
93    if(verbose==TRUE)
94    {
95       fprintf(stderr,"error_min=%.16f\n",error_min);
96       fprintf(stderr,"User requested: D=%f w=%f h=%f x=%f y=%f Er1=%f\n\n",DD,ww,hh,xx,yy,Er1);
97       fprintf(stderr,"Internally the program is using the following grid:\n");
98       fprintf(stderr,"D=%d w=%d h=%d x=%d y=%d \n", D, w, h, x, y);
99       fprintf(stderr,"which equates to dimensions of D=%f w=%f h=%f x=%f y=%f\n", D*gridsize, w*gridsize, h*gridsize, x*gridsize,
100       y*gridsize);
101       if(error_min > TINY)
102       {
103          fprintf(stderr,"This means we are simulating a transmission line with these dimensions:\n");
104          fprintf(stderr,"W=%f w=%f h=%f x=%f y=%f (mm, inches or whatever)\n",D*best_grid_size,w*best_grid_size,h*best_grid_size,x*best_grid_size,y*best_grid_size);
105          fprintf(stderr,"\nThese are slightly different to what you indicated on the command line,\n");
106          fprintf(stderr,"but they are the best approximation possible, given the grid size\n");
107       }
108    }
109    check_error(DD,D,best_grid_size,"D");
110    check_error(ww,w,best_grid_size,"w");
111    check_error(hh,h,best_grid_size,"h");
112    check_error(xx,x,best_grid_size,"x");
113    check_error(yy,y,best_grid_size,"y");
114 }