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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * \brief Low-level mathmatical functions for circles
30 #include "libgeda_priv.h"
32 #ifdef HAVE_LIBDMALLOC
37 /*! \brief Calculates the distance between the given point and the closest
38 * point on the perimeter or interior of the circle.
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.
49 double m_circle_shortest_distance (CIRCLE
*circle
, int x
, int y
, int solid
)
51 double shortest_distance
;
52 double distance_to_center
;
55 g_return_val_if_fail (circle
!= NULL
, G_MAXDOUBLE
);
57 dx
= ((double)x
) - ((double)circle
->center_x
);
58 dy
= ((double)y
) - ((double)circle
->center_y
);
60 distance_to_center
= sqrt ((dx
* dx
) + (dy
* dy
));
63 shortest_distance
= max (distance_to_center
- circle
->radius
, 0);
65 shortest_distance
= fabs (distance_to_center
- circle
->radius
);
68 return shortest_distance
;