Updated copyright text/header in most source files.
[geda-gaf/peter-b.git] / libgeda / src / m_box.c
bloba0845d067c1081907903e6de3a1caeaef3d6a58c
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
21 /*! \file m_box.c
23 * \brief Low-level mathmatical functions for boxes
26 #include <config.h>
27 #include <math.h>
28 #include <stdio.h>
30 #include "libgeda_priv.h"
32 #ifdef HAVE_LIBDMALLOC
33 #include <dmalloc.h>
34 #endif
37 /*! \brief Calculates the distance between the given point and the closest
38 * point on the perimeter or interior of the box.
40 * \param [in] box The box.
41 * \param [in] x The x coordinate of the given point.
42 * \param [in] y The y coordinate of the given point.
43 * \param [in] solid TRUE if the box should be treated as solid, FALSE if
44 * the box should be treated as hollow.
45 * \return The shortest distance from the box to the point. With a solid
46 * shape, this function returns a distance of zero for interior points. With
47 * an invalid parameter, this function returns G_MAXDOUBLE.
49 double m_box_shortest_distance (BOX *box, int x, int y, int solid)
51 double shortest_distance;
52 double x1, y1, x2, y2;
53 double dx, dy;
55 g_return_val_if_fail (box != NULL, G_MAXDOUBLE);
57 x1 = (double) min (box->upper_x, box->lower_x);
58 y1 = (double) min (box->upper_y, box->lower_y);
59 x2 = (double) max (box->upper_x, box->lower_x);
60 y2 = (double) max (box->upper_y, box->lower_y);
62 dx = min (((double)x) - x1, x2 - ((double)x));
63 dy = min (((double)y) - y1, y2 - ((double)y));
65 if (solid) {
66 dx = min (dx, 0);
67 dy = min (dy, 0);
70 if (dx < 0) {
71 if (dy < 0) {
72 shortest_distance = sqrt ((dx * dx) + (dy * dy));
73 } else {
74 shortest_distance = fabs (dx);
76 } else {
77 if (dy < 0) {
78 shortest_distance = fabs (dy);
79 } else {
80 shortest_distance = min (dx, dy);
84 return shortest_distance;