libgeda

m_box.c

Go to the documentation of this file.
00001 /* gEDA - GPL Electronic Design Automation
00002  * libgeda - gEDA's library
00003  * Copyright (C) 1998-2010 Ales Hvezda
00004  * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00026 #include <config.h>
00027 #include <math.h>
00028 #include <stdio.h>
00029 
00030 #include "libgeda_priv.h"
00031 
00032 #ifdef HAVE_LIBDMALLOC
00033 #include <dmalloc.h>
00034 #endif
00035 
00036 
00049 double m_box_shortest_distance (BOX *box, int x, int y, int solid)
00050 {
00051   double shortest_distance;
00052   double x1, y1, x2, y2;
00053   double dx, dy;
00054 
00055   g_return_val_if_fail (box != NULL, G_MAXDOUBLE);
00056 
00057   x1 = (double) min (box->upper_x, box->lower_x);
00058   y1 = (double) min (box->upper_y, box->lower_y);
00059   x2 = (double) max (box->upper_x, box->lower_x);
00060   y2 = (double) max (box->upper_y, box->lower_y);
00061 
00062   dx = min (((double)x) - x1, x2 - ((double)x));
00063   dy = min (((double)y) - y1, y2 - ((double)y));
00064 
00065   if (solid) {
00066     dx = min (dx, 0);
00067     dy = min (dy, 0);
00068   }
00069 
00070   if (dx < 0) {
00071     if (dy < 0) {
00072       shortest_distance = sqrt ((dx * dx) + (dy * dy));
00073     } else {
00074       shortest_distance = fabs (dx);
00075     }
00076   } else {
00077     if (dy < 0) {
00078       shortest_distance = fabs (dy);
00079     } else {
00080       shortest_distance = min (dx, dy);
00081     }
00082   }
00083 
00084   return shortest_distance;
00085 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines