Updated copyright text/header in most source files.
[geda-gaf/peter-b.git] / libgeda / src / m_basic.c
blobd00380fe4d8439771d5ed3a31e45dfa12c3625d1
1 /* gEDA - GPL Electronic Design Automation
2 * libgeda - gEDA's library
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
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 USA
20 #include <config.h>
22 #include <stdio.h>
23 #include <math.h>
25 #include "libgeda_priv.h"
27 #ifdef HAVE_LIBDMALLOC
28 #include <dmalloc.h>
29 #endif
32 /*! \brief Set the contraints for the current page.
33 * \par Function Description
34 * This function will set the current page constraints.
36 * \param [in] toplevel The TOPLEVEL object.
37 * \param [in,out] page The PAGE object to set constraints on.
38 * \param [in] xmin The minimum x coordinate for the page.
39 * \param [in] xmax The maximum x coordinate for the page.
40 * \param [in] ymin The minimum y coordinate for the page.
41 * \param [in] ymax The maximum y coordinate for the page.
43 void set_window(TOPLEVEL *toplevel, PAGE *page,
44 int xmin, int xmax, int ymin, int ymax)
46 double fs,f0,f1;
47 double fw0,fw1,fw;
49 page->left = xmin;
50 page->right = xmax;
51 page->top = ymin;
52 page->bottom = ymax;
54 /* now do the constant setups */
56 /* pix_x */
57 f0 = page->left;
58 f1 = page->right;
59 fs = toplevel->width;
60 page->to_screen_x_constant = fs / (f1 - f0);
62 /* pix_y */
63 f0 = page->top;
64 f1 = page->bottom;
65 fs = toplevel->height;
66 page->to_screen_y_constant = fs / (f1 - f0);
68 /* mil_x */
69 fw1 = page->right;
70 fw0 = page->left;
71 fw = toplevel->width;
72 page->to_world_x_constant = (fw1 - fw0) / fw;
74 /* mil_y */
75 fw1 = page->bottom;
76 fw0 = page->top;
77 fw = toplevel->height;
78 page->to_world_y_constant = (fw1 - fw0) / fw;
82 /*! \brief Rotate a point by an arbitrary angle.
83 * \par Function Description
84 * This function will rotate a point coordinate by an arbitrary angle
85 * and return the new coordinate in the newx and newy parameters.
87 * \param [in] x Input point x coordinate.
88 * \param [in] y Input point y coordinate.
89 * \param [in] angle Angle to rotate in degrees.
90 * \param [out] newx Output point x coordinate.
91 * \param [out] newy Output point y coordinate.
93 void rotate_point(int x, int y, int angle, int *newx, int *newy)
95 double costheta, sintheta;
96 double rad;
98 rad = angle*M_PI/180;
100 costheta = cos(rad);
101 sintheta = sin(rad);
103 *newx = x * costheta - y * sintheta;
104 *newy = x * sintheta + y * costheta;
107 /*! \brief Rotate point in 90 degree increments only.
108 * \par Function Description
109 * This function takes a point coordinate and rotates it by
110 * 90 degrees at a time. The new point coordinate is returned
111 * in newx and newy.
113 * \param [in] x Input point x coordinate.
114 * \param [in] y Input point y coordinate.
115 * \param [in] angle Angle to rotate by (90 degree increments only).
116 * \param [out] newx Output point x coordinate.
117 * \param [out] newy Output point y coordinate.
119 void rotate_point_90(int x, int y, int angle, int *newx, int *newy)
121 double costheta=1;
122 double sintheta=0;
124 /* I could have used sine/cosine for this, but I want absolute
125 * accuracy */
126 switch(angle) {
128 case(0):
129 *newx = x;
130 *newy = y;
131 return;
132 break;
134 case(90):
135 costheta = 0;
136 sintheta = 1;
137 break;
139 case(180):
140 costheta = -1;
141 sintheta = 0;
142 break;
144 case(270):
145 costheta = 0;
146 sintheta = -1;
147 break;
150 *newx = x * costheta - y * sintheta;
151 *newy = x * sintheta + y * costheta;
155 /*! \brief Convert Paper size to World coordinates.
156 * \par Function Description
157 * This function takes the paper size and converts it to
158 * world coordinates. It supports landscape with a fixed aspect ratio.
160 * \param [in] width Paper width. (units?)
161 * \param [in] height Paper height. (units?)
162 * \param [in] border Paper border size. (units?)
163 * \param [out] right Right world coordinate. (units?)
164 * \param [out] bottom Bottom world coordinate. (units?)
166 * \todo Support more modes than just landscape only mode.
168 void PAPERSIZEtoWORLD(int width, int height, int border, int *right, int *bottom)
170 float aspect;
172 aspect = (float) width / (float) height;
174 #if DEBUG
175 printf("%f\n", aspect);
176 #endif
178 if (aspect < 1.333333333) {
179 /* is this lrint really needed? */
180 #ifdef HAVE_LRINT
181 *right = lrint (width+border + ((height+border)*1.33333333 - (width+border)));
182 #else
183 *right = (int) width+border +
184 ((height+border)*1.33333333 - (width+border));
185 #endif
186 *bottom = height+border;
187 } else {
188 *right = (int) width+border;
189 *bottom = (int) height+border + ((width+border)/1.33333333 - (height+border));
192 #if DEBUG
193 aspect = (float) *right / (float) *bottom;
194 printf("%f\n", aspect);
195 #endif