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)
5  *
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*! \file m_circle.c
22  *
23  *  \brief Low-level mathmatical functions for circles
24  */
25 
26 #include <config.h>
27 #include <math.h>
28 #include <stdio.h>
29 
30 #include "libgeda_priv.h"
31 
32 #ifdef HAVE_LIBDMALLOC
33 #include <dmalloc.h>
34 #endif
35 
36 
37 /*! \brief Calculates the distance between the given point and the closest
38  * point on the perimeter or interior of the circle.
39  *
40  *  \param [in] circle  The circle.
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 circle should be treated as solid, FALSE if
44  *  the circle should be treated as hollow.
45  *  \return The shortest distance from the circle 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.
48  */
m_circle_shortest_distance(CIRCLE * circle,int x,int y,int solid)49 double m_circle_shortest_distance (CIRCLE *circle, int x, int y, int solid)
50 {
51   double shortest_distance;
52   double distance_to_center;
53   double dx, dy;
54 
55   g_return_val_if_fail (circle != NULL, G_MAXDOUBLE);
56 
57   dx = ((double)x) - ((double)circle->center_x);
58   dy = ((double)y) - ((double)circle->center_y);
59 
60   distance_to_center = sqrt ((dx * dx) + (dy * dy));
61 
62   if (solid) {
63     shortest_distance = max (distance_to_center - circle->radius, 0);
64   } else {
65     shortest_distance = fabs (distance_to_center - circle->radius);
66   }
67 
68   return shortest_distance;
69 }
70